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 |
过得好纠结#include<iostream> #include<queue> using namespace std; struct point { int x , y , z , step ; }; point temp , next ; int i , j , k ; int a , b , c ; int startx , starty , startz , endx , endy , endz ; int dx[6] = { 1 , -1 , 0 , 0 , 0 , 0 } ; int dy[6] = { 0 , 0 , 1 , -1 , 0 , 0 } ; int dz[6] = { 0 , 0 , 0 , 0 , 1 , -1} ; char map[31][31][31] ; bool visit[31][31][31] , flag ; int main () { while ( cin >> a >> b >> c && a + b + c ) { queue<point> q ; flag = true ; memset( visit , true , sizeof( visit ) ) ; for ( i = 0 ; i < a ; i ++ ) for ( j = 0 ; j < b ; j ++ ) for ( k = 0 ; k < c ; k ++ ) { cin >> map[i][j][k] ; if ( map[i][j][k] == 'S' ) { startx = i ; starty = j ; startz = k ; } if ( map[i][j][k] == 'E' ) { endx = i ; endy = j ; endz = k ; } } visit[startx][starty][startz] = false ; temp.x = startx ; temp.y = starty ; temp.z = startz ; temp.step = 0 ; q.push( temp ) ; while ( ! q.empty() ) { if ( startx == endx && starty == endy && startz == endz ) { cout << "Escaped in 0 minute(s)." << endl ; break ; } temp = q.front() ; q.pop() ; for ( i = 0 ; i < 6 ; i ++ ) { if ( temp.x+dx[i]<a && temp.x+dx[i]>=0 && temp.y+dy[i]<b && temp.y+dy[i]>=0 && temp.z+dz[i]<c && temp.z+dz[i]>=0 && visit[temp.x+dx[i]][temp.y+dy[i]][temp.z+dz[i]] && map[temp.x+dx[i]][temp.y+dy[i]][temp.z+dz[i]] != '#') { next.x = temp.x + dx[i] ; next.y = temp.y + dy[i] ; next.z = temp.z + dz[i] ; next.step = temp.step + 1 ; visit[next.x][next.y][next.z] = false ; q.push(next) ; if (next.x == endx && next.y == endy && next.z == endz ) { cout << "Escaped in "<<next.step<<" minute(s)." << endl ; flag = false ; } } } if ( ! flag ) break ; } if ( flag ) 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