prolog - Minimum number of moves -


in page http://cseweb.ucsd.edu/classes/fa09/cse130/misc/prolog/goat_etc.html demonstrated how solve popular wolf, goat , cabbage puzzle.

change(e,w). change(w,e).  move([x,   x,goat,cabbage],   wolf,[y,   y,goat,cabbage]) :- change(x,y). move([x,wolf,   x,cabbage],   goat,[y,wolf,   y,cabbage]) :- change(x,y). move([x,wolf,goat,      x],cabbage,[y,wolf,goat,      y]) :- change(x,y). move([x,wolf,goat,cabbage],nothing,[y,wolf,goat,cabbage]) :- change(x,y).  oneeq(x,x,_). oneeq(x,_,x).  safe([man,wolf,goat,cabbage]) :-     oneeq(man,goat,   wolf),     oneeq(man,goat,cabbage).  solution([e,e,e,e],[]). solution(config,[firstmove|othermoves]) :-          move(config,firstmove,nextconfig),          safe(nextconfig),                          solution(nextconfig,othermoves). 

but in order find actual solution program necessary specify exact number of moves needed, this:

?- length(x,7), solution([w,w,w,w],x). x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, wolf, goat, cabbage, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; x = [goat, nothing, cabbage, goat, wolf, nothing, goat] ; false. 

is there standard way find minimum moves solution without having specify number of moves in above program?

length/2 has generative capability, avoid specifying value:

?- length(x,_),solution([w,w,w,w],x). 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -