티스토리 뷰
https://www.acmicpc.net/problem/6593
3차원 상에서 시작 지점 S에서 출구 E까지의 최단 거리를 구하는 문제이다. BFS로 구할 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <cstdio> #include <queue> using namespace std; int l,r,c; char map[31][31][31]; bool visit[31][31][31]; int dx[6]={-1,1,0,0,0,0}; int dy[6]={0,0,0,0,-1,1}; int dz[6]={0,0,-1,1,0,0}; queue<pair<pair<int,int>,int>> q; void bfs() { int depth=1; while(!q.empty()) { int size=q.size(); while(size--) { auto now=q.front(); q.pop(); int x=now.first.first,y=now.first.second,z=now.second; for(int i=0;i<6;i++) { int nx=x+dx[i],ny=y+dy[i],nz=z+dz[i]; if(nx<0 || nx>=l || ny<0 || ny>=r || nz<0 || nz>=c) continue; if(map[nx][ny][nz]=='E') { printf("Escaped in %d minute(s).\n",depth); return; } if(map[nx][ny][nz]=='.' && !visit[nx][ny][nz]) visit[nx][ny][nz]=true,q.push({{nx,ny},nz}); } } depth++; } printf("Trapped!\n"); } int main() { while(true) { scanf("%d %d %d",&l,&r,&c); if(l==0 && r==0 && c==0) break; for(int i=0;i<l;i++) for(int j=0;j<r;j++) scanf("%s",map[i][j]); for(int i=0;i<l;i++) for(int j=0;j<r;j++) for(int k=0;k<c;k++) if(map[i][j][k]=='S') visit[i][j][k]=true,q.push({{i,j},k}); bfs(); fill_n(&visit[0][0][0],31*31*31,false); while(!q.empty()) q.pop(); } return 0; } | cs |
'Algorithm > BOJ' 카테고리의 다른 글
1854 K번째 최단경로 찾기 (0) | 2018.03.05 |
---|---|
2667 단지번호 붙이기 (0) | 2018.03.03 |
1504 특정한 최단 경로 (0) | 2018.03.03 |
1753 최단경로 (0) | 2018.03.03 |
1149 RGB거리 (0) | 2018.03.02 |