Lisp董事会计数件

问题描述 投票:0回答:2

我需要在lisp中创建一个函数来计算我板子中的碎片。让我解释一下我的问题/游戏:

我有一个像这样的板(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乘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是一个函数返回一个样本板,其中有几个定位

但是,我不知道从哪里开始。有小费吗?

编辑:永远不会在水平或垂直方面拼凑在一起。只在对角线上

function lisp common-lisp
2个回答
1
投票

我不熟悉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

0
投票

以下是如何使用列表:

(defun count-pieces (board &aux (small 0) (big 0))
  (loop for (row next) on (cons nil board)
    for bigs = nil then
    (loop for (tl tr) on row
      for (bl br) on (or next (mapcar (constantly 0) row)
      for n from 0
      for nearly-bigp = (every #’plusp (list tl tr bl br))
      for bigp = nearly-bigp then (and nearly-bigp (not pbigp))
      and pbigp = nil then bigp
      for ninbig = (or (member n bigs) (and bigp (member (1+ n) bigs)))
      if (and ninbig bigp)
        collect n and collect (+ n 1)
        and do (incf big)
      else if ninbig do (incf small)))
  (values big small))

我在手机上写了这个,所以我不确定它是否有效。它也有点笨拙。它也可能值得使用数组而不是列表,因此它可能是值得的。用-1或2或3代表一个大块,以便它们可以很容易区分。

© www.soinside.com 2019 - 2024. All rights reserved.