|
| 1 | +//https://www.acmicpc.net/problem/2636 |
| 2 | +#include <iostream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <queue> |
| 6 | +#include <cstring> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +/* |
| 10 | + 치즈가 모두 녹아 없어지는 시간 |
| 11 | + 모두 녹기 한시간 전 남아있는 개수 |
| 12 | +*/ |
| 13 | + |
| 14 | +struct Node { |
| 15 | + int x, y; |
| 16 | +}; |
| 17 | + |
| 18 | +int N, M; |
| 19 | +int arr[101][101]; |
| 20 | +bool visited[101][101]; |
| 21 | +const int dx[4] = {-1, 0, 1, 0}; |
| 22 | +const int dy[4] = {0, 1, 0, -1}; |
| 23 | + |
| 24 | +bool OOB(int x, int y) { |
| 25 | + return x < 0 || x >= N || y < 0 || y >= M; |
| 26 | +} |
| 27 | + |
| 28 | +void bfs(int x, int y) { |
| 29 | + queue<Node> q; |
| 30 | + q.push({x, y}); |
| 31 | + visited[x][y] = true; |
| 32 | + |
| 33 | + while(!q.empty()) { |
| 34 | + Node now = q.front(); |
| 35 | + q.pop(); |
| 36 | + |
| 37 | + for(int dir = 0; dir < 4; dir++){ |
| 38 | + int nx = now.x + dx[dir]; |
| 39 | + int ny = now.y + dy[dir]; |
| 40 | + |
| 41 | + if(OOB(nx, ny) || visited[nx][ny]) continue; |
| 42 | + visited[nx][ny] = true; |
| 43 | + // 0일 때 넣기 |
| 44 | + if (arr[nx][ny] == 1) { |
| 45 | + arr[nx][ny] = 0; |
| 46 | + } else q.push({nx, ny}); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +int oneCnt(){ |
| 52 | + int cnt = 0; |
| 53 | + for(int i = 0; i < N; i++){ |
| 54 | + for(int j = 0; j < M; j++){ |
| 55 | + if (arr[i][j] == 1) cnt++; |
| 56 | + } |
| 57 | + } |
| 58 | + return cnt; |
| 59 | +} |
| 60 | + |
| 61 | +int main(){ |
| 62 | + ios_base::sync_with_stdio(false); |
| 63 | + cin.tie(NULL); cout.tie(NULL); |
| 64 | + |
| 65 | + cin >> N >> M; |
| 66 | + for(int i = 0; i < N; i++){ |
| 67 | + for(int j = 0; j < M; j++){ |
| 68 | + cin >> arr[i][j]; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + int last = 0; |
| 73 | + int time = 0; |
| 74 | + while(true) { |
| 75 | + memset(visited, false, sizeof(visited)); |
| 76 | + |
| 77 | + int cnt = oneCnt(); |
| 78 | + // 종료조건 확인 |
| 79 | + if (cnt == 0) break; |
| 80 | + |
| 81 | + time++; |
| 82 | + bfs(0, 0); |
| 83 | + last = cnt; |
| 84 | + } |
| 85 | + |
| 86 | + cout << time << "\n" << last; |
| 87 | + return 0; |
| 88 | +} |
0 commit comments