Guest User

Untitled

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.66 KB | None | 0 0
  1. {$APPTYPE CONSOLE}
  2. uses SysUtils;
  3.  
  4. type
  5.    cell=record
  6.       x,y:integer;
  7.    end;
  8.  
  9. const
  10.    v:array[1..8,1..2] of integer=((1,2),(2,1),(-1,2),(2,-1),(1,-2),(-2,1),(-1,-2),(-2,-1));
  11.  
  12. var
  13.    x,y,x1,last,first,i,j:integer;
  14.    s:string;
  15.    p:cell;
  16.    queue:array[1..64] of cell;
  17.    was:array[1..8,1..8] of boolean;
  18.    from:array[1..8,1..8] of cell;
  19.    answer:array[1..64] of record
  20.       x:char;
  21.       y:integer;
  22.    end;
  23.  
  24.  
  25. procedure add(x,y:integer;p:cell);
  26. begin
  27.    last:=last+1;
  28.    queue[last].x:=x;
  29.    queue[last].y:=y;
  30.  
  31.    was[x,y]:=true;
  32.  
  33.    from[x,y].x:=p.x;
  34.    from[x,y].y:=p.y;
  35. end;
  36.  
  37. function empty:boolean;
  38. begin
  39.    result:=last<first;
  40. end;
  41.  
  42. function pop:cell;
  43. begin
  44.    result:=queue[first];
  45.    first:=first+1;
  46. end;
  47.  
  48. function ok(x,y:integer):boolean;
  49. begin
  50.    result:=(x>0) and (x<9) and (y>0) and (y<9) and not was[x,y];
  51. end;
  52.  
  53.  
  54. begin
  55.    reset(input,'knight.in');
  56.    rewrite(output,'knight.out');
  57.    last:=0;
  58.    first:=1;
  59.  
  60.    for i:=1 to 8 do
  61.       for j:=1 to 8 do
  62.          was[i,j]:=false;
  63.  
  64.    readln(s);
  65.    x:=ord(s[1])-ord('a')+1;
  66.    y:=strtoint(s[2]);
  67.    p.x:=-1;
  68.    p.y:=-1;
  69.    add(x,y,p);
  70.  
  71.    readln(s);
  72.    x:=ord(s[1])-ord('a')+1;
  73.    y:=strtoint(s[2]);
  74.    
  75.    while not empty do begin
  76.       p:=pop;
  77.       for i:=1 to 8 do
  78.          if ok(p.x+v[i][1],p.y+v[i][2]) then
  79.             add(p.x+v[i][1],p.y+v[i][2],p);
  80.    end;
  81.  
  82.  
  83.    last:=0;
  84.    while (x>-1) and (y>-1) do begin
  85.       last:=last+1;
  86.       answer[last].x:=chr(x+ord('a')-1);
  87.       answer[last].y:=y;
  88.       x1:=from[x,y].x;
  89.       y:=from[x,y].y;
  90.       x:=x1;
  91.    end;
  92.  
  93.    for i:=last downto 1 do
  94.       writeln(answer[i].x,answer[i].y);
  95. end.
Add Comment
Please, Sign In to add comment