1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <queue> #include <deque> using namespace std; const int maxn=35; inline int read() { int x=0,f=1; char c=getchar(); while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9') { x=(x<<1)+(x<<3)+c-'0'; c=getchar(); } return x*f; } inline void write(int x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } struct node{ int x,y,ddx,ddy,st; }; int n,m,T; int a[maxn][maxn]; bool vis[maxn][maxn][maxn][maxn]; int dx[]={0,0,1,0,-1}; int dy[]={0,1,0,-1,0};
int main() { std::ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n>>m>>T; for(register int i=1;i<=n;i++) { for(register int j=1;j<=m;j++) cin>>a[i][j]; } while(T--) { deque <node> q; bool flag=false; memset(vis,false,sizeof(vis)); int ex,ey,sx,sy,tx,ty; cin>>ex>>ey>>sx>>sy>>tx>>ty;
node now; now=(node){sx,sy,ex,ey,0}; vis[ex][ey][sx][sy]=true; q.push_back(now); while(q.size()) { now=q.front(); q.pop_front(); int x=now.x; int y=now.y; int lx=now.ddx; int ly=now.ddy; if(x==tx&&y==ty) { flag=true; cout<<now.st<<'\n'; break; } int fx=lx+0,fy=ly+1; int vx,vy; if(fx<n||fy<m||fx>1||fy>1) { vx=x,vy=y; if(x==fx&&y==fy) vx=lx,vy=ly; if(a[fx][fy]&&!vis[fx][fy][vx][vy]) { vis[fx][fy][vx][vy]=true; node nt; nt=(node){vx,vy,fx,fy,now.st+1}; q.push_back(nt); } } fx=lx+1,fy=ly+0; if(fx<n||fy<m||fx>1||fy>1) { vx=x,vy=y; if(x==fx&&y==fy) vx=lx,vy=ly; if(a[fx][fy]&&!vis[fx][fy][vx][vy]) { vis[fx][fy][vx][vy]=true; node nt; nt=(node){vx,vy,fx,fy,now.st+1}; q.push_back(nt); } } fx=lx+0,fy=ly-1; if(fx<n||fy<m||fx>1||fy>1) { vx=x,vy=y; if(x==fx&&y==fy) vx=lx,vy=ly; if(a[fx][fy]&&!vis[fx][fy][vx][vy]) { vis[fx][fy][vx][vy]=true; node nt; nt=(node){vx,vy,fx,fy,now.st+1}; q.push_back(nt); } } fx=lx-1,fy=ly+0; if(fx<n||fy<m||fx>1||fy>1) { vx=x,vy=y; if(x==fx&&y==fy) vx=lx,vy=ly; if(a[fx][fy]&&!vis[fx][fy][vx][vy]) { vis[fx][fy][vx][vy]=true; node nt; nt=(node){vx,vy,fx,fy,now.st+1}; q.push_back(nt); } } } if(!flag) cout<<-1<<'\n'; } return 0; }
|