在一个n m 格子的棋盘上,有一只国际象棋的骑士在棋盘的左下角 (1;1)(如图1),骑士只能根据象棋的规则进行移动,要么横向跳动一格纵向跳动两格,要么纵向跳动一格横向跳动两格。 例如, n=4,m=3 时,若骑士在格子(2;1) (如图2), 则骑士只能移入下面格子:(1;3),(3;3) 或 (4;2);对于给定正整数n,m,I,j值 (m,n<=50,I<=n,j<=m) ,你要测算出从初始位置(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
tail:=tail+1;
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;
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;
readln(n,m);
read(w[1],w[2]);
bfs;
writeln('NEVER');
end.