|
| 1 | +package week8.파이프옮기기; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | +public class BOJ_17070 { |
| 11 | + static int count = 0; |
| 12 | + static int N; |
| 13 | + static int[][] map; |
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + StringTokenizer st; |
| 17 | + |
| 18 | + N = Integer.parseInt(br.readLine()); |
| 19 | + map = new int[N+1][N+1]; |
| 20 | + for(int i=1; i<=N; i++){ |
| 21 | + st = new StringTokenizer(br.readLine()); |
| 22 | + for(int j=1; j<=N; j++){ |
| 23 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + count = 0; |
| 28 | + dfs(1, 2, 0, 1); |
| 29 | + |
| 30 | + System.out.println(count); |
| 31 | + } |
| 32 | + |
| 33 | + static void dfs(int row, int col, int rowDir, int colDir){ |
| 34 | + if(row==N && col==N){ |
| 35 | + count++; |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + List<int[]> nextDir = getNextDir(rowDir, colDir); |
| 40 | + |
| 41 | + for(int i=0; i<nextDir.size(); i++){ |
| 42 | + int nextRowDir = nextDir.get(i)[0]; |
| 43 | + int nextColDir = nextDir.get(i)[1]; |
| 44 | + int nextRow = row + nextRowDir; |
| 45 | + int nextCol = col + nextColDir; |
| 46 | + |
| 47 | + if(nextRow<1 || nextCol<1 || nextRow>N || nextCol>N)continue; |
| 48 | + if(isDiag(nextRowDir, nextColDir)){ |
| 49 | + if(map[nextRow][nextCol]==1)continue; |
| 50 | + if(map[nextRow-1][nextCol]==1)continue; |
| 51 | + if(map[nextRow][nextCol-1]==1)continue; |
| 52 | + } |
| 53 | + else{ |
| 54 | + if(map[nextRow][nextCol]==1)continue; |
| 55 | + } |
| 56 | + |
| 57 | + dfs(nextRow, nextCol, nextRowDir, nextColDir); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static boolean isDiag(int rowDir, int colDir){ |
| 62 | + return (rowDir!=0&&colDir!=0); |
| 63 | + } |
| 64 | + |
| 65 | + static List<int[]> getNextDir(int rowDir, int colDir){ |
| 66 | + List<int[]> nextDir = new ArrayList<>(); |
| 67 | + |
| 68 | + //대각선으로 움직이는 경우 |
| 69 | + if(rowDir!=0&&colDir!=0){ |
| 70 | + nextDir.add(new int[]{rowDir, colDir}); |
| 71 | + nextDir.add(new int[]{rowDir, 0}); |
| 72 | + nextDir.add(new int[]{0, colDir}); |
| 73 | + } |
| 74 | + //row 또는 col이 움직이는 경우 |
| 75 | + else { |
| 76 | + nextDir.add(new int[]{rowDir, colDir}); |
| 77 | + nextDir.add(new int[]{1, 1}); |
| 78 | + } |
| 79 | + |
| 80 | + return nextDir; |
| 81 | + } |
| 82 | +} |
0 commit comments