zoukankan      html  css  js  c++  java
  • USACOThe 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

  • 相关阅读:
    夺命雷公狗—玩转SEO---64---高质量链接打造
    夺命雷公狗—玩转SEO---63---页面相似度算法原理
    夺命雷公狗—玩转SEO---62---TF-IDF核心算法与运用
    夺命雷公狗—玩转SEO---61---全站title与description深层次玩法,门户站和流量站
    夺命雷公狗—玩转SEO---60---全站title与description深层次玩法,企业站内容页
    夺命雷公狗—玩转SEO---59---全站title与description深层次玩法,企业站列表页
    夺命雷公狗—玩转SEO---58---全站title与description深层次玩法,企业站首页篇
    夺命雷公狗—玩转SEO---57---DEDE副标题大爆发
    夺命雷公狗—玩转SEO---56---query需求分析与搜索意图研究
    夺命雷公狗—玩转SEO---55---页面去重原理
  • 原文地址:https://www.cnblogs.com/ay27/p/2727975.html
Copyright © 2011-2022 走看看