|
| 1 | +package week06.BOJ_17070_파이프옮기기1; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.lang.*; |
| 5 | +import java.io.*; |
| 6 | + |
| 7 | +class BOJ17070 { |
| 8 | + static final int WALL = 1; |
| 9 | + static int answer = 0; |
| 10 | + static int N; |
| 11 | + static int[][] house; |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + N = Integer.parseInt(br.readLine()); |
| 15 | + house = new int[N][N]; |
| 16 | + for (int i = 0; i < N; i++) { |
| 17 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 18 | + for (int j = 0; j < N; j++) { |
| 19 | + house[i][j] = Integer.parseInt(st.nextToken()); |
| 20 | + } |
| 21 | + } |
| 22 | + move(0, 1, 'H'); //0,1에서 시작 |
| 23 | + System.out.println(answer); |
| 24 | + } |
| 25 | + |
| 26 | + static void move(int x, int y, char dir) { |
| 27 | + //각 dir에 따라 갈 방법이 없으면 return |
| 28 | + if (dir == 'H' || dir == 'V') { |
| 29 | + if (!isPossible(x, y) || house[x][y] == WALL) { |
| 30 | + return; |
| 31 | + } |
| 32 | + } else { |
| 33 | + if (!isPossible(x, y) || !isPossible(x-1, y) || !isPossible(x, y-1) || |
| 34 | + house[x][y] == WALL || house[x-1][y] == WALL || house[x][y-1] == WALL) { |
| 35 | + return; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if (x == N - 1 && y == N - 1) { //도착 |
| 40 | + answer++; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (dir == 'H') { //가로 이동 |
| 45 | + move(x, y+1, 'H'); //1번 |
| 46 | + move(x+1, y+1, 'D'); //2번 |
| 47 | + |
| 48 | + } else if (dir == 'V') { //세로 이동 |
| 49 | + move(x+1, y, 'V'); //1번 |
| 50 | + move(x+1, y+1, 'D'); //2번 |
| 51 | + |
| 52 | + } else { //대각선 이동 |
| 53 | + move(x, y+1, 'H'); //1번 |
| 54 | + move(x+1, y, 'V'); //2번 |
| 55 | + move(x+1, y+1, 'D'); //3번 |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + static boolean isPossible(int x, int y) { |
| 61 | + return x >= 0 && x < N && y >= 0 && y < N; |
| 62 | + } |
| 63 | +} |
0 commit comments