|
| 1 | +// |
| 2 | +// Created by 융미 on 2019-04-23. |
| 3 | +//2573 빙산 |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <queue> |
| 7 | +#include <utility> |
| 8 | +#include <cstring> |
| 9 | +#include <algorithm> |
| 10 | +using namespace std; |
| 11 | +#define MAX 300 |
| 12 | +int dx[4] = {-1,1,0,0}; |
| 13 | +int dy[4] = {0,0,1,-1}; |
| 14 | + |
| 15 | +int n, m; |
| 16 | +int age; |
| 17 | +int arr[MAX][MAX]; |
| 18 | +bool vis[MAX][MAX]; |
| 19 | + |
| 20 | +void getInput(); //입력 함수 |
| 21 | +void getAge(); //시간 지나는 함수 |
| 22 | +int check(); //덩어리 체크 함수 |
| 23 | +void printOutput(); //결과 출력 함수 |
| 24 | + |
| 25 | +int main(){ |
| 26 | + ios::sync_with_stdio(0); |
| 27 | + cin.tie(0); |
| 28 | + |
| 29 | + getInput(); |
| 30 | + printOutput(); |
| 31 | + |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +void getInput() |
| 36 | +{ |
| 37 | + cin >> n >> m; |
| 38 | + for(int i = 0; i<n; i++) |
| 39 | + { |
| 40 | + for(int j = 0; j<m; j++) |
| 41 | + cin >> arr[i][j]; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +void printOutput() |
| 46 | +{ |
| 47 | + int result = 1; |
| 48 | + while(true) |
| 49 | + { |
| 50 | + age++; |
| 51 | + getAge(); |
| 52 | + result = check(); |
| 53 | + if(result >= 2) {cout << age; break;} |
| 54 | + if(result == 0) {cout << 0; break;} |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void getAge(){ |
| 59 | + int tempArr[MAX][MAX]; |
| 60 | + memset(tempArr,0,sizeof(tempArr)); |
| 61 | + |
| 62 | + for(int i = 1; i<n-1; i++) |
| 63 | + { |
| 64 | + for(int j = 1; j<m-1; j++) |
| 65 | + { |
| 66 | + if(arr[i][j] == 0) continue; |
| 67 | + int count = 0; |
| 68 | + for(int k = 0; k<4; k++){ |
| 69 | + int nx = i + dx[k]; |
| 70 | + int ny = j + dy[k]; |
| 71 | + if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue; |
| 72 | + if(arr[nx][ny] != 0) continue; |
| 73 | + count++; |
| 74 | + } |
| 75 | + tempArr[i][j] = arr[i][j] - count; |
| 76 | + if(tempArr[i][j] < 0) tempArr[i][j] = 0; //0 보다 작을 수 없다. 이조건 때문에 2번 틀림.. |
| 77 | + }; |
| 78 | + } |
| 79 | + memcpy(arr,tempArr, sizeof(arr)); |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +int check(){ |
| 84 | + memset(vis, 0, sizeof(vis)); |
| 85 | + |
| 86 | + int count = 0; |
| 87 | + for(int i = 1; i<n-1; i++) |
| 88 | + { |
| 89 | + for(int j = 1; j<m-1; j++) |
| 90 | + { |
| 91 | + if(arr[i][j] == 0 || vis[i][j] == 1) continue; |
| 92 | + |
| 93 | + queue<pair<int,int>> q; |
| 94 | + |
| 95 | + q.push({i,j}); |
| 96 | + vis[i][j] = 1; |
| 97 | + |
| 98 | + while(!q.empty()) |
| 99 | + { |
| 100 | + auto cur = q.front(); |
| 101 | + q.pop(); |
| 102 | + |
| 103 | + for(int k = 0; k<4; k++) |
| 104 | + { |
| 105 | + int nx = cur.first + dx[k]; |
| 106 | + int ny = cur.second + dy[k]; |
| 107 | + |
| 108 | + if(nx < 1 || nx >= n-1 || ny < 1 || ny >= m-1) continue; |
| 109 | + if(arr[nx][ny] == 0 || vis[nx][ny] == 1) continue; |
| 110 | + |
| 111 | + q.push({nx,ny}); |
| 112 | + vis[nx][ny] = 1; |
| 113 | + } |
| 114 | + } |
| 115 | + count++; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return count; |
| 120 | +} |
| 121 | + |
0 commit comments