USACO The Castle Eventually Accepted

一个简单的floodfill花了几个晚上来调试,数据几乎是一组一组过的,各种各样稀奇古怪的bug都出来了……狂汗!
不过终于……hoho~
思路就是floodfill每一个未访问的格子,fill过程中顺便标号(房间号,同时也代表该格已访问过),每次队列空了(也就是访问完了一个房间)返回房间面积。记录最大的面积。然后从最西南格子开始考察拆掉墙(东或北面)以后的面积(墙两侧格子所在房间之和),记下最大的那个输出。如果发生冲突,要注意题目给出的拆墙顺序(让我郁闷好久的问题)。
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 wall farthest to the west (and then, if still tied, farthest to the south). 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
 
以下是代码,保留了调试时候用的函数,用#define debug开关
 
 
/*
TASK:castle
ID:danmarn1
LANG:C
*/

#include
< stdio.h >
#include
< stdbool.h >
#define  DEBUG  // switch
#ifdef DEBUG 
void  view( int  x, int  y, int  area);
#endif
typedef 
struct   // 储存每个房间的墙及该房间编号 
{
        
bool dir[4];
        
int rn;
}
Room;
Room rooms[
50 ][ 50 ];
int  tm,tn;
int  rsize[ 2501 ];  // room size 

typedef 
struct  xy
{
        
int x,y;
}
XY;
XY d[
4 ] = {{0,1},{1,0},{0,-1},{-1,0}} // 方向坐标 
XY Q[ 2500 ]; // queue

int  front =- 1 ,rear =- 1 ; // pointer to Q


void  build(Room  * r, int  t);
int  main( void )
{
        
int m,n;
        
int i,j,t,rarea[2]={-1,-1},max=0,currn=0,direction[2]={'E','N'},area;
        XY thewall[
2];
        
int the;
        FILE 
*fin=fopen("castle.in","r");
        FILE 
*fout=fopen("castle.out","w");
        fscanf(fin,
"%d %d ",&m,&n);
        tm
=m;tn=n;
        
for(i=0;i<n;i++){
                
for (j=0;j<m;j++){
                        fscanf(fin,
"%d",&t);
                        build(
&rooms[i][j],t);
                }

                fscanf(fin,
" ");
        }

#ifdef DEBUG
        
for (i=0;i<n;i++){
                
for(j=0;j<m;j++)
                        printf(
"%d%d%d%d ",rooms[i][j].dir[0],rooms[i][j].dir[1],
                                rooms[i][j].dir[
2],rooms[i][j].dir[3]);
                printf(
" ");
        }

        system(
"pause");
#endif

        
for(i=0;i<n;i++)
                
for(j=0;j<m;j++)
                        
if(rooms[i][j].rn==0){
                                currn
++;
                                rsize[currn]
=count(i,j,currn);
                                
if (max<rsize[currn]) max=rsize[currn];
                        }

        
for(i=0;i<m-1;i++)
                
for(j=n-1;j>=0;j--)
                        
if(rooms[j][i].rn!=rooms[j][i+1].rn){
                                
if(rsize[rooms[j][i].rn]+rsize[rooms[j][i+1].rn]>rarea[0]){
                                        rarea[
0]=rsize[rooms[j][i].rn]+rsize[rooms[j][i+1].rn];
                                        thewall[
0].x=i;
                                        thewall[
0].y=j;
                                }

                        }

        
for(i=0;i<m;i++)
                
for(j=n-1;j>0;j--)
                        
if(rooms[j][i].rn!=rooms[j-1][i].rn){
                                
if(rsize[rooms[j][i].rn]+rsize[rooms[j-1][i].rn]>rarea[1]){
                                        rarea[
1]=rsize[rooms[j][i].rn]+rsize[rooms[j-1][i].rn];
                                        thewall[
1].x=i;
                                        thewall[
1].y=j;
                                }

                        }

        
if(rarea[0]==rarea[1]){
                
if(thewall[0].x<thewall[1].x) the=0;
                
else if(thewall[0].x>thewall[1].y) the=1;
                
else {
                        
if (thewall[0].y>thewall[1].y) the=0;
                        
else the=1;
                }

        }
else if(rarea[0]>rarea[1]) the=0;
        
else the=1;

        fprintf(fout,
"%d %d %d %d %d %c ",currn,max,rarea[the],thewall[the].y+1,thewall[the].x+1,direction[the]);
#ifdef DEBUG
        system(
"pause");
#endif
        
return 0;
}

int  count( int  i, int  j, int  c)
{
        
int area=0,k;
        
int x,y,yt,xt;
        rear
=front=-1;//initialize Q
        Q[++rear].x=j;
        Q[rear].y
=i;
        rooms[i][j].rn
=c;
        
while (front!=rear){
                x
=Q[++front].x;
                y
=Q[front].y;
                area
++;
#ifdef DEBUG
                view(x,y,area);
#endif
                
for (k=0;k<4;k++){
                        
if(y+d[k].y<0 || x+d[k].x<0continue;
                        
else {
                                yt
=y+d[k].y;
                                xt
=x+d[k].x;
                        }

                        
if(rooms[y][x].dir[k]==false && rooms[yt][xt].rn==0){
                                Q[
++rear].x=xt;
                                Q[rear].y
=yt;
                                rooms[yt][xt].rn
=c;
                        }

                }
       
        }

        
return area;
}

void  build(Room  * r, int  t)
{
        
if(t-8>=0{
                t
-=8;
                r
->dir[0]=true;
        }

        
if(t-4>=0{
                t
-=4;
                r
->dir[1]=true;
        }

        
if(t-2>=0{
                t
-=2;
                r
->dir[2]=true;
        }

        
if(t-1>=0){
                t
-=3;
                r
->dir[3]=true;
        }

}

#ifdef DEBUG
void  view( int  x, int  y, int  area)
{
        
int i,j;
        
for(i=0;i<tn;i++){
                
for (j=0;j<tm;j++
                        
if(i==y&&j==x) printf("#");
                        
else printf("%d",rooms[i][j].rn);
                printf(
" ");
        }

        printf(
"x=%d y=%d area=%d Queue Front: ",x,y,area);
        
for (i=front;i<=rear;i++) printf("(%d,%d)",Q[i].x,Q[i].y);

        printf(
" Rear ");
        system(
"pause");
}

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值