visited.add(state)这行代码出现了TypeError: unhashable type: 'list'的错误要怎么改
时间: 2024-02-11 09:09:18 浏览: 15
这个错误通常出现在使用了不可哈希(unhashable)的数据类型作为集合的元素时。在这个错误信息中,出现了'list',说明你在将一个列表作为集合的元素来使用。而列表是可变对象,不能被哈希,所以会出现这个错误。
如果你想要将列表作为集合的元素,可以将列表转换成元组(tuple),因为元组是不可变对象,是可以被哈希的。可以将代码修改为:
```
visited.add(tuple(state))
```
这样就可以避免这个错误了。
相关问题
代码报错Exception in thread "main" java.lang.StackOverflowError at home.rc.dfs(rc.java:29) at home.rc.dfs(rc.java:51)请修改代码
这个错误是由于递归调用dfs()方法时,栈空间不足而导致的。我们可以通过增加栈空间的方式来解决这个问题。
修改方法如下:
在执行程序时,增加栈空间的大小,例如:
```
java -Xss4m RiverCrossingProblem
```
这将增加栈空间的大小为4MB。
另外,注意在dfs()方法中,如果当前状态已经存在于已访问的状态中,则不再递归调用dfs()方法,避免重复搜索。
修改后的代码如下:
```
import java.util.*;
public class RiverCrossingProblem {
static final int WOLF = 1;
static final int SHEEP = 2;
static final int VEGETABLES = 3;
static final int EMPTY = 0;
static final int LEFT = 0;
static final int RIGHT = 1;
static final int[][] STATE = { { 0, 0, 0 }, { WOLF, SHEEP, VEGETABLES } };
static boolean isValid(int[] state) {
if (state[WOLF] == state[SHEEP] && state[WOLF] != state[EMPTY])
return false;
if (state[SHEEP] == state[VEGETABLES] && state[SHEEP] != state[EMPTY])
return false;
return true;
}
static boolean isFinalState(int[] state) {
return Arrays.equals(state, STATE[1]);
}
static void dfs(int[] state, int side, List<String> solution, Set<String> visited) {
if (!isValid(state))
return;
if (isFinalState(state))
return;
String key = Arrays.toString(state) + side;
if (visited.contains(key))
return;
visited.add(key);
int[] newState = new int[state.length];
for (int i = 0; i < state.length; i++) {
if (state[i] == side) {
newState = Arrays.copyOf(state, state.length);
newState[i] = 1 - side;
String action = "";
switch (i) {
case WOLF:
action = "wolf";
break;
case SHEEP:
action = "sheep";
break;
case VEGETABLES:
action = "vegetables";
break;
}
solution.add(action + " -> " + (side == LEFT ? "right" : "left"));
dfs(newState, 1 - side, solution, visited);
if (isFinalState(newState))
return;
solution.remove(solution.size() - 1);
}
}
}
public static void main(String[] args) {
int[] state = { WOLF, SHEEP, VEGETABLES, 1 };
List<String> solution = new ArrayList<>();
Set<String> visited = new HashSet<>();
dfs(state, 1, solution, visited);
for (String step : solution) {
System.out.println(step);
}
}
}
```
在dfs()方法中,增加了一个Set<String>类型的visited,用于存储已经访问过的状态。在递归调用dfs()方法之前,先检查当前状态是否已经存在于visited集合中,如果是,则不再递归调用dfs()方法。
这样做可以避免重复搜索,从而减少栈空间的使用,避免StackOverflowError错误。
vsited = set() while head: if head in visted: return head else: visited.add(head) head = head.next return None
这段代码是一个用于判断链表中是否有环的算法。具体来说,它使用了一个集合 `visited` 来存储已经访问过的节点,然后依次遍历链表中的每个节点,如果发现某个节点已经在 `visited` 中出现过,则说明链表中存在环,返回这个节点即可。
需要注意的是,代码中有一个拼写错误,`visited` 被写成了 `visted`。另外,最后的 `return None` 应该放在 while 循环之外,以防止链表为空时返回错误的结果。下面是修正后的代码:
```
visited = set()
while head:
if head in visited:
return head
else:
visited.add(head)
head = head.next
return None
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)