Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions LeetCode/481-490/488. 祖玛游戏(困难).md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down Expand Up @@ -139,7 +143,7 @@ class Solution {

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

```Java
```Java []
class Node {
// 当前的棋盘状况
String a;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -206,9 +210,7 @@ class Solution {
public int findMinStep(String _a, String _b) {
b = _b;
m = b.length();
PriorityQueue<Node> q = new PriorityQueue<>((o1,o2)->{
return (o1.val + o1.step) - (o2.val + o2.step);
});
PriorityQueue<Node> 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()) {
Expand All @@ -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));
Expand Down