USACO-The Castle

http://ace.delos.com/usacoprob2?a=B9V6yMSRsQa&S=castle

一题简单而综合的题,主要问题是输入数据的存储和处理。

我定义的存储方式:

struct node{
    bool dd[4];
    int room;
    node()
    {
        for (int i=0;i<4;i++)
            dd[i]=false;
        room=0;
    }
}a[55][55];

其中,a[][]数组存储整个图,dd数组存储4个方向的可行性,0~4分别代表S,E,N,W,room存储是第几个房间。

这样,存储的问题解决了。

对于问题1和问题2,对整个图进行多次BFS,遍历整个图,在BFS过程中记录房间号和房间大小,简单。

对于问题3和问题4,则枚举每一面墙,当然这枚举需要方法。之前BFS时其实已经记录下了每个房间的大小,拆除一面墙只需墙两边的房间大小相加就是了。而注意到:

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

则,我们需从图的左下角开始枚举墙,并且枚举的墙一定是N和E的墙。

#include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;

struct node{
    bool dd[4];   //存储4个方向的可行性,0-4表示S,E,N,W
    int room;
    node()
    {
        for (int i=0;i<4;i++)
            dd[i]=false;
        room=0;
    }
}a[55][55];

const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};         //坐标的移动,顺序和dd数组的顺序一样
int qx[5000],qy[5000];
void bfs(int x,int y,int nn,int &sum)         //nn是当前的房间号,sum是房间的大小
{
    sum=1;
    int l=0,r=1;
    qx[1]=x; qy[1]=y;
    a[x][y].room=nn;
    int xx,yy;
    while (l<r)
    {
        l++;
        xx=qx[l]; yy=qy[l];
        for (int i=0;i<4;i++)
            if (!a[xx][yy].dd[i] && !a[xx+dx[i]][yy+dy[i]].room)
            {
                sum++;
                a[xx+dx[i]][yy+dy[i]].room=nn;
                r++;
                qx[r]=xx+dx[i]; qy[r]=yy+dy[i];
            }
    }    
}

int rr[5000]={0};
int main()
{
    freopen("castle.in","r",stdin);
    freopen("castle.out","w",stdout);
    int n,m;
    cin>>m>>n;
    int temp;
    for (int i=1;i<=n;i++)         //输入的处理
        for (int j=1;j<=m;j++)
        {
            scanf("%d",&temp);
            if (temp>=8)
            {
                a[i][j].dd[0]=true;
                temp-=8;
            }
            if (temp>=4)
            {
                a[i][j].dd[1]=true;
                temp-=4;
            }
            if (temp>=2)
            {
                a[i][j].dd[2]=true;
                temp-=2;
            }
            if (temp>=1)
                a[i][j].dd[3]=true;
        }

    int sumRoom=0,maxRoom=0;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
        if (!a[i][j].room)
        {
            sumRoom++;
            bfs(i,j,sumRoom,temp);
            rr[sumRoom]=temp;         //记录每个房间的大小
            if (temp>maxRoom) maxRoom=temp;
        }
    cout<<sumRoom<<endl<<maxRoom<<endl;

    int maxRoom1=maxRoom,r_x=0,r_y=0;           //r_x,r_y是要拆的墙的位置
    char type;                 //墙的方向
    for (int j=1;j<=m;j++)           //从左下角开始,一列一列往右做
        for (int i=n;i>0;i--)
        {
            if (a[i][j].dd[2] && a[i-1][j].room!=a[i][j].room
                && rr[a[i][j].room]+rr[a[i-1][j].room]>maxRoom1)
            {
                maxRoom1=rr[a[i][j].room]+rr[a[i-1][j].room];
                r_x=i; r_y=j;
                type='N';
            }
            if (a[i][j].dd[1] && a[i][j+1].room!=a[i][j].room
                && rr[a[i][j].room]+rr[a[i][j+1].room]>maxRoom1)
            {
                maxRoom1=rr[a[i][j].room]+rr[a[i][j+1].room];
                r_x=i; r_y=j;
                type='E';
            }
        }
    cout<<maxRoom1<<endl;
    cout<<r_x<<' '<<r_y<<' '<<type<<endl;
    return 0;
}

 

发现USACO的数据爆弱。。。

castle

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值