比赛 20110412pm 评测结果 AAAAAAWAAA
题目名称 拯救奶牛贝希 最终得分 90
用户昵称 ybh 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-04-12 16:51:12
显示代码纯文本
program rescuea;
var
  n,m,si,sj,ti,tj,i,temp,min,mini,minj:longint;

function dist(i1,j1,i2,j2:longint):longint;
var
  h,ans:longint;
begin
  h:=i2-i1;
  if h=0 then exit(abs(j1-j2));
  if j1 mod 2=1 then
  begin
    ans:=(h-1)*2+1;
    if j2<j1+1 then exit(ans+j1+1-j2);
    if j2>j1+2*h-1 then exit(ans+j2-(j1+2*h-1));
    if j2 mod 2=0
      then exit(ans)
      else exit(ans+1);
  end
  else
  begin
    ans:=h*2;
    if j2<j1 then exit(ans+j1-j2);
    if j2>j1+(h-1)*2 then exit(ans+j2-(j1+(h-1)*2));
    if j2 mod 2=0
      then exit(ans)
      else exit(ans+1);
  end;
end;

begin
  assign(input,'rescuea.in');
  reset(input);
  assign(output,'rescuea.out');
  rewrite(output);

  readln(n,m);
  readln(si,sj);
  min:=maxlongint;
  for i:=1 to m do
  begin
    readln(ti,tj);
    if si<=ti
      then temp:=dist(si,sj,ti,tj)
      else temp:=dist(ti,tj,si,sj);
    if temp=min then
      if (ti<mini) or ((ti=mini) and (tj<minj)) then
      begin
        min:=temp;
        mini:=ti;
        minj:=tj;
      end;
    if temp<min then
    begin
      min:=temp;
      mini:=ti;
      minj:=tj;
    end;
  end;

  writeln(mini,' ',minj);
  writeln(min+1);
  close(input);
  close(output);
end.