Clojure——学习迷宫生成
背景
初学clojure,想着看一些算法来熟悉clojure语法及相关算法实现。
找到一个各种语言生成迷宫的网站:http://rosettacode.org/wiki/Maze_generation
在上述网站可以看到clojure的实现版,本文就是以初学者的视角解读改程序。
小试牛刀
先看一些简单的示例,以帮助我们理解迷宫生成程序。
绑定符号x++
(defn f [x]
(let [x++ (+ x 5)]
#{[x x++]}))
(println (f 1))
=> #'sinlov.clojure.base-learn/f
#{[1 6]}
=> nil
Tips: 上述程序将x++
绑定为x+5
,不同于c语言中的自增运算符。
集合过滤
(select odd? (set [1 2 3 4 5]))
=> #{1 3 5}
(select (partial odd?) (set [1 2 3 4 5]))
=> #{1 3 5}
select
语法参考文档:http://clojuredocs.org/clojure.set/select
partial
解释见下文
vec交叉合并
(interleave [0 1 2] ['a 'b 'c])
=> (0 a 1 b 2 c)
(interleave [0 1 2] ['a 'b 'c] ['b])
=> (0 a b)
(interleave [0 1 2] ['a 'b 'c] (repeat 'z))
=> (0 a z 1 b z 2 c z)
文档:http://clojuredocs.org/clojure.core/interleave
transduce
transducer是clojure里面的一种编程思想,使用transducer可以简化很多语法。
可以参考这篇文章链接,帮助理解
文档:http://clojuredocs.org/clojure.core/transduce
思路
笔者阅读了迷宫生成算法,将思路整理如下
坐标点与符号映射关系
比如迷宫的左上角┌
是如何生成的,不同大小的迷宫如何确定?
经过阅读源码发现,一个坐标点的符号与其周围4个临接点相关,如果按照坐标点表示,5个点排序顺序是一致的。
比如,上述坐标点(5,5),和其4个临界点。可以看到在该坐标系内,一个点与其临界点做成的集合排序一定是下面的顺序:
比如迷宫左上角坐标是(0, 0),该点五元组应该是
不在迷宫,不在迷宫,(0, 0), (0, 1), (1, 0)
假设不在迷宫或者该位置为空,记为0
;如果是墙记为1
那么上述五元组可以换算为11100
。
再比如迷宫右上角,五元组为
(n-1, 0), 不在迷宫, (n, 0), (n, 1), 不在迷宫
可换算为10110
。
按照如上规则可以生成如下表:
[" " " " " " " " "· " "╵ " "╴ " "┘ "
" " " " " " " " "╶─" "└─" "──" "┴─"
" " " " " " " " "╷ " "│ " "┐ " "┤ "
" " " " " " " " "┌─" "├─" "┬─" "┼─"]
程序代码
(ns maze.core
(:require [clojure.set :refer [intersection
select]]
[clojure.string :as str]))
;; 得到周围临界点
(defn neighborhood
([] (neighborhood [0 0]))
([coord] (neighborhood coord 1))
([[y x] r]
(let [y-- (- y r) y++ (+ y r)
x-- (- x r) x++ (+ x r)]
#{[y++ x] [y-- x] [y x--] [y x++]})))
;; 判断位置是否为空
(defn cell-empty? [maze coords]
(= :empty (get-in maze coords)))
;; 判断位置是否为墙
(defn wall? [maze coords]
(= :wall (get-in maze coords)))
;; 过滤迷宫中指定类型的点的集合
(defn filter-maze
([pred maze coords]
(select (partial pred maze) (set coords)))
([pred maze]
(filter-maze
pred
maze
(for [y (range (count maze))
x (range (count (nth maze y)))]
[y x]))))
;; 创建新迷宫
(defn create-empty-maze [width height]
(let [width (inc (* 2 width))
height (inc (* 2 height))]
(vec (take height
(interleave
(repeat (vec (take width (repeat :wall))))
(repeat (vec (take width (cycle [:wall :empty])))))))))
(defn next-step [possible-steps]
(rand-nth (vec possible-steps)))
;; 核心算法,深度优先递归
(defn create-random-maze [width height]
(loop [maze (create-empty-maze width height)
stack []
nonvisited (filter-maze cell-empty? maze)
visited #{}
coords (next-step nonvisited)]
(if (empty? nonvisited)
maze
(let [nonvisited-neighbors (intersection (neighborhood coords 2) nonvisited)]
(cond
(seq nonvisited-neighbors)
(let [next-coords (next-step nonvisited-neighbors)
wall-coords (map #(+ %1 (/ (- %2 %1) 2)) coords next-coords)]
(recur (assoc-in maze wall-coords :empty)
(conj stack coords)
(disj nonvisited next-coords)
(conj visited next-coords)
next-coords))
(seq stack)
(recur maze (pop stack) nonvisited visited (last stack)))))))
;; 迷宫坐标与字符映射
(def cell-code->str
[" " " " " " " " "· " "╵ " "╴ " "┘ "
" " " " " " " " "╶─" "└─" "──" "┴─"
" " " " " " " " "╷ " "│ " "┐ " "┤ "
" " " " " " " " "┌─" "├─" "┬─" "┼─"])
;; 获取迷宫坐标的类型
;; 使用5 bit表示一个点对应的字符映射
;; 例如:00111对应┘
(defn cell-code [maze coord]
(transduce
(comp
(map (partial wall? maze))
(keep-indexed (fn [idx el] (when el idx)))
(map (partial bit-shift-left 1)))
(completing bit-or)
0
(sort (cons coord (neighborhood coord)))))
(defn cell->str [maze coord]
(get cell-code->str (cell-code maze coord)))
;; 将迷宫坐标转换为字符
(defn maze->str [maze]
(->> (for [y (range (count maze))]
(for [x (range (count (nth maze y)))]
(cell->str maze [y x])))
(map str/join)
(str/join \newline)))
;; 生成迷宫
(println (maze->str (create-random-maze 10 10)))
上述程序输出:
Clojure——学习迷宫生成的更多相关文章
- ICML 2018 | 从强化学习到生成模型:40篇值得一读的论文
https://blog.csdn.net/y80gDg1/article/details/81463731 感谢阅读腾讯AI Lab微信号第34篇文章.当地时间 7 月 10-15 日,第 35 届 ...
- O(n)线性空间的迷宫生成算法
之前所有的迷宫生成算法,空间都是O(mn),时间同样是O(mn),时间上已经不可能更优化, 于是,我就从空间优化上着手,研究一个仅用O(n)空间的生成算法. 我初步的想法是,每次生成一行,生成后立即输 ...
- NASNet学习笔记—— 核心一:延续NAS论文的核心机制使得能够自动产生网络结构; 核心二:采用resnet和Inception重复使用block结构思想; 核心三:利用迁移学习将生成的网络迁移到大数据集上提出一个new search space。
from:https://blog.csdn.net/xjz18298268521/article/details/79079008 NASNet总结 论文:<Learning Transfer ...
- [迷宫中的算法实践]迷宫生成算法——递归分割算法
Recursive division method Mazes can be created with recursive division, an algorithm which wo ...
- MySQL学习记录--生成时间日期数据
时间数据格式组件: 组件 定义 范围 YYYY 年份,包括世纪 1000~9999 MM 月份 01(January)~12(December) DD 日 01~31 HH 小时 00~23 HHH ...
- Clojure学习笔记(一)——介绍、安装和语法
什么是Clojure Clojure是一种动态的.强类型的.寄居在JVM上的语言. Clojure的特性: 函数式编程基础,包括一套性能可以和典型可变数据结构媲美的持久性数据结构 由JVM提供的成熟的 ...
- [ExtJS5学习笔记]第三节 sencha cmd学习笔记 生成应用程序构建的内部细节
本文地址: http://blog.csdn.net/sushengmiyan/article/details/38316829本文作者:sushengmiyan------------------- ...
- Clojure学习资料
以下大部分收藏自博客:http://blog.csdn.net/ithomer/article/details/17225813 官方文档: http://clojure.org/documentat ...
- Clojure 学习入门(19)—— 数组
1.创建数组 1.1 从集合创建数组 into-array into-array (into-array aseq) (into-array type aseq) 演示样例: user=> (i ...
随机推荐
- Ubuntu14.04下搭建VPN服务 -pptp
在Ubantu下采用PPTP搭建VPN,优点是配置简单快捷.本教程亲自测试,熟练了在新机器上5分钟搞定VPN. - - - - - - - - - - - - - - - - - - - - - - ...
- if 分支语句
写在<script></script>里面. if(判断条件){满足条件时要执行的语句} else{不满足条件时要执行的语句} 三元运算:var x = 判断条件?值1:值2: ...
- jQuery给表单设置值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- macaca web(4)
米西米西滴,吃过中午饭来一篇,话说,上回书说道macaca 测试web(3),参数驱动来搞,那么有小伙本又来给雷子来需求, 登录模块能不能给我给重新封装一下吗, 我说干嘛封装,现在不挺好,于是乎,接着 ...
- Android studio 1.x 安装完毕后无法打开问题解决方案
Android Studio 1.0正式发布,给Android开发者带来了不小的惊喜,再也不用为繁琐的环境配置而烦恼,从某一层面上说这降低了android开发门槛. 不过貌似只能开心一会儿,因为and ...
- NSA武器库知识整理
美国国家安全局(NSA)旗下的"方程式黑客组织"(shadow brokers)使用的部分网络武器被公开,其中包括可以远程攻破全球约70%Windows机器的漏洞利用工具. 其中, ...
- 【译】怎样处理 Safari 移动端对图片资源的限制
原文作者:Thijs van der Vossen 本文翻译自<How to work around the Mobile Safari image resource limit>,原文写 ...
- Linq--一个集合中查找另一个集合,需熟悉这种写法
//获取科室与病区授权的护士信息 public List<SYS_ZGKSBQDYK> GetUserWardMapByWardCode(string wardCode) ...
- 【小白成长撸】--多项式求圆周率PI
/*程序的版权和版本声明部分: *Copyright(c) 2016,电子科技大学本科生 *All rights reserved. *文件名:多项式求PI *程序作用:计算圆周率PI *作者:Amo ...
- 使用vs2015编写c语言程序
使用vs2015编写c语言程序 转载Yanky--博客园 http://www.cnblogs.com/yankyblogs/p/7058036.html 编写c语言程序的软件有很多,当年刚开始学 ...