我正在尝试解决Leetcode.com上的“ Available Captures for Rook”问题,但是我在尝试做的事情上遇到了一些困难(我是python的新手)。
因此,问题基本上是一个8x8的棋盘,其中有一个白鸦,几个白主教和许多黑典当,我必须计算白鸦可用的黑典当。 首先,我获得了白色车的位置,然后获得了黑色棋子的所有位置,并将它们存储在称为“ positions”的元组列表中,然后我试图过滤那些与i或j不匹配的元素白车位置。
示例输入可能是:
[[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".","R",".",".",".","p"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".","p",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."]]
和我的代码。
x = y = 0
count = 0
positions = []
for i in range(8):
for j in range(8):
if board[i][j] == "R":
x = i
y = j
elif board[i][j] == "p":
positions.append(tuple([i,j]))
print(x,y)
for pos in positions:
if x != pos[0] and y != pos[1]:
print(pos[0],pos[1])
positions.remove(pos)
return positions
所以我的职位列表原来是这样的:
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5),
(2, 1), (2, 2), (2, 4), (2, 5),
(3, 1), (3, 5),
(4, 1), (4, 2), (4, 4), (4, 5),
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5)]
,经过过滤后,我应该得到:
[(1, 3), (3, 1), (3, 5), (5, 3)]
但是我得到了:
[[1,2],[1,3],[1,5],[2,2],[2,5],[3,1],[3,5],[4,2],[4,5],[5,2],[5,3],[5,5]]
注意:这当然不是完整的解决方案,而是我尝试解决它的步骤。