【发布时间】:2017-11-30 19:23:51
【问题描述】:
我正在尝试解决自定义矩阵的国际象棋骑士问题,但我不知道问题出在哪里。 国际象棋骑士必须访问每个地方一次,当他设法访问所有领域时,就完成了程序。目前,他一直在寻找正确的字段组合,这需要很长时间...... 我不知道下一步该做什么或要改变什么
append([], POINT, [POINT]).
append([H|T], POINT, [H|R]) :-
append(T, POINT, R).
member(POINT,[POINT|_]).
member(POINT,[_|R]) :-
member(POINT, R).
try(X, Y, Xmax, Ymax, A, B) :-
(X + 1 < Xmax, Y + 2 < Ymax, A is X + 1, B is Y + 2).
try(X, Y, Xmax, Ymax, A, B) :-
(X + 2 < Xmax, Y + 1 < Ymax, A is X + 2, B is Y + 1).
try(X, Y, Xmax, Ymax, A, B) :-
(X + 2 < Xmax, Y - 1 >= Ymax - Ymax, A is X + 2, B is Y - 1).
try(X, Y, Xmax, Ymax, A, B) :-
(X + 1 < Xmax, Y - 2 >= Ymax - Ymax, A is X + 1, B is Y - 2).
try(X, Y, Xmax, Ymax, A, B) :-
(X - 1 >= Xmax - Xmax, Y - 2 >= Ymax - Ymax, A is X - 1, B is Y - 2).
try(X, Y, Xmax, Ymax, A, B) :-
(X - 2 >= Xmax - Xmax, Y - 1 >= Ymax- Ymax, A is X - 2, B is Y - 1).
try(X, Y, Xmax, Ymax, A, B) :-
(X - 2 >= Xmax - Xmax, Y + 1 < Ymax, A is X - 2, B is Y + 1).
try(X, Y, Xmax, Ymax, A, B) :-
(X - 1 >= Xmax - Xmax, Y + 2 < Ymax, A is X - 1, B is Y + 2).
move(X, Y, Xmax, Ymax, L) :-
not(member([X, Y], L)),
write('X='), write(X),
write('Y='), write(Y), nl,
try(X, Y, Xmax, Ymax, A, B),
append(L, [X, Y], L2),
move(A, B, Xmax, Ymax, L2).
例子:move(0, 0, 3, 3, [])。
输出
X=0Y=0
X=1Y=2
X=2Y=0
X=0Y=1
X=2Y=2
X=1Y=0
X=0Y=2
X=2Y=1
X=2Y=1
X=0Y=2
X=1Y=0
X=2Y=2
X=0Y=1
X=2Y=0
X=1Y=2
false
确实如此,因为它永远不会在矩阵的中心。 但是对于矩阵 5x5 他找不到组合...
【问题讨论】:
-
你也应该发布你想要的输出。对我来说,它很快就失败了。
-
实际输出是无穷大...swish.swi-prolog.org 运行命令:move(0, 4, 5, 5, []).
-
所以没有问题?
-
问题是,我不知道它是否有效,因为他一直在寻找正确的组合。
-
但是它打印输出?
标签: algorithm prolog knights-tour