标签:
感觉这题挺水的,dfs 6000ms过
#include<iostream> #include<cstring> #define maxn 1000+5 using namespace std; int n,m; int mapp[maxn][maxn]; int visit[maxn][maxn]; int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int sx,sy,ex,ey; int flag; void dfs(int x,int y,int dire,int cha) { visit[x][y]=1; if(flag){return;} if(x==ex&&y==ey) {flag=1;return;} for(int i=0;i<4;i++) { int xx=x+dir[i][0],yy=y+dir[i][1],diree=dire,chaa=cha; if(diree!=i) diree=i,chaa++; if(xx<0||xx>=n||yy<0||yy>=m||chaa>3||(mapp[xx][yy]&&(xx!=ex||yy!=ey))||visit[xx][yy]) { //cout<<xx<<" "<<yy<<"b"<<endl; continue; } //cout<<xx<<" "<<yy<<" "<<diree<<" "<<chaa<<"a"<<endl; dfs(xx,yy,diree,chaa); //cout<<xx<<' '<<yy<<endl; visit[xx][yy]=0; } } int main() { while(cin>>n>>m&&n&&m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>mapp[i][j]; } } int p; cin>>p; while(p--) { cin>>sx>>sy>>ex>>ey; sx--;sy--;ex--;ey--; if(!mapp[sx][sy]||!mapp[ex][ey]||mapp[sx][sy]!=mapp[ex][ey]||(sx==ex&&sy==ey)) {cout<<"NO"<<endl;continue;} memset(visit,0,sizeof(visit)); flag=0; dfs(sx,sy,-1,0); if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/45010377