Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这么标准的BFS,错的很诡异啊。。。WA无数次#include <iostream> using namespace std; struct node { int x,y,z; }que[3000]; char map[40][40][40]; int use[40][40][40]; int dis[40][40][40]; int d[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}}; int r,c,stages,sx,sy,sz,ex,ey,ez; int bfs() { int i,front,rear,curx,cury,curz; que[0].x=sx; que[0].y=sy; que[0].z=sz; use[sz][sx][sy]=1; front=0; rear=1; memset(que,'\0',sizeof(que)); while(front!=rear) { curx=que[front].x; cury=que[front].y; curz=que[front].z; for(i=0;i<6;i++) { if(curz+d[i][0]>=0&&curz+d[i][0]<stages&&curx+d[i][1]>=0&&curx+d[i][1]<r&&cury+d[i][2]>=0&&cury+d[i][2]<c&&use[curz+d[i][0]][curx+d[i][1]][cury+d[i][2]]==0&&map[curz+d[i][0]][curx+d[i][1]][cury+d[i][2]]!='#') { use[curz+d[i][0]][curx+d[i][1]][cury+d[i][2]]=1; dis[curz+d[i][0]][curx+d[i][1]][cury+d[i][2]]=dis[curz][curx][cury]+1; if(map[curz+d[i][0]][curx+d[i][1]][cury+d[i][2]]=='E') return 1; que[rear].x=curx+d[i][1]; que[rear].y=cury+d[i][2]; que[rear].z=curz+d[i][0]; rear++; } } front++; } return 0; } int main() { int i,j,k; while(cin>>stages>>r>>c) { if(stages==0&&r==0&&c==0) return 0; for(i=0;i<stages;i++) { for(j=0;j<r;j++) { for(k=0;k<c;k++) { cin>>map[i][j][k]; if(map[i][j][k]=='S') { sx=j; sy=k; sz=i; } if(map[i][j][k]=='E') { ex=j; ey=k; ez=i; } } } } memset(use,0,sizeof(use)); memset(dis,0,sizeof(dis)); if(bfs()) cout<<"Escaped in "<<dis[ez][ex][ey]<<" minute(s)."<<endl; else cout<<"Trapped!"<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator