[BOJ] 2573 빙산

Time Lapse :NONE

2573.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <memory.h>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

bool noans;
int N,M;
int arr[300][300];
int arr_cpy[300][300];
bool visit[300][300];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

void BFS(int x, int y)
{
memcpy(arr_cpy,arr,sizeof(arr_cpy));

queue<vector<int>> q;
vector<int> start;
start.push_back(x);
start.push_back(y);
visit[x][y]=true;
int start_de=0;
for(int i=0;i<4;i++)
{
int new_x = x + dx[i];
int new_y = y + dy[i];

if(new_x<0||new_y<0||new_x>=N||new_y>=M)
continue;

if(arr_cpy[new_x][new_y]==0)
start_de++;
}
arr[x][y]-=start_de;
if(arr[x][y]<0)
arr[x][y]=0;

q.push(start);
while(!q.empty())
{
int cur_x = q.front()[0];
int cur_y = q.front()[1];
q.pop();

for(int i=0;i<4;i++)
{
int next_x = cur_x + dx[i];
int next_y = cur_y + dy[i];
if(next_x<0||next_y<0||next_x>=N||next_y>=M)
continue;
if(!visit[next_x][next_y]&&arr_cpy[next_x][next_y]!=0)
{
vector<int> item;
item.push_back(next_x);
item.push_back(next_y);
q.push(item);
visit[next_x][next_y]=true;
int decrease = 0;
for(int j=0;j<4;j++)
{
int new_x = next_x + dx[j];
int new_y = next_y + dy[j];

if(new_x<0||new_y<0||new_x>=N||new_y>=M)
continue;

if(arr_cpy[new_x][new_y]==0)
decrease++;
}
arr[next_x][next_y]-=decrease;
if(arr[next_x][next_y]<0)
arr[next_x][next_y]=0;
}
}
}
}


int main(void) {
cin>>N>>M;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
cin>>arr[i][j];
int ans=0;
while(1) {
bool devide = false;

memset(visit,false,sizeof(visit));

for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
if(arr[i][j]!=0&&!visit[i][j])
{
if(devide)
{
cout<<ans-1<<endl;
return 0;
}
visit[i][j]=true;
devide = true;
BFS(i,j);
ans++;
}
}


int total = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
total += arr[i][j];
}

if(total==0) {
cout << "0" << endl;
return 0;
}
}

}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2573/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.