-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJoon_1926.cpp
More file actions
76 lines (69 loc) · 1.09 KB
/
BaekJoon_1926.cpp
File metadata and controls
76 lines (69 loc) · 1.09 KB
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N, M;
int arr[501][501] = {};
bool visited[501][501] = {};
// 위, 아래, 오른쪽, 왼쪽
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,1,-1 };
int pictureCnt = 0;
int cnt = 0;
int mx = 0;
queue<pair<int, int>> mq;
void BFS()
{
while (!mq.empty())
{
int x = mq.front().first;
int y = mq.front().second;
mq.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >=0 && nx<N && ny>=0 && ny <M && arr[nx][ny] == 1 )
{
if (visited[nx][ny] != true)
{
mq.push({ nx,ny });
cnt++;
visited[nx][ny] = true;
}
}
}
}
}
int main()
{
cin >> N >> M;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (arr[i][j] == 1 && visited[i][j] == false)
{
mq.push({ i,j });
visited[i][j] = true;
cnt++;
BFS();
pictureCnt++;
if (cnt > mx)
{
mx = cnt;
}
cnt = 0;
}
}
}
cout << pictureCnt << "\n";
cout << mx;
}