[BOJ 17070] ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ1

 

1. ๋ฌธ์ œ : https://www.acmicpc.net/problem/17070

 

2. ํ’€์ด :

dfs๋ฅผ ์ด์šฉํ•œ ํ’€์ด

๋น„๊ต์  ์‰ฌ์› ๋‹ค

 

3. ์ฝ”๋“œ

import java.util.*;

class Main {
    static int N;
    static int[][] map;
    static int answer = 0;

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

        N = sc.nextInt();

        map = new int[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                map[i][j] = sc.nextInt();
            }
        }

        dfs(0, 1, 1);
        System.out.println(answer);
    }

    public static void dfs(int x, int y, int i) {

        if (x == N - 1 && y == N - 1) {
            answer++;
            return;
        }
        if (i == 1) {
            // ๊ฐ€๋กœ
            if (y + 1 < N && map[x][y + 1] == 0) dfs(x, y + 1, 1);
            if (x + 1 < N && y + 1 < N && map[x][y + 1] == 0 && map[x + 1][y + 1] == 0 && map[x + 1][y] == 0)
                dfs(x + 1, y + 1, 2);
        } else if (i == 2) {
            // ๋Œ€๊ฐ์„ 
            if (y + 1 < N && map[x][y + 1] == 0) dfs(x, y + 1, 1);
            if (x + 1 < N && y + 1 < N && map[x][y + 1] == 0 && map[x + 1][y + 1] == 0 && map[x + 1][y] == 0)
                dfs(x + 1, y + 1, 2);
            if (x + 1 < N && map[x + 1][y] == 0) dfs(x + 1, y, 3);
        } else if (i == 3) {
            // ์„ธ๋กœ
            if (x + 1 < N && y + 1 < N && map[x][y + 1] == 0 && map[x + 1][y + 1] == 0 && map[x + 1][y] == 0)
                dfs(x + 1, y + 1, 2);
            if (x + 1 < N && map[x + 1][y] == 0) dfs(x + 1, y, 3);
        }
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ