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
|
import java.util.LinkedList;
import java.util.Queue;
public class Main{
static int test;
static int N,M;
static int[][] map;
static int cnt;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0 , -1,1};
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
test = sc.nextInt();
for(int tc = 0 ; tc < test; tc++){
M = sc.nextInt();
N = sc.nextInt();
map = new int[N][M];
Queue<Integer> que = new LinkedList<Integer>();
cnt = sc.nextInt();
for(int i = 0 ; i<cnt; i++){
int y = sc.nextInt();
int x = sc.nextInt();
map[x][y] = 1;
}
for(int i = 0; i< map.length; i++){
for(int j = 0 ; j < map[i].length; j++){
if(map[i][j] > 0){
DFS(i,j);
if(cnt>0){
que.add(cnt);
cnt = 0;
}
}
}
}
}
}
private static void DFS(int x, int y){
if(map[x][y] != 1){
return;
}
cnt+=1;
map[x][y] = 2;
for(int i = 0 ; i< 4; i++){
int xx = x+dx[i];
int yy = y + dy[i];
if(xx>=0 && yy>=0 && xx<map.length && yy<map[0].length){
DFS(xx,yy);
}
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'it > 알고리즘' 카테고리의 다른 글
백준 1110 - 더하기 사이클 (0) | 2019.12.17 |
---|---|
백준 1058 - 친구 (0) | 2019.12.17 |
백준 10026 - 적록색약 (0) | 2019.09.01 |
백준 11057 - 오르막 수 (0) | 2019.09.01 |
백준 1309 - 동물원 (0) | 2019.09.01 |