-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1012.cpp
89 lines (67 loc) · 1.64 KB
/
1012.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
//1012 유기농 배추
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
int arr[50][50];
bool vis[50][50];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int init(){
for(int i =0; i<50; i++)
{
for(int j =0; j<50; j++)
{
arr[i][j] = 0;
vis[i][j]= 0;
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 0;
cin >> tc;
for(int i = 0; i<tc; i++)
{
int num;
int n, m;
int ji = 0;
init();
cin >> n >> m >> num;
for(int j =0 ;j<num; j++)
{
int x,y;
cin >> x >> y;
arr[x][y] = 1;
}
//bfs
for(int o = 0; o < n; o++)
{
for(int p = 0; p < m; p++)
{
if(vis[o][p] == 1|| arr[o][p] == 0) continue;
queue<pair<int,int>> q;
vis[o][p] = 1;
q.push(make_pair(o,p));
ji++;
while(!q.empty())
{
auto cur = q.front();
q.pop();
for(int k = 0; k<4; k++)
{
int nx = cur.first + dx[k];
int ny = cur.second + dy[k];
if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if(vis[nx][ny] == 1 || arr[nx][ny] == 0) continue;
vis[nx][ny] = 1;
q.push(make_pair(nx,ny));
}
}
}
}
cout << ji << '\n';
}
return 0;
}