AmazingCounters.com

USACO 2.1 The Castle

题目大意:给你一个城堡让你求有多少房间,最大房间有多大,敲掉一堵墙后最大的房间有多大,敲掉那座墙

思路:比较恶心的bfs题,反正就是bfs使劲敲

 

/*{
ID:a4298442
PROB:castle
LANG:C++
}
*/
#include<iostream>
#include<cstdio>
#include<fstream>
#include<queue>
#include<algorithm>
#define pii pair<int,int>
using namespace std;
ifstream fin("castle.in");
ofstream fout("castle.out");
const int dx[10]={0,0,-1,0,1};
const int dy[10]={0,-1,0,1,0};
int n,m,map[100][100],a[100][100],large[1000];
int check(int x,int y,int n,int m)
{
    if(1<=x && x<=n && 1<=y && y<=m)return 1;
    return 0;
}
int bfs(int x,int y,int col)
{
    int ret=1;
    queue<pii >q;
    q.push(make_pair(x,y));
    map[x][y]=col;
    //cout<< x<< " "<<y<<" "<<col<<endl;
    while(!q.empty())
    {
        pii u=q.front();
        x=u.first;y=u.second;
        q.pop();
        for(int i=1;i<=4;i++)
        {
            int temp=a[x][y]&(1<<(i-1));
               if(temp!=0)continue;
            int xx=x+dx[i],yy=y+dy[i];
            if(!check(xx,yy,n,m))continue;
            if(map[xx][yy]!=0)continue;
            map[xx][yy]=col;
            ret++;
            q.push(make_pair(xx,yy));
            //cout<<xx<<" "<<yy<<" "<<col<<endl;
        }
    }
    return ret;
}
int main()
{
    //scanf("%d%d",&m,&n);
    fin>>m>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            //scanf("%d",&a[i][j]);
            fin>>a[i][j];
        }
    }
    int col=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(map[i][j]==0)
            {
                col++;
                ans=max(large[col]=bfs(i,j,col),ans);
            }
        }
    }
    int ans2=0,ansx,ansy,ansk;
    for(int j=1;j<=m;j++)
    {
        for(int i=n;i>=1;i--)
        {
            for(int k=2;k<=3;k++)
            {
                int x=i+dx[k],y=j+dy[k];
                if(!check(x,y,n,m))continue;
                if(map[i][j]!=map[x][y])
                {
                    if(ans2<large[map[i][j]]+large[map[x][y]])
                    {
                        ans2=large[map[i][j]]+large[map[x][y]];
                        ansx=i;ansy=j;ansk=k;
                    }
                }
            }
        }
    }
    fout<<col<<endl<<ans<<endl<<ans2<<endl<<ansx<<" "<<ansy<<" ";
    if(ansk==2)fout<<"N"<<endl;
    if(ansk==3)fout<<"E"<<endl;
    return 0;
}

 

posted @ 2015-03-06 22:00  philippica  阅读(97)  评论(0编辑  收藏  举报