USACO 2.1.1 The Castle

一.题目描述

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



二.题目分析

   在本题中有三个部分需要处理,一是求解连通分量,可以使用DFS实现;二是记录每一连通分量中的元素的个数,可以使用全局的数组compont实现,只需在DFS前后稍作添加即可实现;三是求解连通分量的合并,我们可以使用枚举的方式,依次枚举(最大M,N才50)每一条可以推翻的墙来合并连通分量,在枚举之前我们首先要知道每一个方格(i,j)是属于哪个连通分量,因此需要额外的g数组在搜索连通分量时便记录每一个(i,j)的属性。

   在本题中,另一个比较麻烦的事情是关于推翻墙的优先级问题,优先级顺序依次是W,S,N,E,需要在更新最优操作时格外注意,哎,其实读英文题时并没有看懂这些优先级的处理,英语弱爆了。。。

三.代码

/*
ID:xxx
LANG:C
TASK:castle
*/
#include <stdio.h>
#include <stdlib.h>
int Graph[50][50][4];  //保存输入信息
int g[50][50];    //记录每个方格所属的连通分量
int visit[50][50];   //记录每个方格是否已经被访问
int M,N,cn=0,count=0;  //cn代表连通分量,count记录每个连通分量中方格的个数

void DFS(int x,int y)  //用深搜寻找连通分量
{

    visit[x][y]=1;
    g[x][y]=cn;
    count++;

    //search four direction

    if((y-1)>=0&&Graph[x][y][0]==0&&visit[x][y-1]==0)
        DFS(x,y-1);

    if((x-1)>=0&&Graph[x][y][1]==0&&visit[x-1][y]==0)
        DFS(x-1,y);

    if((y+1)<=M&&Graph[x][y][2]==0&&visit[x][y+1]==0)
        DFS(x,y+1);

    if((x+1)<=N&&Graph[x][y][3]==0&&visit[x+1][y]==0)
        DFS(x+1,y);

    return ;
}

int main()
{
    int i,j,k,x,compont[2500];
    int max=-1,maxcp=-1,temp,maxdir,maxi,maxj;

    char dir[4]={'W','N','E','S'};
    FILE *in=fopen("castle.in","r"),*out=fopen("castle.out","w");

    if(!in||!out)
    {
        printf("file open error!\n");
        return -1;
    }

    fscanf(in,"%d%d",&M,&N);

    //初始化操作
    for(i=0;i<N;i++)
    {
        for(j=0;j<M;j++)
        {
            Graph[i][j][0]=0;Graph[i][j][1]=0;Graph[i][j][2]=0;Graph[i][j][3]=0;
            visit[i][j]=0;
        }
    }
  //  memset(Graph,0,sizeof(Graph));
 //   memset(visit,0,sizeof(visit));

    for(i=0;i<N;i++)
    {
        for(j=0;j<M;j++)
        {
            fscanf(in,"%d",&x);
            if((x&1)==1)
                Graph[i][j][0]=1;    //block

            if((x&2)==2)
                 Graph[i][j][1]=1;

            if((x&4)==4)
                Graph[i][j][2]=1;

            if((x&8)==8)
                 Graph[i][j][3]=1;

        }
    }

    //寻找连通分量,并记录每一个连通分量中的方格个数
    cn=0;
    for(i=0;i<N;i++)
    {
        for(j=0;j<M;j++)
        {

            if(!visit[i][j])
            {
                count=0;
                DFS(i,j);
                compont[cn]=count;
                cn++;
            }
        }
    }

    //寻找最大的连通分量
    for(i=0;i<cn;i++)
    {
        if(max<compont[i])
            max=compont[i];
    }

    maxcp=max;
    maxi=-1;
    maxj=M;
    k=0;
    for(i=0;i<N;i++)
    {
        for(j=0;j<M;j++)
        {   //一次从四个方向判断是否可以推翻墙,注意更新优先级是W,S,N,E
            if((j-1)>=0&&Graph[i][j][0]==1&&g[i][j]!=g[i][j-1])
            {
                temp=compont[g[i][j]]+compont[g[i][j-1]];
                if(maxcp<temp||(maxcp==temp&&j<maxj)||(maxcp==temp&&j==maxj&&i>=maxi))  //此处最后为i>=maxi,当j=maxj&i=maxi时可以更新,方向更新为N
                {
                    maxcp=temp;
                    maxdir=0;
                    maxi=i;
                    maxj=j;
                }
            }
            if((i-1)>=0&&Graph[i][j][1]==1&&g[i][j]!=g[i-1][j])
            {
                temp=compont[g[i][j]]+compont[g[i-1][j]];
                if(maxcp<temp||(maxcp==temp&&j<maxj)||(maxcp==temp&&j<=maxj&&i>=maxi))
                {
                    maxcp=temp;
                    maxdir=1;
                    maxi=i;
                    maxj=j;
                }
            }
            if((j+1)<M&&Graph[i][j][2]==1&&g[i][j]!=g[i][j+1])
            {
                temp=compont[g[i][j]]+compont[g[i][j+1]];
                if(maxcp<temp||(maxcp==temp&&j<maxj)||(maxcp==temp&&j<=maxj&&i>maxi))  //此处最后为i>maxi,当j=maxj&i=maxi时不需更新,因为E优先级小于N,在N中更新
                {
                    maxcp=temp;
                    maxdir=2;
                    maxi=i;
                    maxj=j;
                }
            }
            if((i+1)<N&&Graph[i][j][3]==1&&g[i][j]!=g[i+1][j])
            {
                temp=compont[g[i][j]]+compont[g[i+1][j]];
                if(maxcp<temp||(maxcp==temp&&j<maxj)||(maxcp==temp&&j<=maxj&&i>=maxi))
                {
                    maxcp=temp;
                    maxdir=3;
                    maxi=i;
                    maxj=j;
                }
            }
        }
    }
    fprintf(out,"%d\n%d\n%d\n%d %d %c\n",cn,max,maxcp,maxi+1,maxj+1,dir[maxdir]);

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值