castle.pas
来自「Magio牛的usaco源代码」· PAS 代码 · 共 80 行
PAS
80 行
{
ID:maigoak1
PROG:castle
}
program castle;
const
maxsize=50;
var
fin,fout:text;
map:array[1..maxsize,1..maxsize]of 0..15;{Map of the castle}
com:array[1..maxsize,1..maxsize]of integer;{The component each square is in}
size:array[1..maxsize*maxsize]of integer;{Size of each component}
m,n,coms,max,digx,digy,i,j:integer;
dir:char;
function nowall(x,y:integer;dir:char):boolean;
begin
case dir of
's':if map[x,y]>7 then nowall:=false else nowall:=true;
'e':if odd(map[x,y] div 4) then nowall:=false else nowall:=true;
'n':if odd(map[x,y] div 2) then nowall:=false else nowall:=true;
'w':if odd(map[x,y]) then nowall:=false else nowall:=true;
end;
end;
procedure floodfill(x,y:integer);
begin
if com[x,y]>0 then exit;
com[x,y]:=coms;
inc(size[coms]);
if nowall(x,y,'s') then floodfill(x+1,y);
if nowall(x,y,'e') then floodfill(x,y+1);
if nowall(x,y,'n') then floodfill(x-1,y);
if nowall(x,y,'w') then floodfill(x,y-1);
end;
begin
assign(fin,'castle.in');
reset(fin);
readln(fin,m,n);
for i:=1 to n do
for j:=1 to m do
read(fin,map[i,j]);
close(fin);
coms:=0;max:=0;
fillchar(com,sizeof(com),0);
assign(fout,'castle.out');
rewrite(fout);
for i:=1 to n do
for j:=1 to m do
if com[i,j]=0 then begin
inc(coms);
size[coms]:=0;
floodfill(i,j);
if size[coms]>max then max:=size[coms];
end;
writeln(fout,coms);
writeln(fout,max);
max:=0;
for i:=1 to m do begin
for j:=n downto 2 do
if com[j,i]<>com[j-1,i] then
if size[com[j,i]]+size[com[j-1,i]]>max then begin
max:=size[com[j,i]]+size[com[j-1,i]];
digx:=j;digy:=i;dir:='N';
end;
if i=m then break;
for j:=n downto 1 do
if com[j,i]<>com[j,i+1] then
if size[com[j,i]]+size[com[j,i+1]]>max then begin
max:=size[com[j,i]]+size[com[j,i+1]];
digx:=j;digy:=i;dir:='E';
end;
end;
writeln(fout,max);
writeln(fout,digx,' ',digy,' ',dir);
close(fout);
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?