ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 7569 C++ 풀이
    카테고리 없음 2018. 11. 12. 00:40

    2차원으로 해도 충분할 걸 3차원으로 하다니 좀 의미없..


    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <math.h>
    #include <climits>
    #include <map>
    using namespace std;
    int ripenState[101][101][101];
    int m, n, h;
    int totalSpace;
    int totalUnripenTomato;
    int totalRipenTomato;
    int delta [6][3] = {{1, 0 ,0}, {-1 , 0 , 0}, {0, -1 , 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
    bool isInRange(int x, int y, int z){
    return 0<= x && x < m && 0<= y && y < n && 0<=z && z<h;
    }
    int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    cin >> m >> n >> h;
    totalSpace = m * n * h;
    queue< pair<int, pair<int, int>>> q;

    for (auto z=0; z<h; z++){
    for (auto y= 0; y<n; y++){
    for (auto x=0; x<m; x++){
    cin >> ripenState[x][y][z];
    if (ripenState[x][y][z] == 1 ){
    totalRipenTomato++;
    q.push(make_pair(x, make_pair(y, z)));
    }else if (ripenState[x][y][z] == 0){
    totalUnripenTomato++;
    }
    }
    }
    }
    if (totalUnripenTomato== 0){
    cout << 0;
    return 0;
    }
    int date = 0;
    while (!q.empty()){
    date++;
    int size = q.size();
    for (auto i=0; i<size; i++){
    pair<int, pair<int, int>> top = q.front();
    q.pop();
    int x = top.first;
    int y = top.second.first;
    int z = top.second.second;
    for (auto i=0; i<6; i++){
    int * deltas = delta[i];
    int next_x = x + deltas[0];
    int next_y = y + deltas[1];
    int next_z = z + deltas[2];
    if (isInRange(next_x,next_y, next_z)){
    if (ripenState[next_x][next_y][next_z] == 0){
    ripenState[next_x][next_y][next_z] = 1;
    totalUnripenTomato--;
    totalRipenTomato++;
    q.push(make_pair(next_x,make_pair(next_y,next_z)));
    }
    }
    }

    }
    }
    if (totalUnripenTomato == 0){
    cout << date-1;
    }else {
    cout << -1;
    }
    return 0;
    }


Designed by Tistory.