USACO2.1.1--The Castle

The Castle
IOI'94 - Day 1

In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.

Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.

Your task is to help Farmer John know the exact room count and sizes.

The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their "outer edges" to keep out the wind and rain.

Consider this annotated floorplan of a castle:

     1   2   3   4   5   6   7
   #############################
 1 #   |   #   |   #   |   |   #
   #####---#####---#---#####---#   
 2 #   #   |   #   #   #   #   #
   #---#####---#####---#####---#
 3 #   |   |   #   #   #   #   #   
   #---#########---#####---#---#
 4 # ->#   |   |   |   |   #   #   
   ############################# 

#  = Wall     -,|  = No wall
-> = Points to the wall to remove to
     make the largest possible new room

By way of example, this castle sits on a 7 x 4 base. A "room" includes any set of connected "squares" in the floor plan. This floorplan contains five rooms (whose sizes are 9, 7, 3, 1, and 8 in no particular order).

Removing the wall marked by the arrow merges a pair of rooms to make the largest possible room that can be made by removing a single wall.

The castle always has at least two rooms and always has a wall that can be removed.

PROGRAM NAME: castle

INPUT FORMAT

The map is stored in the form of numbers, one number for each module, M numbers on each of N lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.

Each module number tells how many of the four walls exist and is the sum of up to four integers:

  • 1: wall to the west
  • 2: wall to the north
  • 4: wall to the east
  • 8: wall to the south

Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1.

Line 1: Two space-separated integers: M and N
Line 2..: M x N integers, several per line.

SAMPLE INPUT (file castle.in)

7 4
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

OUTPUT FORMAT

The output contains several lines:

Line 1: The number of rooms the castle has.
Line 2: The size of the largest room
Line 3: The size of the largest room creatable by removing one wall
Line 4: The single wall to remove to make the largest room possible

Choose the optimal wall to remove from the set of optimal walls by choosing the module farthest to the west (and then, if still tied, farthest to the south). If still tied, choose 'N' before 'E'. Name that wall by naming the module that borders it on either the west or south, along with a direction of N or E giving the location of the wall with respect to the module.

SAMPLE OUTPUT (file castle.out)

5
9
16
4 1 E
题解:Flood fill。用种子填充法计算出有多少个连通块,并记录最大的连通块中成员总数。其中判断周围是否有墙可以用位运算(哈哈,终于用到了),用这种方法判断好神奇啊!!!拆墙的话,从左下向右上枚举,先判断正西方向有否有墙,再判断正南方向是否有墙,记录拆墙之后两个连通块之和的最大值,以及行,列,方向。
偶滴代码o(╯□╰)o
View Code
 1 /*
 2 ID:spcjv51
 3 PROG:castle
 4 LANG:C
 5 */
 6 #include<stdio.h>
 7 #include<stdlib.h>
 8 int a[55][55],visit[55][55],all[3000];
 9 int m,n,total,ans,max,sum,row,col;
10 char s;
11 void floodfill(int x,int y)
12 {
13     if(visit[x][y]) return;
14     visit[x][y]=ans;
15     total++;
16     if(!(a[x][y]&1))floodfill(x,y-1);
17     if(!(a[x][y]&2))floodfill(x-1,y);
18     if(!(a[x][y]&4))floodfill(x,y+1);
19     if(!(a[x][y]&8))floodfill(x+1,y);
20 }
21 
22 
23 int main(void)
24 {
25     freopen("castle.in","r",stdin);
26     freopen("castle.out","w",stdout);
27     int i,j;
28     scanf("%d%d",&m,&n);
29     memset(visit,0,sizeof(visit));
30     memset(all,0,sizeof(all));
31     for(i=0; i<n; i++)
32         for(j=0; j<m; j++)
33             scanf("%d",&a[i][j]);
34     ans=0;
35     max=0;
36     for(i=0; i<n; i++)
37         for(j=0; j<m; j++)
38             if(visit[i][j]==0)
39             {
40                 ans++;
41                 total=0;
42                 floodfill(i,j);
43                 all[ans]=total;
44                 if(total>max) max=total;
45 
46             }
47     printf("%d\n",ans);
48     printf("%d\n",max);
49     sum=0;
50     for(j=0; j<m; j++)
51         for(i=n-1; i>=0; i--)
52         {
53             if(j-1>=0&&visit[i][j-1]!=visit[i][j])
54                 if(all[visit[i][j]]+all[visit[i][j-1]]>sum)
55                 {
56                     sum=all[visit[i][j]]+all[visit[i][j-1]];
57                     s='E';
58                     row=i+1;
59                     col=j;
60                 }
61             if(i+1<n&&visit[i+1][j]!=visit[i][j])
62                 if(all[visit[i][j]]+all[visit[i+1][j]]>sum)
63                 {
64                     sum=all[visit[i][j]]+all[visit[i+1][j]];
65                     s='N';
66                     row=i+2;
67                     col=j+1;
68                 }
69 
70         }
71     printf("%d\n",sum);
72     printf("%d %d %c\n",row,col,s);
73     return 0;
74 }

 

posted on 2013-02-02 18:40  仗剑奔走天涯  阅读(280)  评论(0编辑  收藏  举报

导航