码迷,mamicode.com
首页 > 其他好文 > 详细

USACO 2.1.1 The Castle

时间:2015-02-22 07:50:51      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

{
ID:anniel11
PROG:castle
LANG:PASCAL
}
var
    a:array[0..50,0..50  ,1..4] of boolean;
    component:array[0..50,0..50] of integer;//which room does it belong to
    room_size:array[0..2500] of integer;
    neighbour:array[0..2500,0..2500] of boolean;
    m,n,i,j,temp,cmp_num,max_size,r_size,r_i,r_j,r_direction:integer;

procedure process(x,y,k:integer);
begin
    if (k and 1)<>1 then a[x,y,1]:=true;//W  left
    if (k and 2)<>2 then a[x,y,2]:=true;//N  up
    if (k and 4)<>4 then a[x,y,3]:=true;//E  right
    if (k and 8)<>8 then a[x,y,4]:=true;//S  down
end;
procedure floodfill(k,i,j:integer);
begin
   if (a[i,j,3])and(component[i,j+1]=0) then
        begin
            component[i,j+1]:=k;
            inc(room_size[k]);
            if room_size[k]>max_size then max_size:=room_size[k];
            floodfill(k,i,j+1);
        end;
   if (a[i,j,4])and(component[i+1,j]=0) then
        begin
            component[i+1,j]:=k;
            inc(room_size[k]);
            if room_size[k]>max_size then max_size:=room_size[k];
            floodfill(k,i+1,j);
        end;
   if (a[i,j,2])and(component[i-1,j]=0) then
        begin
            component[i-1,j]:=k;
            inc(room_size[k]);
            if room_size[k]>max_size then max_size:=room_size[k];
            floodfill(k,i-1,j);
        end;
   if (a[i,j,1])and(component[i,j-1]=0) then
        begin
            component[i,j-1]:=k;
            inc(room_size[k]);
            if room_size[k]>max_size then max_size:=room_size[k];
            floodfill(k,i,j-1);
        end;
end;

begin
    assign(input,‘castle.in‘);
    reset(input);
    assign(output,‘castle.out‘);
    rewrite(output);

    readln(m,n);
    fillchar(a,sizeof(a),false);//true=reachable
    fillchar(component,sizeof(component),0);
    fillchar(neighbour,sizeof(neighbour),false);
    for i:=1 to n do
    begin
        for j:=1 to m do
        begin
            read(temp);
            process(i,j,temp);
        end;
        readln;
    end;

    for i:=1 to n do
    for j:=1 to m do
    if component[i,j]=0 then
    begin
      inc(cmp_num);
      component[i,j]:=cmp_num;
      inc(room_size[cmp_num]);
      if room_size[cmp_num]>max_size then max_size:=room_size[cmp_num];
      floodfill(cmp_num,i,j);
    end;
    for j:=1 to m do
    for i:=n downto 1 do
    begin
        if i<>1 then if component[i,j]<>component[i-1,j] then
        if room_size[component[i,j]]+room_size[component[i-1,j]]>r_size then
        begin
            r_size:=room_size[component[i,j]]+room_size[component[i-1,j]];
            r_i:=i;
            r_j:=j;
            r_direction:=1; //up
        end;
        if j<>m then if component[i,j]<>component[i,j+1] then
        if room_size[component[i,j]]+room_size[component[i,j+1]]>r_size then
        begin
            r_size:=room_size[component[i,j]]+room_size[component[i,j+1]];
            r_i:=i;
            r_j:=j;
            r_direction:=2; //right
        end;
    end;
    for i:=1 to n do
    begin
    for j:=1 to m do write(component[i,j],‘ ‘);
    writeln;
    end;
    writeln(cmp_num);
    writeln(max_size);
    writeln(r_size);
    write(r_i,‘ ‘,r_j,‘ ‘);
    if r_direction=1 then writeln(‘N‘) else writeln(‘E‘);
    close(input);
    close(output);
end.

  

USACO 2.1.1 The Castle

标签:

原文地址:http://www.cnblogs.com/OmegaIota/p/4297302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!