-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1012.c
68 lines (55 loc) · 866 Bytes
/
1012.c
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
#include <stdio.h>
#include <string.h>
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int arr[50][50];
int M, N, K;
int count;
int isBound(int x, int y)
{
return x >= 0 && x < M && y >= 0 && y < N;
}
void search(int x, int y)
{
int i, j;
arr[x][y] = 0;
for(i=0; i<4; i++)
for(j=0; j<4; j++)
if( !(isBound(x+dx[i], y+dy[i])) )
continue;
else
if(arr[x+dx[i]][y+dy[i]])
search(x+dx[i], y+dy[i]);
}
void sol()
{
int i, j;
for(i=0; i<M; i++)
for(j=0; j<N; j++)
if(arr[i][j])
{
count++;
search(i, j);
}
printf("%d\n", count);
return;
}
int main(void)
{
int T, i, j;
int x, y;
scanf("%d", &T);
for(i=0; i<T; i++)
{
scanf("%d %d %d", &M, &N, &K);
for(j=0; j<K; j++)
{
scanf("%d %d", &x, &y);
arr[x][y] = 1;
}
count = 0;
sol();
memset(arr, 0, sizeof(arr));
}
return 0;
}