USACO 2.1.1 The Castle

{
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.

  

时间: 2024-08-06 20:05:24

USACO 2.1.1 The Castle的相关文章

【USACO 2.1】The Castle

/* TASK: castle LANG: C++ SOLVE: 深搜,注意每个方向对应值.枚举去掉的墙,然后再dfs,注意墙要复原,并且dfs里要判断是否超出边界. */ #include<cstdio> #include<algorithm> #include<cstring> #define N 55 using namespace std; int n,m; int a[N][N]; int ans,num,cnt; int rans,rm,d; char di

USACO Section 2.1 The Castle

/* ID: lucien23 PROG: castle LANG: C++ */ /************************************************************************/ /* 求图的连通域问题.利用广度扫描 */ /************************************************************************/ #include <iostream> #include <fs

poj练习题的方法

poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1077--Eightpoj1084--Square Destroyerpoj1085--Triangle War(博弈,極大極小搜索+alpha_beta剪枝)poj1088--滑雪poj1129--Channel Allocation 着色问题 dfspoj1154--letters (dfs)p

POJ 搜索题集

poj1010--邮票问题 DFS poj1011--Sticks dfs + 剪枝 poj1020--拼蛋糕 poj1054--The Troublesome Frog poj1062--昂贵的聘礼 poj1077--Eight poj1084--Square Destroyer poj1085--Triangle War(博弈,極大極小搜索+alpha_beta剪枝) poj1088--滑雪 poj1129--Channel Allocation 着色问题 dfs poj1154--lett

USACO castle

<pre name="code" class="cpp"><pre>USER: Kevin Samuel [kevin_s1] TASK: castle LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.003 secs, 3688 KB] Test 2: TEST OK [0.005 secs, 3688 KB] Test 3: TEST OK [0.008

USACO Section2.1 The Castle 解题报告

castle解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 有N×M的矩阵,边框都是实际存在的“墙”.如下图: 1 2 3 4 5 6 7 ########################

USACO 2.1 The Castle

题目大意:给你一个城堡让你求有多少房间,最大房间有多大,敲掉一堵墙后最大的房间有多大,敲掉那座墙 思路:比较恶心的bfs题,反正就是bfs使劲敲 /*{ ID:a4298442 PROB:castle LANG:C++ } */ #include<iostream> #include<cstdio> #include<fstream> #include<queue> #include<algorithm> #define pii pair<

usaco The Castle

题目给了一个二维矩阵,矩阵的每个数字代表一个单位的面积,每个数字转换为二进制,这个四位二进制数的每一位,分别代表了自己的东南西北是否有墙. 题目求房间的数目,最大的自然房间的大小,拆掉某一堵墙之后的可能会造成某两个自然房间合并,求合成最大房间的面积,以及拆除的抢的坐标,以及位置 做法是,对单位面积进行染色,可以相互联通的区域就是一个房间. 再设一个数组Size表示染色为i的房间的大小, 然后枚举拆掉每堵墙之后的情况,合成的房间的大小就是自己和自己相邻的单位面积所代表的房间大小(Size中的值)相

[【USACO】The Castle(dfs+枚举)

思路很好像,卡了我很久的就是当最大房间一样的时候判断输出哪个的条件, = = 简直无情 /* ID: 18906421 LANG: C++ PROG: castle */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 55; int mat[maxn][maxn][2] = {0}; // 0 1 下 右 int n,m,vis[m