USACO--2.1The Castle

思路:这个题目难在建图,開始的时候我想把每一个房间没有墙的面求出来,然后再和他邻近的房间加上一条边进行建图,后面发现要通过题目给定的条件求出房间那个面没有墙是十分困难的;后面參考了别人的思路,我们记录每一个房间那几面是有墙的(这个非常easy做到),然后就不显示建图了。直接通过dfs标记的思想求出这个图全部的连通块(Flood fill 算法)。后面的处理就比較简单了,求出这个连通块后就能够知道总共同拥有几个房间了,最大的房间有多大;然后我们再依次试探拆除每一个房间的N,E面的墙壁。看一下能得到的最大房间的数量。
对于题目中:
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.
这段话,開始的时候我非常是不能理解,后面才知道,他的意思是:在大的方向上我们要优先拆除西面和然后是南面。对于每一个小的房间我们应该优先拆除北面然后是东面。果然是英语太渣。。。

。。。

代码例如以下:

/*
ID: 15674811
LANG: C++
TASK: castle
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<fstream>
using namespace std;

///南东北西
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

///西南北东
///int dx1[]={0,1,-1,0};
///int dy1[]={-1,0,0,1};
///北东
int dx1[]={-1,0};
int dy1[]={0,1};

int vis[55][55],n,m;
char name[]={'N','E'};

typedef struct
{
    bool flag[5];
}P;
P p[55][55];

void dfs(int x,int y,int flag)
{
     for(int k=0;k<4;k++)
     {
         if(p[x][y].flag[k])
             continue;
         int xx=x+dx[k];
         int yy=y+dy[k];
         if(xx<1||xx>n||yy<1||yy>m)
            continue;
         if(vis[xx][yy])
            continue;
         vis[xx][yy]=flag;
         dfs(xx,yy,flag);
     }
}

int main()
{
    ofstream fout("castle.out");
    ifstream fin("castle.in");
    //ifstream fin("lkl.txt");
    while(fin>>m>>n)
    {
        memset(p,0,sizeof(p));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                int x;
                fin>>x;
                if(x>=8) p[i][j].flag[0]=true,x-=8;
                if(x>=4) p[i][j].flag[1]=true,x-=4;
                if(x>=2) p[i][j].flag[2]=true,x-=2;
                if(x>=1) p[i][j].flag[3]=true;
            }
         int cnt=0;
         for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                if(vis[i][j])
                    continue;
                vis[i][j]=++cnt;
                dfs(i,j,cnt);
            }
         int num[3000];
         memset(num,0,sizeof(num));
         int Max=0;
         for(int i=1;i<=n;i++)
           for(int j=1;j<=m;j++)
           {
               num[vis[i][j]]++;
               Max=max(Max,num[vis[i][j]]);
           }
         fout<<cnt<<endl<<Max<<endl;
         int room=0,rx,ry,k;
         for(int x=1;x<=n;x++)
            for(int y=1;y<=m;y++)
            {
                for(int d=0;d<2;d++)
                {
                    int xx=x+dx1[d];
                    int yy=y+dy1[d];
                    if(xx<1||xx>n||yy<1||yy>m)
                        continue;
                    if(vis[x][y]==vis[xx][yy])
                        continue;
                    int tmp=num[vis[x][y]]+num[vis[xx][yy]];
                    if(room<tmp)
                    {
                        rx=x; ry=y;
                        room=tmp;
                        k=d;
                    }
                    else if(room==tmp)
                    {
                       if(y<=ry)
                       {
                           if(rx==x&&ry==y) ///同一个格子北东都能够要保证北优先
                              continue;
                           rx=x; ry=y;
                           k=d;
                       }
                    }
                }
            }
        fout<<room<<endl;
        fout<<rx<<" "<<ry<<" "<<name[k]<<endl;
    }
  return 0;
}
posted on 2015-12-26 08:12  gcczhongduan  阅读(151)  评论(0编辑  收藏  举报