here is my wrong dijkstra program... maybe some one can look over it... who knows ? program SAVEMAN_TIMUS; type TCOORD=record x,y:shortint; end; TCOORD3=record x,y,k:shortint; end; const NMOVES=4; MOVELEFT=1; MOVERIGHT=2; MOVEBW=3; MOVEFW=4; type TMOVES=array[1..4]of tcoord; const MOVES:TMOVES=((x:-1;y:0),(x:+1;y:0),(x:0;y:+1),(x:0;y:-1)); const NFACES=6; FW=1; BW=2; TOP=3; RIGHT=4; BOTTOM=5; LEFT=6; type TROLES=array[1..NMOVES,1..NFACES]OF 1..NFACES; const ROLES:TROLES= ((FW,BW,RIGHT,BOTTOM,LEFT,TOP), (FW,BW,LEFT,TOP,RIGHT,BOTTOM), (BOTTOM,TOP,FW,RIGHT,BW,LEFT), (TOP,BOTTOM,BW,RIGHT,FW,LEFT)); const NPERMUTATIONS=24; type TFACE=array[1..NFACES]of word; TFACES=array[1..NFACES]OF 1..NFACES; TPERMUTATIONS=array[1..NPERMUTATIONS] of TFACES; const PERMUTATIONS:TPERMUTATIONS= ((1,2,3,4,5,6),(1,2,4,5,6,3),(1,2,5,6,3,4),(1,2,6,3,4,5),(2,1,3,6,5,4), (2,1,4,3,6,5),(2,1,5,4,3,6),(2,1,6,5,4,3),(3,5,1,6,2,4),(3,5,2,4,1,6), (3,5,4,1,6,2),(3,5,6,2,4,1),(4,6,1,3,2,5),(4,6,2,5,1,3),(4,6,3,2,5,1), (4,6,5,1,3,2),(5,3,1,4,2,6),(5,3,2,6,1,4),(5,3,4,2,6,1),(5,3,6,1,4,2), (6,4,1,5,2,3),(6,4,2,3,1,5),(6,4,3,1,5,2),(6,4,5,2,3,1)); type TBR=array[1..NPERMUTATIONS,1..2]OF 1..NFACES; const BR:TBR=((5,4),(6,5),(3,6),(4,3),(6,5),(6,3),(3,4),(4,5),(2,6),(1,4), (3,1),(4,2),(2,3),(1,5),(5,2),(3,1),(2,4),(1,6),(6,2),(4,1), (2,5),(1,3),(5,1),(3,2)); type TART=record cost:longint; father:TCOORD3; optimized:boolean; exists:boolean; end; TMATRIX=array[1..8,1..8,1..24]of TART; const SCOORD:STRING[8]='abcdefgh'; const ZERO:TCOORD=(x:0;y:0); ZERO3:TCOORD3=(x:0;y:0;k:0); var startpoz,endpoz:TCOORD; face:TFACE; m:TMATRIX; procedure ReadCoord(var poz:TCOORD); var c:char; begin read(c); poz.x:=pos(c,SCOORD); read(poz.y); read(c); end; procedure ReadFace(var f:TFACE); var i:byte; begin for i:=1 to NFACES do read(f[i]); end; procedure read_data; begin ReadCoord(startpoz); ReadCoord(endpoz); ReadFace(face); end; procedure init_data; begin fillchar(m,sizeof(m),0); m[startpoz.x,startpoz.y,1].cost:=face[BOTTOM]; m[startpoz.x,startpoz.y,1].father:=ZERO3; m[startpoz.x,startpoz.y,1].optimized:=false; m[startpoz.x,startpoz.y,1].exists:=true; end; function SOLVED:boolean; var i:byte; begin SOLVED:=false; for i:=1 to 24 do if m[endpoz.x,endpoz.y,i].optimized=false then exit; SOLVED:=true; end; procedure FindMinCost(var coord3:TCOORD3); var i,j,k:byte; cost:longint; begin coord3:=ZERO3; cost:=maxlongint; for i:=1 to 8 do for j:=1 to 8 do for k:=1 to 24 do if (m[i,j,k].exists) and (not m[i,j,k].optimized)and (m[i,j,k].cost < cost) then begin cost:=m[i,j,k].cost; coord3.x:=i; coord3.y:=j; coord3.k:=k; end; end; function IsZero(c:TCOORD):boolean; begin IsZero:=(c.x=0)and(c.y=0); end; function IsZero3(c:TCOORD3):boolean; begin IsZero3:=(c.x=0)and(c.y=0)and(c.k=0); end; function Inside(x,y:shortint):boolean; begin Inside:=(x>0)and(x<9)and(y>0)and(y<9); end; procedure MakeMove(var f:tfaces;move:shortint); var f2:tfaces; i:shortint; begin for i:=1 to NFACES do f2[i]:=f[ROLES[move,i]]; f:=f2; end; function FindPerm(f:tfaces):shortint; var i:shortint; begin for i:=1 to 24 do if (BR[i,1]=f[BOTTOM]) and (BR[i,2]=f[RIGHT]) then begin FindPerm:=i; exit; end; end; function MovePermutation(oldp,move:shortint):shortint; var f:tfaces; i:shortint; begin f:=PERMUTATIONS[oldp]; MakeMove(f,move); MovePermutation:=FindPerm(f); end; procedure OptimizeCoord(coord:TCOORD3); var i:byte; nx,ny,nk:shortint; ccost:longint; ncost:longint; begin with coord do
I FIXED IT!! It runs in 0.04 secs, wow! the fixed part is a stupid constant: const BR:TBR=((5,4),(6,5),(3,6),(4,3),(5,6), (6,3),(3,4),(4,5),(2,6),(1,4), (6,1),(4,2),(2,3),(1,5),(5,2), (3,1),(2,4),(1,6),(6,2),(4,1), (2,5),(1,3),(5,1),(3,2)); |