我需要在 lisp 中创建一个函数来计算棋盘中的棋子数量。让我解释一下我的问题/游戏:
我有一 block 像这样的板 (10x10):
(
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
)
在这个棋盘中,我们将代表两种类型的棋子:
- 1乘1正方形
- 2 x 2 正方形
它们将像这样放置在我的面板中:
(
(0 1 0 0 0 0 0 1 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 1 1 0 0 0 0 0)
(0 0 0 1 1 0 0 1 1 0)
(0 0 0 0 0 0 0 1 1 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 1 0 0 0 0 1 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
)
在这个例子中,我有四个 1×1 的正方形和两个 2×2 的正方形。 我需要一个函数来计算我的板上有多少给定类型的棋子。
示例:
(defun countPiece (board pieceType)
; here I am supposed to write my code
)
因此,如果我调用 (countPiece (testBoard) 'twoByTwoSquare)
,它会返回 2
,例如,其中 testboard 是一个返回样本板的函数,其中放置了几 block
但是,我不知道从哪里开始。有什么建议吗?
编辑:永远不会有水平或垂直的碎片。仅在对角线上
最佳答案
我的 lisp 不是很流利,所以我的回答只会给你算法解决方案:
singlePieces = empty list
doublePieces = empty list
for each row
for each column
if ((value[row][column] = 1)
and ((row - 1, column - 1) not in doublePieces)
and ((row - 1, column) not in doublePieces)
and ((row, column - 1) not in doublePieces)) then
if ((row < rowCount)
and (column < columnCount) and (value[row][column + 1] = 1)
and (value[row + 1][column] = 1)
and (value[row + 1][column + 1]) = 1) then
doublePieces.add(row, column)
else
singlePieces.add(row, column)
end if
end if
end for
end for
关于function - Lisp 板计数件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47857414/