섬나라 아일랜드(DFS)

Changwook Yang·2023년 2월 5일

알고리즘 연습

목록 보기
21/41

N*N 섬나라 아일랜드에서 몇개의 섬이 있는지 구하라
각 섬은 1로 표시되어있고 상하좌우와 대각선으로 연결되어 있고 0은 바다이다.

섬나라 아일랜드에 몇개의 섬이 있는지 구하라.

입력)
7
1 1 0 0 0 1 0
0 1 1 0 1 1 0
0 1 0 0 0 0 0
0 0 0 1 0 1 1
1 1 0 1 1 0 0
1 0 0 0 1 0 0
1 0 1 0 1 0 0

출력)
5

import java.util.Scanner;

public class Main {

    static int n;
    static int[][] arr;
    static int[][] check;
    static int[] moveX = {-1, -1, -1, 0,  0, 1, 1,  1};
    static int[] moveY = { 1,  0, -1, 1, -1, 1, 0, -1};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();

        arr = new int[n + 1][n + 1];
        check = new int[n + 1][n + 1];
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < n + 1; j++) {
                arr[i][j] = scanner.nextInt();
            }
        }

        int cnt = 0;
        for (int i = 1; i < n+1; i++) {
            for (int j = 1; j < n+1; j++) {
                cnt += DFS(i, j);
            }
        }

        System.out.println(cnt);
    }

    private static int DFS(int x, int y) {
        if (x == 0 || y == 0 || x == n+1 || y == n+1)
            return 0;
        else if (arr[x][y] == 0 || check[x][y] == 1)
            return 0;
        else {
            for (int i = 0; i < 8; i++) {
                check[x][y] = 1;
                DFS(x + moveX[i], y + moveY[i]);
            }
            return 1;
        }
    }
}
profile
멋있는 백엔드 개발자 / 꾸준히 의미있게!

0개의 댓글