Prolog:从哪里开始解决类似扫雷的难题? [英] Prolog: where to begin solving Minesweeper-like puzzle?

查看:49
本文介绍了Prolog:从哪里开始解决类似扫雷的难题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在序言中写一些类似扫雷的东西.我可以用普通"语言做到这一点,但是当我尝试使用 prolog 开始编码时,我完全不知道如何开始.我需要一些提示.输入规范:

I need to write something like a minesweeper in prolog. I am able to do that in "normal" language but when i try to start coding with prolog i totally don't know how to start. I need some sort of tips. Input specification:

棋盘尺寸:m × n (m, n ∈ {1,...,16}),三元组列表 (i, j, k),其中 i ∈ {1,...,m}, j ∈ {1,...,n}, k ∈ {1,...,8}) 用数字描述字段.

Board size: m × n (m, n ∈ {1,...,16}), list of triples (i, j, k), where i ∈ {1,...,m}, j ∈ {1,...,n}, k ∈ {1,...,8}) describing fields with numbers.

例如:

5
5
[(1,1,1), (2,3,3), (2,5,2), (3,2,2), (3,4,4), (4,1,1), (4,3,1), (5,5,2)].

输出:数字和原子列表*(宝藏)和(空白字段).它是拼图解决方案的表示.

Output: list of digits and the atoms * (for treasure) and (for blank fields). It is a representation of puzzle solution.

这个谜题的规则:在一块板的20个领域有隐藏的宝藏.一个字段中的数字表示有多少个邻居字段有一个宝藏.有数字的领域没有宝藏.用宝物标记所有字段.

Rules of this puzzle: In 20 fields of a board there are hidden treasures. A digit in a field represents how many neighbour-fields have a treasure. There are no treasures in fields with a digit. Mark all fields with a treasure.

你需要猜猜对角线上藏着多少宝藏.

You need to guess how many treasures are hidden in diagonals.

如果您提供任何提示,我将不胜感激.我不想要完整的解决方案,我想自己写,但没有线索我无法做到.

I would be grateful for any tips. I don't want full solution, I want to write it by my own, but without clues I am not able to do that.

推荐答案

矩阵通常作为列表的列表处理,您可以使用 length/2 和 findall/3 来构建.空变量矩阵(您将在猜测时放置值....)

A matrix is usually handled as a list of list, you can build using length/2 and findall/3. A matrix of empty variables (where you will place values while guessing....)

build_matrix(NRows, NCols, Mat) :-
  findall(Row, (between(1, NRows, _), length(Row, NCols)), Mat).

可以使用 nth1 完成通过坐标访问元素(请参阅此处 另一个答案,您可以在其中找到一些详细信息:请参阅单元格/3).

Accessing elements via coordinates can be done using nth1 (see here for another answer where you can find some detail: see cell/3).

然后你放置所有的三元组约束:消耗隐藏的宝藏"计数器的方式是有限的,让 Prolog 搜索所有的方式,枚举相邻的.

Then you place all your triples constraints: there is finite number of ways of consuming the 'hidden treasure' counters, let Prolog search all the ways, enumerating adjacents.

使用递归谓词处理三元组列表,将每个计数器放置在兼容的单元格中.当列表结束时,你有一个猜测.

Process the list of triples, placing each counter in compatible cells, with a recursive predicate. When the list ends, you have a guess.

为了让你的代码更简单,不要担心索引超出矩阵范围,记住搜索时失败是正常的"...

To keep your code simpler, don't worry about indexes out of matrix bounds, remember that failures are 'normal' when searching...

这篇关于Prolog:从哪里开始解决类似扫雷的难题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆