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;
}
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
공지 | 안내사항 | 관리자 | 2019.12.21 | 163 |
74 | 9663 | 관리자 | 2019.12.21 | 114 |
73 | 9345 | 관리자 | 2020.04.11 | 150 |
72 | 9019 | 관리자 | 2019.12.21 | 113 |
71 | 7569 | 관리자 | 2019.12.21 | 113 |
70 | 6987 | 관리자 | 2019.12.21 | 113 |
69 | 5842 | 관리자 | 2020.04.11 | 151 |
68 | 5625 | 관리자 | 2019.12.21 | 109 |
67 | 5419 | 관리자 | 2020.04.11 | 155 |
66 | 3392 | 관리자 | 2020.04.11 | 156 |
65 | 3019 | 관리자 | 2019.12.21 | 111 |
64 | 2933 | 관리자 | 2019.12.21 | 112 |
63 | 2931 | 관리자 | 2019.12.21 | 111 |
62 | 2836 | 관리자 | 2019.12.21 | 110 |
61 | 2667 | 관리자 | 2019.12.21 | 113 |
» | 2636 | 관리자 | 2019.12.21 | 113 |
59 | 2629 | 관리자 | 2020.04.11 | 146 |
58 | 2615 | 관리자 | 2019.12.21 | 111 |
57 | 2610 | 관리자 | 2020.04.11 | 146 |
56 | 2606 | 관리자 | 2019.12.21 | 111 |
55 | 2585 | 관리자 | 2019.12.21 | 113 |
54 | 2578 | 관리자 | 2019.12.21 | 111 |
53 | 2573 | 관리자 | 2020.04.11 | 151 |
52 | 2557 | 관리자 | 2019.12.21 | 111 |
51 | 2512 | 관리자 | 2019.12.21 | 111 |
50 | 2504 | 관리자 | 2020.04.11 | 147 |
49 | 2481 | 관리자 | 2019.12.21 | 112 |
48 | 2478 | 관리자 | 2020.04.11 | 148 |
47 | 2473 | 관리자 | 2020.04.11 | 153 |
46 | 2470 | 관리자 | 2020.04.11 | 151 |