#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; int an[1100][1100]; int n,m; bool f1(int x1,int y1,int x2,int y2) { if(x1 == x2) { int a = 0; for(int i = min(y1,y2)+1;i<max(y1,y2);i++) { a+=an[x1][i]; } if(a==0) return 1; } if(y1 == y2) { int a = 0; for(int i = min(x1,x2)+1;i<max(x1,x2);i++) { a+=an[i][y1]; } if(a==0) return 1; } return 0; } bool f2(int x1,int y1,int x2,int y2) { if(an[x1][y2] == 0&&f1(x1,y1,x1,y2)&&f1(x1,y2,x2,y2)) return 1; if(an[x2][y1] == 0&&f1(x1,y1,x2,y1)&&f1(x2,y1,x2,y2)) return 1; return 0; } bool f3(int x1,int y1,int x2,int y2) { int i; for(i = 1;i<=n;i++) { if(an[i][y1] == 0&&an[i][y2] == 0&&f1(x1,y1,i,y1)&&f1(i,y1,i,y2)&&f1(i,y2,x2,y2))return 1; } for(i = 1;i<=m;i++) { if(an[x1][i] == 0&&an[x2][i] == 0&&f1(x1,y1,x1,i)&&f1(x1,i,x2,i)&&f1(x2,i,x2,y2))return 1; } return 0; } int main() { int i,j,k; while(cin>>n>>m) { if(n == 0&&m == 0) return 0; for(i = 1;i<=n;i++) for(j = 1;j<=m;j++) scanf("%d",&an[i][j]); int q; cin>>q; while(q--) { int x1,x2,y1,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); bool bl = 0; if(f1(x1,y1,x2,y2))bl = 1; else if(f2(x1,y1,x2,y2))bl = 1; else if(f3(x1,y1,x2,y2))bl = 1; if(an[x1][y1]!=an[x2][y2]||an[x1][y1] == 0) bl = 0; if(bl) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0; }
原文:http://www.cnblogs.com/wos1239/p/4565039.html