[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);
}
}
}
'๐ป Coding Problems Solving > DFS | BFS | Backtracking' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 1303] ์ ์-์ ํฌ (0) | 2022.07.04 |
---|---|
[BOJ 1260] DFS์ BFS (0) | 2022.07.01 |
[BOJ 2667] ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2022.06.29 |
[BOJ 14888] ์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ (0) | 2022.06.25 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํ๊ฒ ๋๋ฒ (0) | 2022.04.08 |
์ต๊ทผ๋๊ธ