1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package Samsung.done;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer;
public class 사다리조작_15684번 { static int n, m, h; static int[][] map; static int answer; static boolean isCheck; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); h = Integer.parseInt(st.nextToken()); map = new int[h+1][n+1]; answer = 0;
int x,y; for(int i=0; i<m; i++){ st = new StringTokenizer(br.readLine()); x = Integer.parseInt(st.nextToken()); y = Integer.parseInt(st.nextToken()); map[x][y] = 1; map[x][y+1] = 2; } for(int i=0; i<= 3; i++){ answer = i; dfs(1, 0, i); if(isCheck) break; } System.out.println(isCheck ? answer : -1); }
private static void dfs(int x, int cnt, int len) { if(isCheck) return; if(cnt == len){ if (arriveAtDestination()) { isCheck = true; } return; } for(int i=x; i<h+1; i++){ for(int j=1; j<n; j++){ if(map[i][j] == 0 && map[i][j+1] == 0){ map[i][j] = 1; map[i][j+1] = 2; dfs(i, cnt+1, len); map[i][j] = 0; map[i][j+1] = 0; } } } }
private static boolean arriveAtDestination() { for(int i=1; i<=n; i++){ int mx = 1; int my = i; for(int j=0; j<h; j++){ if(map[mx][my] == 1){ my++; }else if(map[mx][my] == 2){ my--; } mx++; } if(my != i){ return false; } } return true; } }
|