Thief of Wealth
Published 2019. 4. 25. 00:57
6594 Dungeon Master 개발/알고리즘




시간 제한메모리 제한제출정답맞은 사람정답 비율
1 초128 MB41211345104932.874%

문제

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

입력

The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).

L is the number of levels making up the dungeon.

R and C are the number of rows and columns making up the plan of each level.

Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

출력

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.

If it is not possible to escape, print the line

Trapped!

간단한 BFS문제인데 2번이나 틀렸다.

구현은 맞았는데, 

테스트 케이스마다 que를 초기화 안해줘서 틀렸었다.

while(!que,empty())일때만 빠져나온다 생각하다보니, 안일해졌다;;


나머지는 그냥 3차원으로 BFS돌리면된당


#include <bits/stdc++.h>
using namespace std;

struct Point{
int z;
int x;
int y;
};

//상 하 좌 우 전 후
int z_direction[6] = {-1,1,0,0,0,0};
int x_direction[6] = {0,0,0,0,-1,1};
int y_direction[6] = {0,0,-1,1,0,0};

int L,R,C;

char building[31][31][31];
bool visited[31][31][31];

queue<Point> que;

int bfs();

int main(){

freopen("input.txt","r",stdin);

ios::sync_with_stdio(false);
cin.tie(0);
char temp;

cin>>L>>R>>C;
while( !(L==0 && R==0 && C==0) ){
memset(building, 0, sizeof(building));
memset(visited, false, sizeof(visited));

while(!que.empty()){ que.pop(); }

for(int i=0; i<L; ++i){
for(int j=0; j<R; ++j){
for(int k=0; k<C; ++k){
cin>>building[i][j][k];
if( building[i][j][k] == 'S' ){
que.push({i,j,k}); // 미리추가
visited[i][j][k]=true; // 방문표시까지 먼저 해보리기
} else if(building[i][j][k] == '#'){ // 지나갈 수 없으면 그냥 미리 방문처리 해놓자
visited[i][j][k]=true;
}
}
}
}
int didFind = bfs();
if( didFind != -1 ){
cout<<"Escaped in "<<didFind<<" minute(s).\n";
}else{
cout<<"Trapped!\n";
}
cin>>L>>R>>C;
}

return 0;
}


int bfs(){

Point p;
int step_size;
int total_step = 0;
while(!que.empty()){
step_size = que.size();
for(int i=0; i<step_size; ++i){
p = que.front();
que.pop();
if( building[p.z][p.x][p.y] == 'E' ) {//탈출구인가
return total_step;
}
for(int j=0; j<6; ++j){
if(
((p.z+z_direction[j] < L) && (p.z+z_direction[j]>=0)) &&
((p.x+x_direction[j] < R) && (p.x+x_direction[j]>=0)) &&
((p.y+y_direction[j] < C) && (p.y+y_direction[j]>=0)) &&
!visited[p.z+z_direction[j]][p.x+x_direction[j]][p.y+y_direction[j]]
){
que.push({p.z+z_direction[j],p.x+x_direction[j],p.y+y_direction[j]});
visited[p.z+z_direction[j]][p.x+x_direction[j]][p.y+y_direction[j]] = true;
}
}
}
total_step++;
}
return -1;
}














'개발 > 알고리즘' 카테고리의 다른 글

13913 숨바꼭질4  (0) 2019.04.28
13549 숨바꼭질3  (1) 2019.04.27
5014 Elevator Trouble / 스타트링크  (0) 2019.04.21
2146 다리만들기  (0) 2019.04.14
5427 불  (0) 2019.04.14
profile on loading

Loading...