标签:
一开始很疑惑自己为什么错了,后来才知道原来这题是可以往回 走的.开4维visit数组记录状态,状态不同则往回走,相同再往回走.
1
30 31 95
S..............................
..X............................
...X...........................
....X..........................
.....X.........................
......X........................
.......X.......................
........X......................
.........X.....................
..........X....................
...........X...................
............X..................
.............X.................
..............X................
...............X...............
................X..............
.................X.............
..................X............
...................X...........
....................X..........
.....................X.........
......................X........
.......................X.......
........................X......
.........................X.....
..........................X....
...........................X...
............................X..
.............................XE
..............................D
结果是88,如果这组数据能过那就没问题了.
#include<iostream> #include<queue> #include<cstring> using namespace std; int n,m,t; char mapp[105][105]; int visit[105][105][2][2]; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int dx,dy,ex,ey,sx,sy; int re; int flag1,flag2; struct stu { int x,y,s; int flag1,flag2; }; int judgee(int x,int y) { int flag=0; if(x==ex) { int nn=min(y,ey),mm=max(y,ey); int sum=0; for(int i=nn+1;i<mm;i++) { if(mapp[x][i]=='.') sum++; } if(sum==(mm-nn-1)) flag=1; } if(y==ey) { int nn=min(x,ex),mm=max(x,ex); int sum=0; for(int i=nn+1;i<mm;i++) { if(mapp[i][y]=='.') sum++; } if(sum==(mm-nn-1)) flag=1; } return flag; } int judged(int x,int y) { int flag=0; if(x==dx) { int nn=min(y,dy),mm=max(y,dy); int sum=0; for(int i=nn+1;i<mm;i++) { if(mapp[x][i]=='.') sum++; } if(sum==(mm-nn-1)) flag=1; } if(y==dy) { int nn=min(x,dx),mm=max(x,dx); int sum=0; for(int i=nn+1;i<mm;i++) { if(mapp[i][y]=='.') sum++; } if(sum==(mm-nn-1)) flag=1; } return flag; } void bfs() { stu x,y; queue<stu>root; x.x=sx;x.y=sy;x.s=0;x.flag1=0;x.flag2=0; root.push(x); if(judgee(x.x,x.y)) x.flag1=1; if(judged(x.x,x.y)) x.flag2=1; visit[x.x][x.y][x.flag1][x.flag2]=1; while(root.size()) { x=root.front(); //cout<<x.x<<x.y<<endl; if(judgee(x.x,x.y)) x.flag1=1; if(judged(x.x,x.y)) x.flag2=1; //cout<<x.x<<x.y<<x.flag1<<x.flag2<<endl; if(x.flag1&&x.flag2) {re=x.s;return;} root.pop(); for(int i=0;i<4;i++) { y.x=x.x+dir[i][0]; y.y=x.y+dir[i][1]; y.flag1=x.flag1; y.flag2=x.flag2; y.s=x.s+1; if(y.x<0||y.x>=n||y.y<0||y.y>=m||visit[y.x][y.y][y.flag1][y.flag2]||mapp[y.x][y.y]!='.'||y.s>t){continue;} root.push(y); visit[y.x][y.y][y.flag1][y.flag2]=1; } } } int main() { int u,p; cin>>u; for(int p=0;p<u;p++) { cin>>n>>m>>t; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>mapp[i][j]; if(mapp[i][j]=='S') sx=i,sy=j; if(mapp[i][j]=='E') ex=i,ey=j; if(mapp[i][j]=='D') dx=i,dy=j; // visit[i][j]=-1; } } memset(visit,0,sizeof(visit)); mapp[sx][sy]='.'; re=-1; bfs(); cout<<"Case "<<p+1<<":"<<endl; cout<<re<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/44995363