Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {$APPTYPE CONSOLE}
- uses SysUtils;
- type
- cell=record
- x,y:integer;
- end;
- const
- v:array[1..8,1..2] of integer=((1,2),(2,1),(-1,2),(2,-1),(1,-2),(-2,1),(-1,-2),(-2,-1));
- var
- x,y,x1,last,first,i,j:integer;
- s:string;
- p:cell;
- queue:array[1..64] of cell;
- was:array[1..8,1..8] of boolean;
- from:array[1..8,1..8] of cell;
- answer:array[1..64] of record
- x:char;
- y:integer;
- end;
- procedure add(x,y:integer;p:cell);
- begin
- last:=last+1;
- queue[last].x:=x;
- queue[last].y:=y;
- was[x,y]:=true;
- from[x,y].x:=p.x;
- from[x,y].y:=p.y;
- end;
- function empty:boolean;
- begin
- result:=last<first;
- end;
- function pop:cell;
- begin
- result:=queue[first];
- first:=first+1;
- end;
- function ok(x,y:integer):boolean;
- begin
- result:=(x>0) and (x<9) and (y>0) and (y<9) and not was[x,y];
- end;
- begin
- reset(input,'knight.in');
- rewrite(output,'knight.out');
- last:=0;
- first:=1;
- for i:=1 to 8 do
- for j:=1 to 8 do
- was[i,j]:=false;
- readln(s);
- x:=ord(s[1])-ord('a')+1;
- y:=strtoint(s[2]);
- p.x:=-1;
- p.y:=-1;
- add(x,y,p);
- readln(s);
- x:=ord(s[1])-ord('a')+1;
- y:=strtoint(s[2]);
- while not empty do begin
- p:=pop;
- for i:=1 to 8 do
- if ok(p.x+v[i][1],p.y+v[i][2]) then
- add(p.x+v[i][1],p.y+v[i][2],p);
- end;
- last:=0;
- while (x>-1) and (y>-1) do begin
- last:=last+1;
- answer[last].x:=chr(x+ord('a')-1);
- answer[last].y:=y;
- x1:=from[x,y].x;
- y:=from[x,y].y;
- x:=x1;
- end;
- for i:=last downto 1 do
- writeln(answer[i].x,answer[i].y);
- end.
Add Comment
Please, Sign In to add comment