The Milk Jug Problem
Vasiliki Liontou
19/12/2017

A={{8,0,0}};
Lines={};

For[i=1,i≤Length[A],i++,
If[(0<=A[[i]][[1]]-5+A[[i]][[2]]≤8)∧(A[[i]][[1]]+A[[i]][[2]]+A[[i]][[3]]==8),Lines= AppendTo[Lines, A[[i]]->{A[[i]][[1]]-5+A[[i]][[2]],5,A[[i]][[3]]}];
If[Count[A,{A[[i]][[1]]-5+A[[i]][[2]],5,A[[i]][[3]]}]==0,A= AppendTo[A,{A[[i]][[1]]-5+A[[i]][[2]],5,A[[i]][[3]]}] ]];
If[(0≤ A[[i]][[1]]-3+A[[i]][[3]]≤8)∧(A[[i]][[1]]+A[[i]][[2]]+A[[i]][[3]]==8), Lines=AppendTo[Lines,A[[i]]->{A[[i]][[1]]-3+A[[i]][[3]],A[[i]][[2]],3}];If[ Count[A,{A[[i]][[1]]-3+A[[i]][[3]],A[[i]][[2]],3}]==0,A=AppendTo[A,{A[[i]][[1]]-3+A[[i]][[3]],A[[i]][[2]],3}]]];
If[(0≤A[[i]][[2]]-8+A[[i]][[1]]≤5)∧(A[[i]][[1]]+A[[i]][[2]]+A[[i]][[3]]==8),Lines=AppendTo[Lines,A[[i]]->{8,A[[i]][[2]]-8+A[[i]][[1]],A[[i]][[3]]}];If[Count[A,{8,A[[i]][[2]]-8+A[[i]][[1]],A[[i]][[3]]}]==0,A=AppendTo[A,{8,A[[i]][[2]]-8+A[[i]][[1]],A[[i]][[3]]}]]]; If[(0<=A[[i]][[1]]+A[[i]][[2]]≤8)∧(A[[i]][[1]]+A[[i]][[2]]+A[[i]][[3]]==8),Lines= AppendTo[Lines,A[[i]]->{A[[i]][[1]]+A[[i]][[2]],0,A[[i]][[3]]}];If[Count[A,{A[[i]][[1]]+A[[i]][[2]],0,A[[i]][[3]]}]==0,A=AppendTo[A,{A[[i]][[1]]+A[[i]][[2]],0,A[[i]][[3]]}]]];If[(0≤(A[[i]][[2]]-3+A[[i]][[3]])≤5),Lines= AppendTo[Lines,A[[i]]->{A[[i]][[1]],A[[i]][[2]]-3+A[[i]][[3]],3}] ;If[ Count[A,{A[[i]][[1]],A[[i]][[2]]-3+A[[i]][[3]],3}]==0,AppendTo[A,{A[[i]][[1]],A[[i]][[2]]-3+A[[i]][[3]],3}]]];
If[(0≤A[[i]][[2]]+A[[i]][[3]]≤3),Lines=AppendTo[Lines, A[[i]]->{A[[i]][[1]],0,A[[i]][[2]]+A[[i]][[3]]}];If[Count[A,{A[[i]][[1]],0,A[[i]][[2]]+A[[i]][[3]]}]==0,AppendTo[A,{A[[i]][[1]],0,A[[i]][[2]]+A[[i]][[3]]}] ]];
If[(0≤ A[[i]][[2]]+A[[i]][[3]]≤5),Lines=AppendTo[Lines,A[[i]]->{A[[i]][[1]], A[[i]][[2]]+A[[i]][[3]],0}];If[Count[A,{A[[i]][[1]], A[[i]][[2]]+A[[i]][[3]],0}]==0,AppendTo[A,{A[[i]][[1]], A[[i]][[2]]+A[[i]][[3]],0}]]];
If[(0≤A[[i]][[3]]-5-A[[i]][[2]]≤3),
Lines= AppendTo[Lines,A[[i]]->{A[[i]][[1]],5,A[[i]][[3]]-5+A[[i]][[2]]}];If[Count[A,{A[[i]][[1]],5,A[[i]][[3]]-5-A[[i]][[2]]}]==0,AppendTo[A,{A[[i]][[1]],5,A[[i]][[3]]-5+A[[i]][[2]]}]]];
If[(0≤ A[[i]][[1]]+A[[i]][[3]]≤3),Lines= AppendTo[Lines, A[[i]]->{0,A[[i]][[2]],A[[i]][[1]]+A[[i]][[3]]}];If[ Count[A,{0,A[[i]][[2]],A[[i]][[1]]+A[[i]][[3]]}]==0 ,AppendTo[A,{0,A[[i]][[2]],A[[i]][[1]]+A[[i]][[3]]}]]];
If[(0≤  A[[i]][[1]]+A[[i]][[3]]≤8), Lines=AppendTo[Lines, A[[i]]->{ A[[i]][[1]]+A[[i]][[3]],A[[i]][[2]],0}];If[ Count[A,{ A[[i]][[1]]+A[[i]][[3]],A[[i]][[2]],0}]==0,AppendTo[A,{ A[[i]][[1]]+A[[i]][[3]],A[[i]][[2]],0}]]];
If[(0≤A[[i]][[3]]-8+A[[i]][[1]]≤3),Lines= AppendTo[Lines,A[[i]]->{8,A[[i]][[2]],A[[i]][[3]]-8+A[[i]][[1]]}];If[Count[A,{8,A[[i]][[2]],A[[i]][[3]]-8+A[[i]][[1]]}]==0, AppendTo[A,{8,A[[i]][[2]],A[[i]][[3]]-8+A[[i]][[1]]}]]]]
A

Lines

flag=0;

counter=0;

For[a=1,a≤ Length[Lines],a++,
If[Lines[[a]][[1]]===Lines[[a]][[2]], Lines=Delete[Lines,a];flag=1]
If[flag==1,a=a-1;flag=0;counter++]]

counter

Lines

GraphPlot[Lines, VertexLabeling->True]

graph=Graph[A,Lines]

For[j=1,j≤Length[A],j++,
If[A[[j]][[1]]==4∨A[[j]][[2]]==4∨A[[j]][[3]]==4, AppendTo[fours,A[[j]]]]]

fours=DeleteDuplicates[fours]

For[k=1,k≤Length[fours],k++,
Print[FindShortestPath[Graph[A,Lines],{8,0,0},fours[[k]]]]
]

HighlightGraph[Graph[A,Lines],PathGraph[FindShortestPath[Graph[A,Lines],{8,0,0},fours[[1]]],DirectedEdges->True]]

HighlightGraph[Graph[A,Lines],PathGraph[FindShortestPath[Graph[A,Lines],{8,0,0},fours[[2]]],DirectedEdges->True]]

HighlightGraph[Graph[A,Lines],PathGraph[FindShortestPath[Graph[A,Lines],{8,0,0},fours[[3]]],DirectedEdges->True]]