|
| 1 | +package LeetCodeJava.BFS; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/minimum-knight-moves/description/ |
| 4 | +// https://leetcode.ca/all/1197.html |
| 5 | + |
| 6 | +import java.util.ArrayDeque; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.LinkedList; |
| 9 | +import java.util.Queue; |
| 10 | + |
| 11 | +/** |
| 12 | + * 1197. Minimum Knight Moves |
| 13 | + * In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0]. |
| 14 | + * <p> |
| 15 | + * A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. |
| 16 | + * <p> |
| 17 | + * <p> |
| 18 | + * <p> |
| 19 | + * Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists. |
| 20 | + * <p> |
| 21 | + * <p> |
| 22 | + * <p> |
| 23 | + * Example 1: |
| 24 | + * <p> |
| 25 | + * Input: x = 2, y = 1 |
| 26 | + * Output: 1 |
| 27 | + * Explanation: [0, 0] → [2, 1] |
| 28 | + * Example 2: |
| 29 | + * <p> |
| 30 | + * Input: x = 5, y = 5 |
| 31 | + * Output: 4 |
| 32 | + * Explanation: [0, 0] → [2, 1] → [4, 2] → [3, 4] → [5, 5] |
| 33 | + * <p> |
| 34 | + * <p> |
| 35 | + * Constraints: |
| 36 | + * <p> |
| 37 | + * |x| + |y| <= 300 |
| 38 | + * Difficulty: |
| 39 | + * Medium |
| 40 | + * Lock: |
| 41 | + * Prime |
| 42 | + * Company: |
| 43 | + * Amazon Facebook Google Oracle |
| 44 | + * Problem Solution |
| 45 | + * 1197-Minimum-Knight-Moves |
| 46 | + */ |
| 47 | +public class MinimumKnightMoves { |
| 48 | + |
| 49 | + // V0 |
| 50 | + // IDEA : BFS |
| 51 | + // TODO : fix and validate |
| 52 | +// public int minKnightMoves(int x, int y) { |
| 53 | +// |
| 54 | +// if (x==0 && y==0){ |
| 55 | +// return 0; |
| 56 | +// } |
| 57 | +// |
| 58 | +// // init |
| 59 | +// int[][] moves = new int[][]{ {1,2}, {2,1}, {2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1,2} }; |
| 60 | +// // queue : FIFO |
| 61 | +// Queue<List<Integer>> q = new LinkedList<>(); // Queue([x, y, step]) |
| 62 | +// List<Integer> tmp = new ArrayList<>(); |
| 63 | +// tmp.add(0); // x |
| 64 | +// tmp.add(0); // y |
| 65 | +// tmp.add(0); // step |
| 66 | +// q.add(tmp); |
| 67 | +// |
| 68 | +// while(!q.isEmpty()){ |
| 69 | +// List<Integer> cur = q.poll(); |
| 70 | +// int cur_x = cur.get(0); |
| 71 | +// int cur_y = cur.get(1); |
| 72 | +// int cur_step = cur.get(2); |
| 73 | +// if (cur_x == x && cur_y == y){ |
| 74 | +// return cur_step; |
| 75 | +// } |
| 76 | +// for (int[] move : moves){ |
| 77 | +// List<Integer> newCoor = new ArrayList<>(); |
| 78 | +// newCoor.add(cur_x + move[0]); |
| 79 | +// newCoor.add(cur_y + move[1]); |
| 80 | +// newCoor.add(cur_step + 1); |
| 81 | +// q.add(newCoor); |
| 82 | +// } |
| 83 | +// } |
| 84 | +// |
| 85 | +// return -1; |
| 86 | +// } |
| 87 | + |
| 88 | + // V1 |
| 89 | + // IDEA : BFS |
| 90 | + // https://leetcode.ca/2019-03-11-1197-Minimum-Knight-Moves/ |
| 91 | + public int minKnightMoves_1(int x, int y) { |
| 92 | + x += 310; |
| 93 | + y += 310; |
| 94 | + int ans = 0; |
| 95 | + Queue<int[]> q = new ArrayDeque<>(); |
| 96 | + q.offer(new int[]{310, 310}); |
| 97 | + // NOTE !!! use vis (boolean 2D array) to AVOID visit same coordination again |
| 98 | + boolean[][] vis = new boolean[700][700]; |
| 99 | + vis[310][310] = true; |
| 100 | + int[][] dirs = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; |
| 101 | + while (!q.isEmpty()) { |
| 102 | + for (int k = q.size(); k > 0; --k) { |
| 103 | + int[] p = q.poll(); |
| 104 | + if (p[0] == x && p[1] == y) { |
| 105 | + return ans; |
| 106 | + } |
| 107 | + for (int[] dir : dirs) { |
| 108 | + int c = p[0] + dir[0]; |
| 109 | + int d = p[1] + dir[1]; |
| 110 | + if (!vis[c][d]) { |
| 111 | + vis[c][d] = true; |
| 112 | + q.offer(new int[]{c, d}); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + ++ans; |
| 117 | + } |
| 118 | + return -1; |
| 119 | + } |
| 120 | + |
| 121 | + // V2 |
| 122 | + // IDEA : BFD |
| 123 | + // https://www.cnblogs.com/cnoodle/p/12820573.html |
| 124 | + public int minKnightMoves_2(int x, int y) { |
| 125 | + int[][] dirs = new int[][]{{-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {-2, -1}, {-2, 1}, {2, -1}, {2, 1}}; |
| 126 | + x = Math.abs(x); |
| 127 | + y = Math.abs(y); |
| 128 | + HashSet<String> visited = new HashSet<>(); |
| 129 | + Queue<int[]> queue = new LinkedList<>(); |
| 130 | + queue.offer(new int[]{0, 0}); |
| 131 | + visited.add("0,0"); |
| 132 | + |
| 133 | + int step = 0; |
| 134 | + while (!queue.isEmpty()) { |
| 135 | + int size = queue.size(); |
| 136 | + while (size-- > 0) { |
| 137 | + int[] cur = queue.poll(); |
| 138 | + if (cur[0] == x && cur[1] == y) { |
| 139 | + return step; |
| 140 | + } |
| 141 | + |
| 142 | + for (int[] dir : dirs) { |
| 143 | + int i = cur[0] + dir[0]; |
| 144 | + int j = cur[1] + dir[1]; |
| 145 | + // (0, 0) -> (2, -1) -> (1, 1) |
| 146 | + // +2的意思是多给两个格子的空间以便于骑士跳出去再跳回来的操作 |
| 147 | + if (!visited.contains(i + "," + j) && i >= -1 && j >= -1 && i <= x + 2 && j <= y + 2) { |
| 148 | + queue.offer(new int[]{i, j}); |
| 149 | + visited.add(i + "," + j); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + step++; |
| 154 | + } |
| 155 | + return -1; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments