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 |
用结构体的同学看过来 终于ac了#include <iostream> #include <cstring> #include <queue> using namespace std; const int N=35; struct SE{ int l,r,c,ans; bool operator ==(const SE &b)const{ return (this->l==b.l && this->r==b.r && this->c==b.c); } }; SE s,e; bool map[N][N][N],vis[N][N][N]; int L,R,C,shortans; bool BFS(){ int i; queue<SE> q; vis[s.l][s.r][s.c]=true; q.push(s); while(!q.empty()){ SE u; u=q.front(), q.pop(); if(u==e){ shortans=u.ans; return true; } if(!vis[u.l+1][u.r][u.c] && map[u.l+1][u.r][u.c]){ vis[u.l+1][u.r][u.c]=1; SE next; next.l=u.l+1; next.r=u.r; next.c=u.c; next.ans=u.ans+1; q.push(next); } if(!vis[u.l-1][u.r][u.c] && map[u.l-1][u.r][u.c]){ vis[u.l-1][u.r][u.c]=1; SE next; next.l=u.l-1; next.r=u.r; next.c=u.c; next.ans=u.ans+1; q.push(next); } if(!vis[u.l][u.r+1][u.c] && map[u.l][u.r+1][u.c]){ vis[u.l][u.r+1][u.c]=1; SE next; next.l=u.l; next.r=u.r+1; next.c=u.c; next.ans=u.ans+1; q.push(next); } if(!vis[u.l][u.r-1][u.c] && map[u.l][u.r-1][u.c]){ vis[u.l][u.r-1][u.c]=1; SE next; next.l=u.l; next.r=u.r-1; next.c=u.c; next.ans=u.ans+1; q.push(next); } if(!vis[u.l][u.r][u.c+1] && map[u.l][u.r][u.c+1]){ vis[u.l][u.r][u.c+1]=1; SE next; next.l=u.l; next.r=u.r; next.c=u.c+1; next.ans=u.ans+1; q.push(next); } if(!vis[u.l][u.r][u.c-1] && map[u.l][u.r][u.c-1]){ vis[u.l][u.r][u.c-1]; SE next; next.l=u.l; next.r=u.r; next.c=u.c-1; next.ans=u.ans+1; q.push(next); } } return false; } int main(){ ios::sync_with_stdio(false); while(scanf("%d%d%d",&L,&R,&C)!=EOF && (L+R+C)){ memset(map,false,sizeof(map)); memset(vis,false,sizeof(vis)); char cc; s.ans=0; for(int l=1;l <= L;l++){//前面错在这里 从1开始,从0开始的话,会搜到-1 for(int r=1;r <= R;r++){ for(int c=1;c <= C;c++){ cin>>cc; if(cc== '.') map[l][r][c]=true; if(cc== 'S'){ map[l][r][c]=true; s.l=l; s.r=r; s.c=c; } if(cc== 'E'){ map[l][r][c]=true; e.l=l; e.r=r; e.c=c; } } } } if(BFS()) printf("Escaped in %d minute(s).\n",shortans); else printf("Trapped!\n"); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator