记录编号 113246 评测结果 AAAAAAAAAAAAA
题目名称 [POI 1999] 积水 最终得分 100
用户昵称 Gravatarteacher 是否通过 通过
代码语言 Pascal 运行时间 0.011 s
提交时间 2014-07-20 14:12:39 内存使用 0.32 MiB
显示代码纯文本
const
c:array[1..4,1..2] of shortint=((1,0),(-1,0),(0,1),(0,-1));
var
x0,y0,x1,y1,n,m,j:byte;
i,x:integer;
h:array[1..100,1..100] of integer;
b:array[0..101,0..101] of boolean;
f:array[1..32676] of integer;
g:array[1..32676,1..2] of shortint;
ans:longint;
begin
 assign(input,'wod.in'); reset(input);
 assign(output,'wod.out'); rewrite(output);
 readln(n,m);
 for i:=1 to n do for j:=1 to m do read(h[i,j]);
 x:=10000;
 fillchar(b,sizeof(b),true);
 for i:=1 to n do begin b[i,0]:=false; b[i,m+1]:=false; end;
 for i:=1 to m do begin b[0,i]:=false; b[n+1,i]:=false; end;
 b[1,1]:=false;b[1,m]:=false; b[n,1]:=false;b[n,m]:=false;
 for i:=2 to n-1 do begin
  inc(x);
  b[i,1]:=false;
  f[x]:=f[h[i,1]];
  f[h[i,1]]:=x;
  g[x,1]:=i; g[x,2]:=1;
 end;
 for i:=2 to n-1 do if b[i,m] then begin
  inc(x);
  b[i,m]:=false;
  f[x]:=f[h[i,m]];
  f[h[i,m]]:=x;
  g[x,1]:=i; g[x,2]:=m;
 end;
 for i:=2 to m-1 do if b[1,i] then begin
  inc(x);
  b[1,i]:=false;
  f[x]:=f[h[1,i]];
  f[h[1,i]]:=x;
  g[x,1]:=1; g[x,2]:=i;
 end;
 for i:=2 to m-1 do if b[n,i] then begin
  inc(x);
  b[n,i]:=false;
  f[x]:=f[h[n,i]];
  f[h[n,i]]:=x;
  g[x,1]:=n; g[x,2]:=i;
 end;
 for i:=1 to 10000 do
  while f[i]>0 do begin
   x0:=g[f[i],1];
   y0:=g[f[i],2];
   f[i]:=f[f[i]];
   inc(ans,i-h[x0,y0]);
   for j:=1 to 4 do begin
    x1:=x0+c[j,1]; y1:=y0+c[j,2];
    if (b[x1,y1]) then
     if h[x1,y1]<i then begin
      b[x1,y1]:=false;
      inc(x);
      f[x]:=f[i];
      f[i]:=x;
      g[x,1]:=x1;
      g[x,2]:=y1;
     end
     else begin
      b[x1,y1]:=false;
      inc(x);
      f[x]:=f[h[x1,y1]];
      f[h[x1,y1]]:=x;
      g[x,1]:=x1;
      g[x,2]:=y1;
     end;
    end;
  end;
  writeln(ans);
 close(input); close(output);
end.