문제
코드
단지번호 붙이기와 비슷하게 풀었다 아이고 속시원해
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
class Main{
static int[] dx = {0,0,-1,1};
static int[] dy = {-1,1,0,0};
public static void dfs(int[][] field, int x, int y, int m, int n) {
field[x][y]=0;
for(int i=0; i<4; i++) {
if(x+dx[i]<0 || x+dx[i]>=m || y+dy[i]<0 || y+dy[i]>=n )
continue;
else {
if(field[x+dx[i]][y+dy[i]]==1)
dfs(field,x+dx[i],y+dy[i],m,n);
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
int[] answer = new int[t];
for(int i=0; i<t; i++) {
String[] cond = br.readLine().split("\\s");
int m = Integer.parseInt(cond[0]);
int n = Integer.parseInt(cond[1]);
int cabbage = Integer.parseInt(cond[2]);
int[][] field = new int[m][n];
for(int j=0; j<cabbage; j++) {
String[] pos = br.readLine().split("\\s");
int x = Integer.parseInt(pos[0]);
int y = Integer.parseInt(pos[1]);
field[x][y] = 1;
}
int cnt = 0;
for(int x=0; x<m; x++) {
for(int y=0; y<n; y++) {
if(field[x][y]==1) {
cnt++;
dfs(field, x,y, m, n);
}
}
}
answer[i] = cnt;
}
for(int cnt : answer) {
bw.write(Integer.toString(cnt)+"\n");
}
bw.flush();
}
}
'Preparing Coding Test > Baekjoon' 카테고리의 다른 글
[Java/백준/BFS] 7576번, 7569번: 토마토 (0) | 2020.09.21 |
---|---|
[Java/백준/BFS] 2178번: 미로 탐색 (0) | 2020.09.21 |
[Java/백준/DFS와 BFS] 2667 - 단지번호붙이기 (0) | 2020.09.17 |
[Java/백준/DFS와 BFS] 2606 - 바이러스 (0) | 2020.09.12 |
[Java/백준/DFS와 BFS] 1260 - DFS와 BFS (0) | 2020.09.10 |