Prolog错误:超出本地堆栈

时间:2016-05-01 19:08:58

标签: search prolog

我目前正在努力完成这项任务,但我无法弄清楚我的终止未能结束的原因。我已经制作(或接近)广度优先搜索算法,用于跟踪发生了什么动作的迷宫。

fewestRotationsSingle(Maze, Moves):-
    search([Maze], [[]], Moves).

search([], [], []).
search(Mazes, [PH|PT], R):-
    length(PH, M),
    (M > 5 ->
        search([], [], R) ;
        append([PH], PT, Paths),
        (possible(Mazes, Paths, Mazes1, Paths1),
        checkIfDone(Mazes1, Paths1, Solutions),
        length(Solutions, N),
        (N > 0 ->
            member(R, Solutions);
            search(Mazes1, Paths1, R)
        )
        )
    ).

possible([], [], [], []).
possible([MH|MT], [PH|PT], Ms, Ps):-
    helpers:oneMove(MH, 180, M1),
    helpers:oneMove(MH, c, M2),
    helpers:oneMove(MH, cc, M3),
    possible(MT, PT, MS, PS),
    append([M1, M2, M3], MS, Ms),
    append(PH, [180], P1),
    append(PH, [c], P2),
    append(PH, [cc], P3),
    append([P1, P2, P3], PS, Ps).

checkIfDone([], [], []).
checkIfDone([M|Ms], [P|Ps], R):-
    helpers:checkIfGoal(M, G),
    (G = 1 -> 
        checkIfDone(Ms, Ps, R) ;
        (checkIfDone(Ms, Ps, S),
        append([P], S, R))
    ).

当我测试时我使用:

Y = [[x,x,x,x],[x,-,g,x],[x,1,-,x],[x,x,x,x]],
fewestRotationsSingle(Y, Moves).

给了我:

Moves = [180, cc] ;
Moves = [c, c];
ERROR: Out of local stack

我哪里错了?

use_module(library(clpfd), []).

trans([], []).
trans([H|T], R):-
    nodebug,
    length(H, N),
    emptyLists(N, E),
    append([H], T, M),
    transRec(M, E, R).

emptyLists(0, []).
emptyLists(N, R):-
    L is N - 1,
    emptyLists(L, S),
    append(S, [[]], R).

transRec([], A, A).
transRec([Row|Rows], Cols, Result):-
    transposeRow(Row, Cols, S),
    transRec(Rows, S, Result).

transposeRow([], [], []).
transposeRow([RH|RT], [CH|CT], Result):-
    transposeRow(RT, CT, S),
    append(CH, [RH], R),
    append([R], S, Result).

oneMove(M, 180, R):-
    move180(M, M1),
    dropPlayers(M1, R).

oneMove(M, c, R):-
    movec(M, M1),
    dropPlayers(M1, R).

oneMove(M, cc, R):-
    movecc(M, M1),
    dropPlayers(M1, R).

movec(Maze, R):-
    trans(Maze, S),
    maplist(reverse, S, R).

movecc(Maze, R):-
    trans(Maze, S),
    reverse(S, R).

move180(Maze, R):-
    reverse(Maze, S),
    maplist(reverse, S, R).

dropPlayers(M, R):-
    trans(M, S1),
    dropRows(S1, S2),
    trans(S2, R).

dropRows([], []).
dropRows([H|T], R):-
    dropRow(H, Q),
    dropRows(T, S),
    append([Q], S, R).

dropRow([], []).
dropRow([H|T], R):-
    numlist(1,4,NL),
    (member(H, NL) ->
        (append([H], T, S), dropPlayer(S, R));
        (dropRow(T, S),
        append([H], S, R))
    ).

dropPlayer([], []).
dropPlayer([H1|[H2|T]], R):-
    (H2 = - ->
        (append([H1], T, S1),
        dropPlayer(S1, S2),
        append([H2], S2, R)) ;
        (H2 = x ->
            (dropRow(T, S1),
            append([[H1],[H2],S1], R)) ;
            (H2 = g ->
                (dropRow(T, S1),
                append([[-],[H1],S1], R)) ;
                (append([H2], T, S1),
                dropPlayer(S1, S2),
                append([H1], S2, S3),
                dropPlayer2(S3, R))
            )
        )
    ).

dropPlayer2([H1|[H2|T]], R):-
    (H2 = - ->
        (append([H1], T, S1),
        dropPlayer2(S1, S2),
        append([H2], S2, R)) ;
        (dropRow(T, S1),
        append([[H1], [H2], S1], R))
    ).

checkIfGoal([], 0).
checkIfGoal([H|T], R):-
    findall(X, (member(X, H), X = g), L),
    length(L,N),
    checkIfGoal(T, S),
    R is N + S.

0 个答案:

没有答案