728x90
반응형
문제
제출한 답안
package BOJ;
import java.util.Scanner;
public class BOJ_4963 {
private static String[][] map;
private static boolean[][] isVisited;
private static final int[] dx = {1,-1,0,0,1,-1,-1,1};
private static final int[] dy = {0,0,1,-1,1,-1,1,-1};
private static int W;
private static int H;
private static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
String[] wh = sc.nextLine().split(" ");
W = Integer.parseInt(wh[0]);
H = Integer.parseInt(wh[1]);
if (W == 0 && H == 0) break;
map = new String[H][W];
isVisited = new boolean[H][W];
for (int i = 0; i < H; i++) {
String[] split = sc.nextLine().split(" ");
for (int j = 0; j < split.length; j++) {
map[i][j] = split[j];
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (!isVisited[i][j] && map[i][j].equals("1")){
dfs(i,j);
count++;
}
}
}
System.out.println(count);
count = 0;
}
}
private static void dfs(int x, int y){
isVisited[x][y] = true;
for (int i = 0; i < dx.length; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx >= H || ny >= W || isVisited[nx][ny] || !map[nx][ny].equals("1")) continue;
dfs(nx,ny);
}
}
}
728x90
반응형
'Algorithm' 카테고리의 다른 글
[BOJ 1987] 알파벳 ( with Java ) (0) | 2023.04.17 |
---|---|
[BOJ 2468] 안전 영역 ( with Java ) (0) | 2023.04.16 |
[BOJ 10026] 적록색약 ( with Java ) (0) | 2023.04.16 |
[BOJ 11724] 연결 요소의 개수 ( with Java ) (0) | 2023.04.16 |
[BOJ 1012] 유기농 배추 ( with Java ) (0) | 2023.04.16 |