【问题标题】:Idiomatic Clojure implementation of maze generation algorithm迷宫生成算法的惯用 Clojure 实现
【发布时间】:2014-06-17 03:41:24
【问题描述】:

我正在实现算法以在 Python 和 Clojure 中创建和解决迷宫。我有使用 Python 的经验,并且正在努力学习 Clojure。我可能对从 Python 到 Clojure 的转换过于直白,我正在寻找一种更惯用的方式来在 Clojure 中实现代码的输入。

首先是工作的 Python 实现

import random

N, S, E, W = 1, 2, 4, 8
DX = {E: 1, W: -1, N: 0, S: 0}
DY = {E: 0, W: 0, N: -1, S: 1}
OPPOSITE = {E: W, W: E, N: S, S: N}


def recursive_backtracker(current_x, current_y, grid):
    directions = random_directions()
    for direction in directions:
        next_x, next_y = current_x + DX[direction], current_y + DY[direction]
        if valid_unvisited_cell(next_x, next_y, grid):
            grid = remove_walls(current_y, current_x, next_y, next_x, direction, grid)
            recursive_backtracker(next_x, next_y, grid)
    return grid


def random_directions():
    directions = [N, S, E, W]
    random.shuffle(directions)
    return directions


def valid_unvisited_cell(x, y, grid):
    return (0 <= y <= len(grid) - 1) and (0 <= x <= len(grid[y]) - 1) and grid[y][x] == 0


def remove_walls(cy, cx, ny, nx, direction, grid):
    grid[cy][cx] |= direction
    grid[ny][nx] |= OPPOSITE[direction]
    return grid

现在是我目前拥有的 Clojure 版本。目前我认为它不起作用,因为我正在使用 for 宏,它在需要传递向量时将符号传递给 recur。当我试图为这个问题找到解决方案时,我觉得我太努力地强制代码是 Python,这引发了这个问题。任何指导表示赞赏。

(ns maze.core)

(def DIRECTIONS { :N 1, :S 2, :E 4, :W 8})
(def DX { :E 1, :W -1, :N 0, :S 0})
(def DY { :E 0, :W 0, :N -1, :S 1})
(def OPPOSITE { :E 8, :W 4, :N 2, :S 1})

(defn make-empty-grid
  [w h]
  (vec (repeat w (vec (repeat h 0)))))

(defn valid-unvisited-cell?
  [x y grid]
  (and
    (<= 0 y (- (count grid) 1)) ; within a column
    (<= 0 x (- (count (nth grid y)) 1)) ; within a row
    (= 0 (get-in grid [x y])))) ; unvisited

(defn remove-walls
  [cy, cx, ny, nx, direction, grid]
  (-> grid
    (update-in [cy cx] bit-or (DIRECTIONS direction))
    (update-in [ny nx] bit-or (OPPOSITE direction))))

(defn recursive-backtracker
  [current-x current-y grid]
  (loop [current-x current-x current-y current-x grid grid]
    (let [directions (clojure.core/shuffle [:N :S :E :W])]
      (for [direction directions]
        (let [next-x (+ current-x (DX direction))
              next-y (+ current-y (DY direction))]
          (if (valid-unvisited-cell? next-x next-y grid)
            (loop next-x next-y (remove-walls current-x current-y next-x next-y direction grid)))))
      grid)))

【问题讨论】:

标签: clojure


【解决方案1】:

这似乎是将 Python 代码基本合理地翻译成 Clojure(包括一些初学者经常错过的东西 - 做得很好)......直到我们找到问题的核心 recursive-backtracker。您不能在这里只音译 Python,因为您的算法假定 grid 的可变性:您在 for 循环内递归地调用自己四次,并且您需要对网格进行更改以反映。这不是 Clojure 的工作方式,所以整个事情都行不通。你得到的实际错误是一个不相关的语法错误(仔细检查循环/递归的接口),但它在这里并不真正相关,因为无论如何你都必须重写函数,所以我会留在那里。

现在,如何在不改变 grid 的情况下重写此函数以使其工作?通常情况下,您可以使用reduce:对于四个方向中的每一个,您调用recursive-backtracker,获取修改后的网格,并确保使用修改后的网格下一个递归调用。总体轮廓如下所示:

(defn recursive-backtracker
  [current-x current-y grid]
  (reduce (fn [grid direction]
            (let [next-x (+ current-x (DX direction))
                  next-y (+ current-y (DY direction))]
              (if (valid-unvisited-cell? next-x next-y grid)
                (recursive-backtracker next-x next-y
                                       (remove-walls current-x current-y next-x next-y
                                                     direction grid))
                grid)))
          grid, (clojure.core/shuffle [:N :S :E :W])))

有了这个定义,(recursive-backtracker 0 0 (make-empty-grid 5 5)) 产生[[2 5 6 3 5] [4 10 9 6 9] [14 3 1 12 4] [12 6 3 9 12] [10 11 3 3 9]] - 这是一个有效的迷宫吗?看起来不错,但我不知道。你可能也不知道。这让我想到了另一点:使用整数和按位算术是一种毫无意义的优化练习。相反,让网格中的每个条目成为地图或集合,其中包含关键字,说明对其开放的方向。然后通过检查,您至少可以大致了解迷宫是否自洽。

【讨论】:

  • 顺便说一句,您可以在这里通过将 x/y 对用作实际对象而不是两个不同的数字来做得更好。例如,如果pos[4 3] 并且dir[0 -1],那么(map + pos dir) 产生[4 2]。比在各处分别管理 x 和 y 容易得多。
  • 通过使用位域来存储有关每个单独单元格的信息,关于不必要的优化的有效点。出色的答案和非常有用的反馈。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多