-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1926.cpp
65 lines (49 loc) · 989 Bytes
/
1926.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
#include <iostream>
#include <algorithm>
using namespace std;
bool visited[1000][1000];
int map[1000][1000];
int N, M;
int dfs(int x, int y)
{
visited[x][y] = true;
int result = 1;
int xxxx[] = {-1, 0, 1, 0};
int yyyy[] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++)
{
int nextX = x + xxxx[i];
int nextY = y + yyyy[i];
if (nextX < 0 || nextX >= N || nextY < 0 || nextY >= M || visited[nextX][nextY] || map[nextX][nextY] == 0)
continue;
result += dfs(nextX, nextY);
}
return result;
}
int main()
{
cin >> N >> M;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
visited[i][j] = false;
cin >> map[i][j];
}
}
int count = 0;
int maximum = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (visited[i][j] || !map[i][j])
continue;
count++;
maximum = max(maximum, dfs(i, j));
}
}
cout << count << "\n"
<< maximum;
return 0;
}