baekjoon 7569 본문

참고/아무거나

baekjoon 7569

뚜스머리 2022. 5. 27. 20:24
#include <stdio.h>
#include <queue>

#define MAX_SIZE 100
char Boxes[MAX_SIZE][MAX_SIZE][MAX_SIZE] = {0,};
int N,M,H = 0;

struct box
{
    char r;
    char c;
    char h;
};
std::queue<box> mBox;

int dr[6] = {1, 0, -1, 0, 0, 0};
int dc[6] = {0,-1,0,1,0,0};
int dh[6] = {0,0,0,0,1,-1};

void check(int r,int c,int h)
{
    for(int i=0; i<6 ; i++)
    {
        int nh = h + dh[i];
        int nc = c + dc[i];
        int nr = r + dr[i];

        if(nh < 0 || nc < 0 || nr < 0) continue;
        if(nh >= H || nc >= M || nr >= N) continue;

        if(Boxes[nh][nr][nc] == 0) {
            box in = {(char)nr,(char)nc,(char)nh};
            mBox.push(in);
            Boxes[nh][nr][nc]++;
        }
    }
}

int main()
{
    scanf("%d %d %d", &M, &N, &H);
    for(int h_=0; h_<H; h_++)
    {
        for(int row=0; row<N; row++)
        {
            for(int col=0; col<M; col++)
            {
                int buf;
                scanf("%d", &buf);
                Boxes[h_][row][col] = (char)buf;
                if(buf > 0)
                {
                    box b = {(char)row, (char)col, (char)h_};
                    mBox.push(b);
                }
            }
        }
    }
    int days = 0;
    while(true) {
        if (mBox.empty()) break;
        int count = mBox.size();
        for(int i=0; i<count; i++)
        {
            box outBox;
            outBox = mBox.front();
            mBox.pop();

            check(outBox.r, outBox.c, outBox.h);
        }
        days ++;
    }

    bool notFinish = false;
    for(int hei=0; hei<H; hei++) {
        for (int row = 0; row < N; row++) {
            for(int col =0; col< M ; col++)
            {
                if(Boxes[hei][row][col] == 0)
                    notFinish = true;
            }

        }
    }

    if(notFinish) printf("-1\n");
    else          printf("%d\n", --days);

    return 0;
}

'참고 > 아무거나' 카테고리의 다른 글

baekjoon 2667  (0) 2022.05.27
테스트 샘플  (0) 2017.07.26
테스트  (0) 2017.07.25
티스토리 초대장  (43) 2017.06.26
초대장  (6) 2017.06.18