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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| package airbnb; import java.util.Random;
public class TileBoard { int[][] board; int[][] visited; int x; int y; public TileBoard() { board = new int[3][3]; int start = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (start >= 9) { break; } board[i][j] = start++; } } x = 2; y = 2; visited = new int[3][3]; } public void shuffle() { Random random = new Random(); int moves = random.nextInt(5); while (moves > 0) { int dir = random.nextInt(4); try { slide(Direction.values()[dir], 0, false); moves--; } catch (RuntimeException e) { } } System.out.println("finished shuffle"); } public boolean sovle() { return solveHelper(1, 0); } public boolean solveHelper(int level, int moves) { if (x == 2 && y == 2) { if (moves == 1) { System.out.println("back to 2,2"); } return finalCheck(); } else if (moves >= 8) { return false; } else { int oldX = x; int oldY = y; for (Direction dir : Direction.values()) { visited[oldX][oldY] = 1; try { slide(dir, level, false); System.out.println(toString()); } catch (RuntimeException e) { continue; } if (solveHelper(level + 1, moves + 1)) { return true; } else { try { Direction back = reverse(dir); System.out.println("reversing " + back.toString()); slide(back, level, true); System.out.println(toString()); } catch (RuntimeException e) { } } } return false; } } public Direction reverse(Direction dir) { switch (dir) { case Up: return Direction.down; case down: return Direction.Up; case left: return Direction.right; case right: return Direction.left; } return null; } public boolean finalCheck() { int start = 1; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (start > 8) { break; } if (board[i][j] != start++) { return false; } } } return true; } public void slide(Direction direction, int level, boolean reverse) { int newY = y; int newX = x; switch (direction) { case Up: newX = x - 1; if (newX < 0) { throw new RuntimeException(); } break; case down: newX = x + 1; if (newX > 2) { throw new RuntimeException(); } break; case left: newY = y - 1; if (newY < 0) { throw new RuntimeException(); } break; case right: newY = y + 1; if (newY > 2) { throw new RuntimeException(); } break; } if (reverse) { visited[newX][newY] = 0; } if (visited[newX][newY] == 0) { int target = board[newX][newY]; board[x][y] = target; board[newX][newY] = 0; x = newX; y = newY; System.out.println(level + " slide " + direction.toString() + " to x " + x + " y " + y); } else { throw new RuntimeException(); } } public enum Direction { Up, down, left, right } public static void main(String[] args) { TileBoard interview = new TileBoard();
System.out.println(interview.toString());
interview.shuffle(); System.out.println(interview.toString()); interview.sovle(); System.out.println(interview.toString()); System.out.println(interview.finalCheck()); } @Override public String toString() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 3; i++) { if (i > 0) { sb.append("\n"); } for (int j = 0; j < 3; j++) { sb.append(board[i][j] + " "); } } return sb.toString(); } }
|