Some details are not handled well. Wa has been done many times.
Pruning is not strong, and it runs for more than 6000 ms.
View code
1 /* 2 Author:Zhaofa Fang 3 Lang:C++ 4 */ 5 #include <cstdio> 6 #include <cstdlib> 7 #include <sstream> 8 #include <iostream> 9 10 #include <cstring>11 #include <algorithm>12 #include <string>13 #include <utility>14 #include <vector>15 #include <queue>16 #include <stack>17 #include <map>18 #include <set>19 20 using namespace std;21 22 typedef long long ll;23 #define DEBUG(x) cout<< #x << ':' << x << endl24 #define PII pair<int,int>25 #define PB push_back26 #define MP make_pair27 #define FI first28 #define SE second29 #define lowbit(x) (x&(-x))30 #define INF (1<<30)31 32 const double eps = 1e-6;33 34 int maz[1005][1005];35 int dx[]={1,0,0,-1};36 int dy[]={0,1,-1,0};37 bool OK;38 int n,m;39 int x1,y1,x2,y2;40 bool check(int x,int y)41 {42 if(x<1 || x>n || y<1 || y>m)return false;43 return true;44 }45 void dfs(int x,int y,int turn,int dire)46 {47 if(turn > 2 || !check(x,y))return;48 if(OK)return ;49 if(x == x2 && y == y2)50 {51 OK = 1;52 return;53 }54 for(int i=0;i<4;i++)55 {56 if(dire + i == 3)continue;57 int xx = x + dx[i];58 int yy = y + dy[i];59 if((xx != x2 || yy != y2) && maz[xx][yy] != 0)continue;60 if(dire != i && dire != -1)dfs(xx,yy,turn + 1,i);61 else dfs(xx,yy,turn,i);62 }63 }64 65 int main()66 {67 #ifndef ONLINE_JUDGE68 freopen("in","r",stdin);69 #endif70 while(~scanf("%d%d",&n,&m),n,m)71 {72 for(int i=1;i<=n;i++)73 {74 for(int j=1;j<=m;j++)75 scanf("%d",&maz[i][j]);76 }77 int q;78 scanf("%d",&q);79 while(q--)80 {81 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);82 if(maz[x1][y1] != maz[x2][y2]83 || maz[x1][y1] == 0 || maz[x2][y2] == 0)84 {85 puts("NO");86 continue;87 }88 OK=0;89 dfs(x1,y1,0,-1);90 if(OK)puts("YES");91 else puts("NO");92 }93 }94 return 0;95 }