Algorithm

[BOJ] 6593번 : 상범 빌딩

rudgh99_algo 2024. 9. 19. 16:26

1. problem : 

https://www.acmicpc.net/problem/6593

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;
char board[32][32][32]; 
int dx[6] = { 0,1,0,-1,0,0 }; 
int dy[6] = { 1,0,-1,0,0,0 }; 
int dz[6] = { 0,0,0,0,1,-1 };
queue<tuple<int,int,int>> Q;
int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0); 
	while (true) {
		int L, R, C; 
		cin >> L >> R >> C; 
		int isvis[32][32][32] = {};
		while (!Q.empty()) Q.pop();
		if (L == 0 && R == 0 && C == 0) break;
		for (int i = 0; i < L; i++) {
			for (int j = 0; j < R; j++) {
				string s; 
				cin >> s; 
				for (int k = 0; k < C; k++) {
					board[i][j][k] = s[k];
					if (s[k] == 'S') {
						Q.push({ i,j,k });
						isvis[i][j][k] = 1; 
					}
				}
			}
		}
		bool escaped = false;
		while (!Q.empty()) {
			auto cur = Q.front(); Q.pop(); 
			int z = get<0>(cur);
			int x = get<1>(cur);
			int y = get<2>(cur);
			for (int dir = 0; dir < 6; dir++) {
				int nz = z + dz[dir]; 
				int nx = x + dx[dir]; 
				int ny = y + dy[dir]; 
				if (nx < 0 || nx >= R || ny < 0 || ny >= C || nz < 0 || nz >= L) continue; 
				if (isvis[nz][nx][ny] || board[nz][nx][ny] == '#') continue;
				Q.push({ nz,nx,ny });
				isvis[nz][nx][ny] = isvis[z][x][y] + 1;
				if (board[nz][nx][ny] == 'E') {
					cout << "Escaped in " << isvis[nz][nx][ny] - 1 << " minute(s)." << '\n';
					escaped = true;
					break;
				}
			}
		}
		if (!escaped) cout << "Trapped!" << '\n';
	}
}

bfs를 이용해 풀었다. 나는 어디가 잘못되었는지 모르겠지만, 채점과정에서 틀렸습니다가 계속 떠서, chatGPT한테 물어보니, Q의 초기화, isvis의 초기화, 그리고 'E'를 찾는 즉시 종료라는 조건이 더 들어가야 한다고 했다. 

 

3. solution 2 :

// Authored by : yongjunleeme
// Co-authored by : -
// http://boj.kr/eee7356385264e9195a2b641b856fe67
#include <bits/stdc++.h>
using namespace std;
const int MX = 31;
int dx[6] = {0, 0, 1, -1, 0, 0};
int dy[6] = {1, -1, 0, 0, 0, 0};
int dh[6] = {0, 0, 0, 0, 1, -1};
int l, r, c;

int main(void){
  ios::sync_with_stdio(0);
  cin.tie(0);
  while(1){
    cin >> l >> r >> c;
    if(l == 0 && r == 0 && c == 0) break;
    queue<tuple<int,int,int>> Q;
    char board[MX][MX][MX];
    int dist[MX][MX][MX];
    bool isEscape = false;

    for(int i = 0; i < l; i++)
      for(int j = 0; j < r; j++) fill(dist[i][j], dist[i][j] + c, 0);

    for(int i = 0; i < l; i++){
      for(int j = 0; j < r; j++){
        for(int k = 0; k < c; k++){
          cin >> board[i][j][k];
          if(board[i][j][k] == 'S'){
            Q.push({i,j,k});
            dist[i][j][k] = 0;
          }
          else if(board[i][j][k] == '.') dist[i][j][k] = -1;
        }
      }
    }

    while(!Q.empty()){
      if(isEscape) break;
      auto cur = Q.front(); Q.pop();
      int curH, curX, curY;
      tie(curH, curX, curY) = cur;
      for(int dir = 0; dir < 6; dir++){
        int nh = curH + dh[dir];
        int nx = curX + dx[dir];
        int ny = curY + dy[dir];
        if(nx < 0 || nx >= r || ny < 0 || ny >= c || nh < 0 || nh >= l) continue;
        if(board[nh][nx][ny] == '#' || dist[nh][nx][ny] > 0) continue;

        dist[nh][nx][ny] = dist[curH][curX][curY] + 1;
        if(board[nh][nx][ny] == 'E'){
          cout << "Escaped in " << dist[nh][nx][ny] << " minute(s).\n";
          isEscape = true;
          break;
        }
        Q.push({nh,nx,ny});
      }
    }
    if(!isEscape) cout << "Trapped!" << "\n";
  }
}

source code 출처 : https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x09/solutions/6593.cpp

 

basic-algo-lecture/0x09/solutions/6593.cpp at master · encrypted-def/basic-algo-lecture

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

여기서 const int를 이용한 이유를 chatGPT한테 물어봤다. 

1. 의미의 명확성이 증가한다. : 배열의 크기가 고정되어 변하지 않음을 알 수 있다. 

2. 코드의 유지보수성 향상 : 나중에 값을 바꿔야 한다면, 변숫값 하나만 바꿔주면 된다. 

3. 안전성 제공 : 임의로 변경하려는 것을 막을 수 있다.

4. 컴파일러의 최적화 가능성 : const로 선언된 값은 컴파일 시간에 상수로 평가되기 때문에, 컴파일러가 이를 활용해 더 효율적으로 최적화할 수 있다.