[BOJ] 2234 성곽

Time Lapse :15min 33sec

2234.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
#include <stdio.h>
#include <queue>
using namespace std;
int map[50][50];
int map_convert[50][50];
int mapsize[2501], nums = 1, maxsize, extensionsize;
bool visit[50][50];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
int N, M;
void BFS(){
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j){
if(map_convert[i][j]) continue;
queue<pair<int, int>> q;
map_convert[i][j] = nums;
++mapsize[nums];
q.emplace(i,j);
while(!q.empty()){
int x = q.front().second;
int y = q.front().first;
q.pop();
for(int k = 0; k < 4; ++k){
if(map[y][x]&(1<<k)) continue;
int nx = x + dx[k];
int ny = y + dy[k];
if(0<=nx&&nx<M && 0<=ny&&ny<N && !map_convert[ny][nx]){
map_convert[ny][nx] = nums;
++mapsize[nums];
q.emplace(ny,nx);
}
}
}
maxsize = max(maxsize,mapsize[nums++]);
}

for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j){
if(visit[i][j]) continue;
queue<pair<int,int>> q;
visit[i][j] = true;
q.emplace(i,j);
while(!q.empty()){
int y = q.front().first;
int x = q.front().second;
q.pop();
for(int k = 0; k < 4; ++k){
int nx = x + dx[k];
int ny = y + dy[k];
if(0<=nx&&nx<M && 0<=ny&&ny<N){
if(map_convert[ny][nx]==map_convert[i][j] && !visit[ny][nx]){
visit[ny][nx] = true;
q.emplace(ny,nx);
}
else if(map_convert[ny][nx]!=map_convert[i][j])
extensionsize = max(extensionsize,mapsize[map_convert[i][j]]+mapsize[map_convert[ny][nx]]);
}
}
}
}
printf("%d\n%d\n%d",nums-1,maxsize,extensionsize);
}
int main(){
scanf("%d %d",&M,&N);
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
scanf("%d",&map[i][j]);
BFS();
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2234/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.