diff --git "a/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" "b/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" index 6b286127..a19a4cf9 100644 --- "a/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" +++ "b/LeetCode/481-490/488. \347\245\226\347\216\233\346\270\270\346\210\217\357\274\210\345\233\260\351\232\276\357\274\211.md" @@ -82,7 +82,7 @@ Tag : 「DFS」、「搜索」、「启发式搜索」 但在爆搜过程中同时维持两个字符串构造会超时,考虑使用一个 `int` 来记录 $hand$ 的使用情况。 -![image.png](https://pic.leetcode-cn.com/1636422194-vvIBfl-image.png) +![image.png](https://pic.leetcode-cn.com/1636436638-kqDwIl-image.png) 代码: ```Java @@ -106,6 +106,10 @@ class Solution { if (((cur >> i) & 1) == 1) continue; int next = (1 << i) | cur; for (int j = 0; j <= n; j++) { + boolean ok = false; + if (j > 0 && j < n && a.charAt(j) == a.charAt(j - 1) && a.charAt(j - 1) != b.charAt(i)) ok = true; + if (j < n && a.charAt(j) == b.charAt(i)) ok = true; + if (!ok) continue; StringBuilder sb = new StringBuilder(); sb.append(a.substring(0, j)).append(b.substring(i, i + 1)); if (j != n) sb.append(a.substring(j)); @@ -139,7 +143,7 @@ class Solution { 我们建立一个类 `Node` 来代指当前搜索局面。 -```Java +```Java [] class Node { // 当前的棋盘状况 String a; @@ -169,7 +173,7 @@ class Node { 需要注意的是:对于某个局面 $node$ 而言,最终的距离是由「已确定距离」+「估值距离」两部分组成,我们应当根据这两部分之和进行出队,才能确保算法的正确性。 -![image.png](https://pic.leetcode-cn.com/1636424014-cCaHWU-image.png) +![image.png](https://pic.leetcode-cn.com/1636436200-gPQhnD-image.png) 代码: ```Java @@ -206,9 +210,7 @@ class Solution { public int findMinStep(String _a, String _b) { b = _b; m = b.length(); - PriorityQueue q = new PriorityQueue<>((o1,o2)->{ - return (o1.val + o1.step) - (o2.val + o2.step); - }); + PriorityQueue q = new PriorityQueue<>((o1,o2)->(o1.val+o1.step)-(o2.val+o2.step)); q.add(new Node(_a, 1 << m, f(_a, 1 << m), 0)); map.put(_a, 0); while (!q.isEmpty()) { @@ -221,6 +223,10 @@ class Solution { if (((cur >> i) & 1) == 1) continue; int next = (1 << i) | cur; for (int j = 0; j <= n; j++) { + boolean ok = false; + if (j > 0 && j < n && a.charAt(j) == a.charAt(j - 1) && a.charAt(j - 1) != b.charAt(i)) ok = true; + if (j < n && a.charAt(j) == b.charAt(i)) ok = true; + if (!ok) continue; StringBuilder sb = new StringBuilder(); sb.append(a.substring(0, j)).append(b.substring(i, i + 1)); if (j != n) sb.append(a.substring(j));