关闭

迷宫

114人阅读 评论(0) 收藏 举报
分类:
program migong;
type node=record
          xx,yy:longint;
     end;
     all=array[1..10,1..10]of longint;
 const n=400;
 dx:array[1..4]of longint=(0,1,0,-1);
 dy:array[1..4]of longint=(1,0,-1,0);
 a:all=((0,0,0,0,0,0,1,0,1,1),
        (0,1,0,1,1,0,0,0,0,0),
        (0,1,0,1,1,0,1,1,1,0),
        (0,1,0,0,0,0,0,1,0,0),
        (0,1,1,1,1,1,1,1,1,1),
        (0,0,0,1,0,1,0,0,0,0),
        (0,1,1,1,0,0,0,1,1,1),
        (0,0,0,0,0,1,0,0,0,0),
        (1,1,0,1,1,1,0,1,1,0),
        (1,1,1,1,1,1,0,1,1,1));
 var head,tail,k,x1,y1,x2,y2,nx,ny:longint;
     state:array[1..n]of node;
     father:array[1..n]of longint;
 function check:boolean;
 begin
  check:=false;
  nx:=state[head].xx+dx[k];ny:=state[head].yy+dy[k];
  if (nx<1)or(nx>10)or(ny<1)or(ny>10) then
     check:=false
      else if a[ny,nx]=1 then check:=false
       else check:=true;
 end;
 function dupe:boolean;
 var i,j:longint;buf:boolean;
 begin
  buf:=false;i:=0;
  repeat
   inc(i);
   if (state[i].xx=state[tail].xx)and(state[i].yy=state[tail].yy)then
   buf:=true else buf:=false
    until buf or(i>=tail-1);
   dupe:=buf;
   end;
   procedure print;
   var buf:array[1..n]of longint;
   i,j,m:longint;
   begin
    m:=1;j:=tail;buf[1]:=j;
    while j>0 do
     begin
      j:=father[j];
      inc(m);buf[m]:=j;
     end;
    for i:=m-1 downto 2 do
      write('(',state[buf[i]].xx,',',state[buf[i]].yy,')','--');
      writeln('(',state[buf[1]].xx,',',state[buf[1]].yy,')');
    halt;
 end;
begin
 x1:=8;y1:=1;x2:=7;y2:=10;
 state[1].xx:=x1;
 state[1].yy:=y1;
 head:=1;tail:=1;father[head]:=0;
 repeat
  for k:=1 to 4 do
   if check then
    begin
     inc(tail);
     father[tail]:=head;
     state[tail].xx:=nx;
     state[tail].yy:=ny;
     if dupe then dec(tail)
      else if (state[tail].xx=x2)and(state[tail].yy=y2)
           then print;
   end;
 head:=head+1;
 until head>tail;
end.
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14875次
    • 积分:106
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档