1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1010; 8 int n,m,x1,y1,x2,y2,dir; 9 int vis[maxn][maxn]; 10 int pic[maxn][maxn]; 11 int dix[4] = {0,1,0,-1}; 12 int diy[4] = {1,0,-1,0}; 13 int flag; 14 15 void dfs(int x,int y,int turn,int dir) 16 { 17 if(flag) return; 18 if(x < 1 || x > n || y < 1 || y > m || vis[x][y]) return; 19 if(turn == 2 && x != x2 && y != y2) return; 20 if(turn > 2) return; 21 if(x == x2 && y == y2 && turn <= 2) 22 { 23 printf("YES\n"); 24 flag = 1; 25 return; 26 } 27 if(pic[x][y] != 0) 28 { 29 if(x == x1 && y == y1); 30 else return; 31 } 32 vis[x][y] = 1; 33 int dx,dy; 34 for(int i = 0;i < 4;i++) 35 { 36 dx = x + dix[i]; 37 dy = y + diy[i]; 38 if(dir == i) 39 { 40 dfs(dx,dy,turn,i); 41 } 42 else 43 { 44 dfs(dx,dy,turn + 1,i); 45 } 46 } 47 vis[x][y] = 0; 48 return; 49 } 50 51 int main() 52 { 53 while(scanf("%d%d",&n,&m) && n && m) 54 { 55 for(int i = 1;i <= n;i++) 56 { 57 for(int j = 1;j <= m;j++) 58 { 59 scanf("%d",&pic[i][j]); 60 } 61 } 62 int k; 63 scanf("%d",&k); 64 while(k--) 65 { 66 scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 67 if(x1 == x2 && y1 == y2) 68 { 69 printf("NO\n"); 70 continue; 71 } 72 if(x1 < 1 || x1 > n || y1 < 1 || y1 > m || x2 < 1 || x2 > n || y2 < 1 || y2 > m) 73 { 74 printf("NO\n"); 75 continue; 76 } 77 if(pic[x1][y1] == pic[x2][y2] && pic[x1][y1]) 78 { 79 flag = 0; 80 memset(vis,0,sizeof(vis)); 81 for(int i = 0;i < 4;i++) 82 { 83 dfs(x1,y1,0,i); 84 } 85 if(!flag) printf("NO\n"); 86 } 87 else 88 { 89 printf("NO\n"); 90 } 91 } 92 } 93 return 0; 94 }