메뉴 건너뛰기

Programing

BAEKJOON

2636

관리자 2019.12.21 19:17 조회 수 : 130

C++

#include <cstdio>

#include <vector>

#include <queue>

 

using namespace std;

 

int cheese[110][110];

struct Pos{

    int x;

    int y;

};

 

int N,M;

vector<Pos> cheese_vec;

queue<Pos> q_air;

 

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

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

 

int is_safe(int x, int y)

{

    return (x < 0 || y < 0 || x >= M || y >= N) ? 0 : 1;

}

 

void check_cheese(int x, int y)

{

    for(int i = 0; i < 4; ++i)

    {

        int adj_y = y + dy[i];

        int adj_x = x + dx[i];

        if(!is_safe(adj_x, adj_y))

            continue;

        if(cheese[adj_y][adj_x] > 0)

        {

            cheese[adj_y][adj_x]++;

            if(cheese[adj_y][adj_x] == 2)

                cheese_vec.push_back({adj_x, adj_y});

        }

    }

}

 

void bfs_air()

{

    while(!q_air.empty())

    {

        auto p = q_air.front();

        q_air.pop();

        cheese[p.y][p.x] = -1;

        check_cheese(p.x, p.y);

 

        for(int i = 0; i < 4; ++i)

        {

            int adj_y = p.y + dy[i];

            int adj_x = p.x + dx[i];

            if(!is_safe(adj_x, adj_y))

                continue;

            if(cheese[adj_y][adj_x] == 0)

            {

                cheese[adj_y][adj_x] = -1;

                q_air.push({adj_x, adj_y});

            }

        }

    }

}

 

int main()

{

    scanf("%d %d",&N,&M);

    for(int y=0;y<N;y++){

        for(int x=0;x<M;x++){

            scanf("%d",&cheese[y][x]);

        }

    }

 

    int hour = 0;

    int ch_cnt = 0;

 

    q_air.push({0, 0});

 

    do

    {

        bfs_air();

 

        if(cheese_vec.empty())

            break;

 

        hour++;

        ch_cnt = cheese_vec.size();

        for(auto &p : cheese_vec)

            q_air.push({p.x, p.y});

 

        cheese_vec.clear();

 

    } while(1);

 

 

    printf("%d\n%d", hour, ch_cnt);

    return 0;

}

번호 제목 글쓴이 날짜 조회 수
공지 안내사항 관리자 2019.12.21 191
74 9663 관리자 2019.12.21 136
73 9345 관리자 2020.04.11 188
72 9019 관리자 2019.12.21 141
71 7569 관리자 2019.12.21 136
70 6987 관리자 2019.12.21 141
69 5842 관리자 2020.04.11 172
68 5625 관리자 2019.12.21 140
67 5419 관리자 2020.04.11 188
66 3392 관리자 2020.04.11 181
65 3019 관리자 2019.12.21 134
64 2933 관리자 2019.12.21 137
63 2931 관리자 2019.12.21 133
62 2836 관리자 2019.12.21 133
61 2667 관리자 2019.12.21 138
» 2636 관리자 2019.12.21 130
59 2629 관리자 2020.04.11 168
58 2615 관리자 2019.12.21 132
57 2610 관리자 2020.04.11 169
56 2606 관리자 2019.12.21 134
55 2585 관리자 2019.12.21 141
54 2578 관리자 2019.12.21 130
53 2573 관리자 2020.04.11 177
52 2557 관리자 2019.12.21 136
51 2512 관리자 2019.12.21 135
50 2504 관리자 2020.04.11 168
49 2481 관리자 2019.12.21 132
48 2478 관리자 2020.04.11 170
47 2473 관리자 2020.04.11 175
46 2470 관리자 2020.04.11 172
위로