0

我有一个 5x5 网格,由max_size(5, 5). 我需要使用 DCG 从该描述中生成所有单元格的列表。

这是我到目前为止的代码:

:- use_module(library(clpfd)).

map_size(5, 5).

natnum(0).
natnum(X) :-
    X #= X0 + 1,
    natnum(X0).

list_all_cells(Visited) -->
    { length(Visited, 25) },
    []. 
list_all_cells(Visited) -->
    [X-Y],
    { map_size(X_max, Y_max),
      natnum(X), natnum(Y),
      X #< X_max, Y #< Y_max,
      maplist(dif(X-Y), Visited) },
    list_all_cells([X-Y|Visited]).

但是,它不会生成列表并且仅输出 4 对。

对 DCG 的可能查询看起来list_all_cells([])应该列出网格上的所有单元格。例如,它将[0-0, 1-0, 1-1, 0-1]用于 2x2 网格(顺序无关紧要)。

事实上,我需要这个谓词来构建另一个谓词,该谓词available_steps/2将生成给定位置的所有可能移动的列表。有了available_steps(CurrentPos, Visited),我将能够进行蛮力Hunt the Wumpus游戏并找到所有可能的获得金币的途径。

4

1 回答 1

1
list_all_cells(Cells) :-
    bagof(C,cell(C),Cells).

cell(X-Y) :-
    between(0,4,X),
    between(0,4,Y).

示例运行:

?- list_all_cells(Cells); true.
Cells= [0-0, 0-1, 0-2, 0-3, 0-4, 1-0, 1-1, 1-2, ... - ...|...] [write]  % The letter w was pressed.
Cells= [0-0, 0-1, 0-2, 0-3, 0-4, 1-0, 1-1, 1-2, 1-3, 1-4, 2-0, 2-1, 2-2, 2-3, 2-4, 3-0, 3-1, 3-2, 3-3, 3-4, 4-0, 4-1, 4-2, 4-3, 4-4] ;
true.
于 2019-03-10T12:39:32.317 回答