|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我正在写八皇后代码,尝试运行,出现错误提示
Traceback (most recent call last):
File ".\eightqueen.py", line 63, in <module>
dfs(0)
File ".\eightqueen.py", line 44, in dfs
dfs(deep+1)
File ".\eightqueen.py", line 39, in dfs
if row[i] == False and x_row[i+deep] == False and y_row[i-deep+N] == False :
TypeError: 'bool' object is not subscriptable
代码如下:
- """
- Ver: 1.0
- Date: 2020年8月16日08:59:02
- Auther: CCandle
- """
- ####### definition of arrays ######
- N = 8
- map = []
- for i in range(0, N+1):
- line = []
- for j in range(0, N+1):
- line.append(False)
- map.append(line)
- # create a N*N map filles with False
- # to store the queens' positions
- row = []
- x_row = []
- y_row = []
- for i in range(0, N+1):
- row.append(False)
- for i in range(0, N*2 + 1):
- x_row.append(False)
- y_row.append(False)
- # to mark which row/x_row/y_row is occupied
- ###### definitions of functions ########
- def dfs(deep):
- global row
- global x_row
- global y_row
- if(deep >= N):
- print_map()
- return
- for i in range(0, N+1):
- if row[i] == False and \
- x_row[i+deep] == False \
- and y_row[i-deep+N] == False :
- row[i] = True
- x_row = True
- y_row = True
- map[deep][i] = True
- dfs(deep+1)
- row[i] = False
- x_row = False
- y_row = False
- map[deep][i] = False
- return
- def print_map():
- for i in range(0, N+1):
- for j in range(0, N+1):
- if map[i][j]:
- print(' ', end='*')
- else:
- print(' ', end=' ')
- print()
- ########## call the function ###########
- dfs(0)
复制代码
百度后发现好像是给bool类型下标了,但是我检查代码发现并没有这一问题,后来我在编辑器中看到提示:
x_row: bool
x_row: list
我就蒙了,同一个变量怎么还出来两个类型,而且我上一行的row变量就是个单纯的列表
请问各位我是哪个地方出现了问题
|
|