骑士旅行

SSLGZ-1456-骑士旅行

题意:

在一个n*m (m,n<=50)格子的棋盘上,请你要测算出从初始位置(1,1) 到格子(I,j)最少需要多少次移动,如果始终不能就输出‘NEVER’.
这里写图片描述

解题思路:

先判断哪个格子能跳(有没有出界),统计步数,如果找到了目标点就输出从起点到这的最少的步数。

var
  i,j,k,l,n,m,ans:longint;
  a:array[0..10000,0..10000] of longint;
  s:array[0..10000,1..3]of longint;
  f:array[0..20000] of longint;
  w:array[1..2] of longint;
  fx:array[1..8,1..2] of longint;
procedure bfs;
var
  head,tail,k,i,x,y:longint;
begin
  f[1]:=0;
  s[1,1]:=1;
  s[1,2]:=1;
  s[1,3]:=0;
  head:=0;
  tail:=1;
  repeat
    head:=head+1;
    for i:=1 to 8 do
    begin
      x:=s[head,1]+fx[i,1];
      y:=s[head,2]+fx[i,2];
      if (x>=1) and (x<=n) and (y>=1) and (y<=m) and (a[x,y]=0) then
        begin
          inc(tail);
          f[tail]:=head;
          s[tail,1]:=x;
          s[tail,2]:=y;
          a[x,y]:=1;
          s[tail,3]:=s[head,3]+1;
          if (x=w[1]) and (y=w[2]) then
            begin
              ans:=s[head,3]+1;
              writeln(ans);
              halt;
            end;
        end;
    end;
  until head>=tail;
end;

procedure init;
begin
  fx[1,1]:=1; fx[1,2]:=2;
  fx[2,1]:=2; fx[2,2]:=1;
  fx[3,1]:=2; fx[3,2]:=-1;
  fx[4,1]:=1; fx[4,2]:=-2;
  fx[5,1]:=-1; fx[5,2]:=-2;
  fx[6,1]:=-2; fx[6,2]:=-1;
  fx[7,1]:=-2; fx[7,2]:=1;
  fx[8,1]:=-1; fx[8,2]:=2;
end;

begin
  init;
  readln(n,m);
  read(w[1],w[2]);
  bfs;
  writeln('NEVER');
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值