记录编号 7981 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 奥术能量环流 最终得分 100
用户昵称 Gravatarfrancis 是否通过 通过
代码语言 Pascal 运行时间 1.816 s
提交时间 2008-11-12 16:39:27 内存使用 0.29 MiB
显示代码纯文本
program arcane;
const
fin='arcane.in';
fou='arcane.out';
g:array[1..4,1..2]of longint=((-1,0),(1,0),(0,-1),(0,1));
var
a,b:array[1..100,1..100,1..4]of boolean;
p,q,bo:array[1..100,1..100]of boolean;
d:array[1..10000,1..2]of longint;
light,all,total,ii,jj,k,x,y,i,j,n,m:longint;
f1,f2:text;

procedure init;
begin
assign(f1,fin);
assign(f2,fou);
reset(f1); rewrite(f2);
read(f1,n,m);
for i:=1 to n do
for j:=1 to m do
begin
 read(f1,x);
 if x=0 then bo[i,j]:=true;
 k:=4;
 while (k>0)and(x>0) do
 begin
  y:=x mod 2;
  x:=x div 2;
  if y=1 then a[i,j,k]:=true;
  dec(k);
 end;
end;
for i:=1 to n do
for j:=1 to m do
for k:=1 to 4 do
if a[i,j,k]=true then
begin
 x:=i+g[k,1]; y:=j+g[k,2];
 if (x>0)and(x<=n)and(y>0)and(y<=m) then
 case k of
  1: b[x,y,2]:=true;
  2: b[x,y,1]:=true;
  3: b[x,y,4]:=true;
  4: b[x,y,3]:=true;
 end else a[i,j,k]:=false;
end;
end;

procedure bfs1(x,y:longint);
var
i,j,xx,yy:longint;
begin
for i:=x to y do
for j:=1 to 4 do
if a[d[i,1],d[i,2],j]=true then
 begin
   xx:=d[i,1]+g[j,1]; yy:=d[i,2]+g[j,2];
   if (p[xx,yy]=false)and(bo[xx,yy]=false) then
    begin
      p[xx,yy]:=true;
      inc(light); d[light,1]:=xx; d[light,2]:=yy;
    end;
 end;
if light>=y+1 then bfs1(y+1,light);
end;

procedure bfs2(x,y:longint);
var
i,j,xx,yy:longint;
begin
for i:=x to y do
for j:=1 to 4 do
if b[d[i,1],d[i,2],j]=true then
 begin
   xx:=d[i,1]+g[j,1]; yy:=d[i,2]+g[j,2];
   if (q[xx,yy]=false)and(bo[xx,yy]=false) then
     begin
      q[xx,yy]:=true;
      inc(light); d[light,1]:=xx; d[light,2]:=yy;
      if p[xx,yy]=true then begin inc(all); bo[xx,yy]:=true; end;
     end;
 end;
if light>=y+1 then bfs2(y+1,light);
end;


procedure main;
begin
for i:=1 to n do
for j:=1 to m do
if bo[i,j]=false then
begin
 for ii:=1 to n do
 for jj:=1 to m do
 begin
  p[ii,jj]:=false;
  q[ii,jj]:=false;
 end;
 bo[i,j]:=true;
 p[i,j]:=true; light:=1; d[light,1]:=i; d[light,2]:=j;
 bfs1(1,1);
 q[i,j]:=true; light:=1; d[light,1]:=i; d[light,2]:=j; all:=1;
 bfs2(1,1);
 if all>1 then inc(total);
end;
end;

begin
init;
main;
write(f2,total);
close(f1); close(f2);
end.