BOJ 7569, 토마토

전형적인 bfs문제. -> bfs를 돌면서 안 익은 토마토를 익게 만든다.

크게 주의할 부분은 없고, 전형적인 2차원문제가 아니라 3차원이라는 것만 다르다.
가로, 세로, 높이의 변수를 잘봤어야했다.

3차원이라는 좌표를 저장하기 위해 tuple을 사용했다.
c++ 11에 추가된 STL이므로 그 부분만 주의하며 사용하면 될 것 같다.

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

int n, m, h;
int board[101][101][101];
bool vis[101][101][101];
queue<tuple<int, int, int>> q;

int dx[] ={1, -1, 0, 0, 0, 0};
int dy[] ={0, 0, -1, 1, 0, 0};
int dz[] ={0, 0, 0, 0, 1, -1};

int ans;

void bfs(){
    while(!q.empty()){
        auto cur = q.front();
        q.pop();

        for(int dir = 0; dir < 6; dir++){
            int nx = get<1>(cur) + dy[dir]; //행
            int ny = get<2>(cur) + dz[dir]; //열
            int nz = get<0>(cur) + dx[dir]; //높이
            if(nx <0 || ny <0 || nz < 0 || nx >= m || ny >= n || nz >= h) continue;
            if(board[nz][nx][ny] == -1 || vis[nz][nx][ny]) continue;
            q.push({nz, nx, ny});
            board[nz][nx][ny] = board[get<0>(cur)][get<1>(cur)][get<2>(cur)] + 1;
            vis[nz][nx][ny] = 1;
        }
    }
}

int check(){
    int cnt =0;
    for(int k=0; k<h; k++){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(board[k][i][j] == 0) {return -1;}
                cnt = max(cnt, board[k][i][j]);
            } 
        }
    }
    return cnt-1;
}

int main(){
    cin>>n>>m>>h;
    for(int k=0; k<h; k++){ //높이
        for(int i=0; i<m; i++){ //행
            for(int j=0; j<n; j++){ //열
                cin>>board[k][i][j];
                if(board[k][i][j] == 1){
                    q.push({k, i, j});
                    vis[k][i][j] = 1;
                } 
            }
        }
    }

    bfs();
    
    if(check() == -1) cout<< -1;
    else cout<< check();
}

Categories:

Updated:

Leave a comment