简体   繁体   English

用prolog解决Numberlink难题

[英]Solving the Numberlink puzzle with prolog

I have an assignment that seems out of scope of my class (I say this because they barely taught us anything about prolog), I have to write a prolog program to solve the game "Flow Free" on android. 我的作业似乎超出了我的课程范围(我说这是因为他们几乎没有教我们关于prolog的任何内容),我必须编写一个prolog程序来解决Android上的游戏“Flow Free”。 In the assignment it is called Numberlink. 在赋值中,它被称为Numberlink。 I could solve this in C++ in a hour but because I'm not too familiar with prolog it is giving me trouble. 我可以在一小时内用C ++解决这个问题,但因为我对prolog不太熟悉它给我带来了麻烦。 Here's what I would like to do: 这就是我想做的事情:

  1. Make a list that holds a boolean to indicate whether it has been visited or used. 创建一个包含布尔值的列表,以指示它是否已被访问或使用过。
  2. Recursively search all possible paths from a given starting point to the end point using a breadth first search to find the shortest paths. 使用广度优先搜索递归搜索从给定起点到终点的所有可能路径以找到最短路径。
  3. Go from there I guess. 从那里开始我想。

My attempt included searching the web on how to make a list. 我的尝试包括在网上搜索如何制作清单。 Of course prolog is not documented well at all anywhere so I came up blank and gave up. 当然prolog在任何地方都没有记录好,所以我空白并放弃了。 A friend told me to use maplist which I don't understand how I would use it to make a list including what I need. 一位朋友告诉我使用maplist,我不明白如何用它来制作一份包含我需要的清单。

Thanks in advance. 提前致谢。

EDIT: Thanks for the link, but I was looking to make a 2D list to represent the board being played on. 编辑:感谢您的链接,但我希望制作一个2D列表来代表正在播放的电路板。 Function would look like this: 功能看起来像这样:

makeList(size, list):- makeList(size,list): -

Where size is an integer representing the size of one dimension in the square list ex. 其中size是一个整数,表示方形列表ex中一个维度的大小。 (7x7). (7×7)。

Here's an implementation of @CapelliC's solution. 这是@ CapelliC解决方案的实现。 The code is self-explanatory. 代码不言自明。 2 blocks are connected if they are adjacent and have the same color, or adjacent to another connected block of the same color. 如果它们相邻并且具有相同的颜色,或者与相同颜色的另一个连接的块相邻,则连接2个块。 (I used X and Y instead of row and column, it made writing the conditions at the end a little confusing.) (我使用X和Y而不是行和列,它使得在最后编写条件有点令人困惑。)

Solving in SWI-Prolog 在SWI-Prolog中解决

在此输入图像描述

https://flowfreesolutions.com/solution/?game=flow&pack=green&set=5&level=1 https://flowfreesolutions.com/solution/?game=flow&pack=green&set=5&level=1

connected(P1, P2, M, Visited) :-
    adjacent(P1, P2),
    maplist(dif(P2), Visited),
    color(P1, C, M),
    color(P2, C, M).
connected(P1, P2, M, Visited) :-
    adjacent(P1, P3),
    maplist(dif(P3), Visited),
    color(P1, C, M),
    color(P3, C, M),
    connected(P3, P2, M, [P3|Visited]).

adjacent(p(X,Y1), p(X,Y2)) :- Y2 is Y1+1.
adjacent(p(X,Y1), p(X,Y2)) :- Y2 is Y1-1.
adjacent(p(X1,Y), p(X2,Y)) :- X2 is X1+1.
adjacent(p(X1,Y), p(X2,Y)) :- X2 is X1-1.

color(p(X,Y), C, M) :-
    nth1(Y, M, R),
    nth1(X, R, C).

sol(M) :-
    M = [[1,_,_,_,1],
         [2,_,_,_,_],
         [3,4,_,4,_],
         [_,_,_,_,_],
         [3,2,5,_,5]],
    connected(p(1,1), p(5,1), M, [p(1,1)]),
    connected(p(1,2), p(2,5), M, [p(1,2)]),
    connected(p(1,3), p(1,5), M, [p(1,3)]),
    connected(p(2,3), p(4,3), M, [p(2,3)]),
    connected(p(3,5), p(5,5), M, [p(3,5)]).

Sample query: 示例查询:

?- sol(M).
M = [[1, 1, 1, 1, 1],
     [2, 2, 2, 2, 2], 
     [3, 4, 4, 4, 2],
     [3, 2, 2, 2, 2], 
     [3, 2, 5, 5, 5]].

The declarative Prolog 'modus operandi' is based on non determinism, implemented by depth first search. 声明性Prolog'运作方式'基于非确定性,由深度优先搜索实现。 Let's apply to this puzzle: M is the playground, a list of lists of free cells (variables) or integers (colors) 让我们适用于这个难题:M是游乐场,自由单元格(变量)或整数(颜色)列表的列表

one_step(M) :-
  cell(M, X,Y, C),
  integer(C), % the selected cell is a color
  delta(X,Y,X1,Y1),
  cell(M, X1,Y1, C). % bind adjacent to same color - must be free

cell(M, X,Y, C) :- nth1(Y,M,R), nth1(X,R,C).

% moves
delta(X,Y,X1,Y) :- X1 is X+1. % right
delta(X,Y,X1,Y) :- X1 is X-1. % left
delta(X,Y,X,Y1) :- Y1 is Y-1. % up
delta(X,Y,X,Y1) :- Y1 is Y+1. % down

what this does ? 这是做什么的? let's try on a 3x3 playground 让我们试试一个3x3的游乐场

?- M=[[_,9,_],[_,0,_],[_,_,9]],one_step(M).
M = [[_G1824, 9, 9], [_G1836, 0, _G1842], [_G1848, _G1851, 9]] ;
M = [[9, 9, _G1830], [_G1836, 0, _G1842], [_G1848, _G1851, 9]] ;
M = [[_G1824, 9, _G1830], [_G1836, 0, 0], [_G1848, _G1851, 9]] ;
M = [[_G1824, 9, _G1830], [0, 0, _G1842], [_G1848, _G1851, 9]] ;
M = [[_G1824, 9, _G1830], [_G1836, 0, _G1842], [_G1848, 0, 9]] ;
M = [[_G1824, 9, _G1830], [_G1836, 0, _G1842], [_G1848, 9, 9]] ;
M = [[_G1824, 9, _G1830], [_G1836, 0, 9], [_G1848, _G1851, 9]] ;
false.

No need to declare grid size, check index boundaries, etc... when one_step/1 succeeds it has instantiated a free cell to an adjacent same color... 无需声明网格大小,检查索引边界等...当one_step / 1成功时,它已将一个空闲单元格实例化为相邻的相同颜色...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM