Skip to content

Commit 1bd7556

Browse files
Merge pull request SharingSource#213 from SharingSource/ac_oier
✨update: Modify 488
2 parents 960752c + 50e95c8 commit 1bd7556

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

LeetCode/481-490/488. 祖玛游戏(困难).md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Tag : 「DFS」、「搜索」、「启发式搜索」
8282

8383
但在爆搜过程中同时维持两个字符串构造会超时,考虑使用一个 `int` 来记录 $hand$ 的使用情况。
8484

85-
![image.png](https://pic.leetcode-cn.com/1636422194-vvIBfl-image.png)
85+
![image.png](https://pic.leetcode-cn.com/1636436638-kqDwIl-image.png)
8686

8787
代码:
8888
```Java
@@ -106,6 +106,10 @@ class Solution {
106106
if (((cur >> i) & 1) == 1) continue;
107107
int next = (1 << i) | cur;
108108
for (int j = 0; j <= n; j++) {
109+
boolean ok = false;
110+
if (j > 0 && j < n && a.charAt(j) == a.charAt(j - 1) && a.charAt(j - 1) != b.charAt(i)) ok = true;
111+
if (j < n && a.charAt(j) == b.charAt(i)) ok = true;
112+
if (!ok) continue;
109113
StringBuilder sb = new StringBuilder();
110114
sb.append(a.substring(0, j)).append(b.substring(i, i + 1));
111115
if (j != n) sb.append(a.substring(j));
@@ -139,7 +143,7 @@ class Solution {
139143

140144
我们建立一个类 `Node` 来代指当前搜索局面。
141145

142-
```Java
146+
```Java []
143147
class Node {
144148
// 当前的棋盘状况
145149
String a;
@@ -169,7 +173,7 @@ class Node {
169173

170174
需要注意的是:对于某个局面 $node$ 而言,最终的距离是由「已确定距离」+「估值距离」两部分组成,我们应当根据这两部分之和进行出队,才能确保算法的正确性。
171175

172-
![image.png](https://pic.leetcode-cn.com/1636424014-cCaHWU-image.png)
176+
![image.png](https://pic.leetcode-cn.com/1636436200-gPQhnD-image.png)
173177

174178
代码:
175179
```Java
@@ -206,9 +210,7 @@ class Solution {
206210
public int findMinStep(String _a, String _b) {
207211
b = _b;
208212
m = b.length();
209-
PriorityQueue<Node> q = new PriorityQueue<>((o1,o2)->{
210-
return (o1.val + o1.step) - (o2.val + o2.step);
211-
});
213+
PriorityQueue<Node> q = new PriorityQueue<>((o1,o2)->(o1.val+o1.step)-(o2.val+o2.step));
212214
q.add(new Node(_a, 1 << m, f(_a, 1 << m), 0));
213215
map.put(_a, 0);
214216
while (!q.isEmpty()) {
@@ -221,6 +223,10 @@ class Solution {
221223
if (((cur >> i) & 1) == 1) continue;
222224
int next = (1 << i) | cur;
223225
for (int j = 0; j <= n; j++) {
226+
boolean ok = false;
227+
if (j > 0 && j < n && a.charAt(j) == a.charAt(j - 1) && a.charAt(j - 1) != b.charAt(i)) ok = true;
228+
if (j < n && a.charAt(j) == b.charAt(i)) ok = true;
229+
if (!ok) continue;
224230
StringBuilder sb = new StringBuilder();
225231
sb.append(a.substring(0, j)).append(b.substring(i, i + 1));
226232
if (j != n) sb.append(a.substring(j));

0 commit comments

Comments
 (0)