洛谷 P1443 马的遍历

题目描述

有一个n*m的棋盘(1

type
  node=record
    x,y,b:longint;
  end;

var a,b:array[1..1000,1..1000]of longint;
  list:array[1..50000]of node;
  f:array[1..8,1..2]of longint=((2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2));
  n,m,i,j,qx,qy:longint;
  t:ansistring;

procedure bfs(x,y:longint);
var hx,hy:longint;
  h,t,k:longint;
begin
  h:=1;t:=1;
  list[1].b:=0;
  list[1].x:=x;
  list[1].y:=y;
  while (h<=t) do
   begin
    for k:=1 to 8 do
     begin
      hx:=list[h].x+f[k,1];
      hy:=list[h].y+f[k,2];
      if(hx>=1) and (hx<=n) and (hy>=1) and (hy<=m) and (b[hx,hy]=0)then begin
        inc(t);
        b[hx,hy]:=1;
        list[t].x:=hx;
        list[t].y:=hy;
        list[t].b:=list[h].b+1;
        a[hx,hy]:=list[t].b;
      end;
    end;
    inc(h);
  end;
  exit;
end;

begin
  readln(n,m,qx,qy);
  fillchar(b,sizeof(b),0);
  bfs(qx,qy);

  for i:=1 to n do begin
  for j:=1 to m do begin
    if(a[i,j]=0)then
    a[i,j]:=-1;
  end;

  a[qx,qy]:=0;

  end;
  for i:=1 to n do begin
  for j:=1 to m do begin
     str(a[i,j],t);
     write(t,'':5-length(t));
   end;
      writeln;
   end;
end.

你可能感兴趣的:(暴力)