我有 3 壶水的问题要解决,但有一个小技巧。我不必使用算法,而是要有一个“功能”,允许用户将升从一个壶移动到另一个壶,其初始和最终状态是它所写的也由他。
例如,他写了 initial(10,0,0,0,r) 并且第一个状态是第一个是 10 升,其他两个是零,他还写了 final(0,3,3,3,l) 和 final state 在两个较小的罐子里有 3 升,在第一个罐子里是零。
壶之间的“移动”发生在他写 go(7,3,r) 时,他向右移动 3 升(从右到左,壶的形状从大到小)从大到第二个壶,-7是剩下的升,3 是要移动的升,r 是方向-。
我已经写了这个序言代码,但是每个 go-state 都是错误的..有人知道为什么吗?
:- dynamic go/3.
:- dynamic cur_state/1,init/5.
:- dynamic end_state/1, final/5.
cur_state(State):-State = state(10,0,0,7,l).
end_state(State):-State = state(0,3,3,0,r).
pour(state(D1,D2,D3,N,l),move(D,C,r),state(D,C,D3,N,r)) :-
D is D1-N,
C is D2+N.
pour(state(D1,D2,D3,N,r),move(D,C,l),state(D,C,D3,N,l)) :-
D is D1-N,
C is D2.
pour(state(D1,D2,D3,N,l),move(D,C,r),state(D,D2,C,N,r)) :-
D is D1-N,
C is D3+N.
pour(state(D1,D2,D3,N,l),move(D,C,r),state(D1,D,C,N,r)) :-
D is D2-N,
C is D3+N.
pour(state(D1,D2,D3,N,r),move(D,C,l),state(D1,D,C,N,l)) :-
D is D2-N,
C is D1+N.
pour(state(D1,D2,D3,N,r),move(D,C,l),state(D1,D,c,N,l)) :-
D is D2-N,
C is D3.
pour(state(D1,D2,D3,N,l),move(D,C,r),state(C,D2,D,N,r)) :-
D is D3-N,
C is D1.
pour(state(D1,D2,D3,N,r),move(D,C,l),state(D1,C,D,N,l)) :-
D is D3-N,
C is D2+N.
pour(state(D1,D2,D3,N,r),move(D,C,l),state(C,D2,D,N,l)) :-
D is D3-N,
C is D1+N.
carry(7,0).
carry(3,0).
carry(10,0).
carry(4,0).
carry(7,3).
legal(10,X,Y):-X+Y=<10.
legal(X,Y,Z):-X+Y+Z=<10.
legal(X,7,Y):-X+Y=<3.
legal(X,Y,3):-X+Y=<7.
newstate(state(D1,D2,D3,N,l),state(D11,D22,D33,N1,r)):-
carry(M,C),
M=<7,C=<3,
D22 is D2+N,
D11 is D1-N,
D3 is D33,
N1 is N,
D2=<7,D1=<10,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,r),state(D11,D22,D33,N1,l)):-
carry(M,C),
M=<10,C=<100,
D11 is D1-N,
D22 is D2,
D33 is D3,
D1=<10,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,l),state(D11,D22,D33,N1,r)):-
carry(M,C),
M=<10,C<3,
D11 is D1-N,
D33 is D3+N,
D22 is D2,
D1=<10,D3=<3,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,r),state(D11,D22,D33,N1,l)):-
carry(M,C),
M=<7,C=<3,
D22 is D2-N,
D33 is D1+N,
D11 is D1,
D2=<7,D1=<10,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,l),state(D11,D22,D33,N1,r)):-
carry(M,C),
M=<7,C=0,
D22 is D2-N,
D33 is D3+N,
D11 is D1,
D2=<7,D3=<3,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,r),state(D11,D22,D33,N1,l)):-
carry(M,C),
M=<7,C=<100,
D22 is D2-N,
D33 is D3,
D11 is D1,
D2=<7,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,r),state(D11,D22,D33,N1,l)):-
carry(M,C),
M=<3,C=<7,
D22 is D2+N,
D33 is D3-N,
D11 is D1,
D3=<3,D2=<7,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,r),state(D11,D22,D33,N1,l)):-
carry(M,C),
M=<3,C=<100,
D11 is D1+N,
D33 is D3-N,
D22 is D2,
D3=<3,D1=<10,
legal(D1,D2,D3).
newstate(state(D1,D2,D3,N,l),state(D11,D22,D33,N1,r)):-
carry(M,C),
M=<3,C=<100,
D33 is D3-N,
D22 is D2,
D11 is D1,
D3=<3,
legal(D1,D2,D3).
eisodos(_):- cur_state(State),write(State),nl.
init(S1,S2,S3,S4,S5):-assert(cur_state(State):-State = state(S1,S2,S3,S4,S5)),write('Arxikh:'),
write(state(S1,S2,S3,S4,S5)),retractall(init(S1,S2,S3,S4,S5)),nl.
final(S1,S2,S3,S4,S5):-assert(end_state(State):-State = state(S1,S2,S3,S4,S5)),write('Telikh:'),
write(state(S1,S2,S3,S4,S5)),retractall(final(S1,S2,S3,S4,S5)),nl.
go(Move1,Move2,Move3):-cur_state(State),newstate(State,NextState),
pour(State,move(Move1,Move2,Move3), NextState),
retractall(cur_state(State):-State = state(_,_,_,_,_)),asserta(cur_state(NextState)),
((end_state(NextState),write('Bravo!!!!')) ;(write(' ---*Eiste sthn katastash --- :'),write(NextState))),nl.