[BOJ] 2468 안전 영역

Time Lapse :NONE

2468.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

#include <iostream>
#include <memory.h>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

int N;
int arr[100][100];
int high=0;
int ans = 1;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
bool visit[100][100];
bool digree[101];

void BFS(int rain)
{
memset(visit,false,sizeof(visit));
int cnt=0;
queue<pair<int,int>> q;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
if(!visit[i][j]&&arr[i][j]>rain)
{
q.push(make_pair(i,j));
visit[i][j]=true;
while(!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int k=0;k<4;k++)
{
int next_x = x+dx[k];
int next_y = y+dy[k];
if(next_x<0||next_x>=N||next_y<0||next_y>=N)
continue;
if(!visit[next_x][next_y]&&arr[next_x][next_y]>rain)
{
q.push(make_pair(next_x,next_y));
visit[next_x][next_y]=true;
}
}
}
cnt++;
}
}
ans = max(ans,cnt);
}

int main(void) {
cin>>N;
memset(digree, false,sizeof(digree));
for(int i=0;i<N;i++)
for(int j=0;j<N;j++) {
cin >> arr[i][j];
high = max(high,arr[i][j]);
digree[arr[i][j]]=true;
}

for(int i=1;i<high;i++)
if(digree[i])
BFS(i);

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