USACO 2.1.1The Castle

// the castle.cpp : 定义控制台应用程序的入口点。
//
/*
ID: maiyuet1
PROG: castle
LANG: C++
*/
//#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int dir[4][2] = {0,-1,-1,0,0,1,1,0}; //北 西 东 南
const int MAXN = 55;
int num_house;

typedef struct POINT
{
	int x;
	int y;
	int wall[4];
	int cnt;
	int sign;
}Point;

Point  p[MAXN][MAXN];
int visited[MAXN][MAXN];	
int row,column;

int bfs(int x, int y)
{
	int cnt = 1;
	queue<Point>Q;
	queue<Point>Save;
	Point start;
	start.x = x;
	start.y = y;
	Q.push(start);
	Save.push(start);
	while(!Q.empty())
	{
		Point flag = Q.front();
		Q.pop();
		for(int i=0; i<4; i++)
		{
			if(!p[flag.x][flag.y].wall[i]) //没有墙
			{
				Point temp;
				temp.x = flag.x + dir[i][0];
				temp.y = flag.y + dir[i][1];
				if(temp.x >= 1 && temp.x <= row && temp.y >= 1 && temp.y <= column && !visited[temp.x][temp.y])
				{
					cnt++;
					visited[temp.x][temp.y] = 1;
					Q.push(temp);
					Save.push(temp);
				}
			}
		}
	}
	while(!Save.empty())
	{
		Point top = Save.front();
		Save.pop();
		p[top.x][top.y].cnt = cnt;
		p[top.x][top.y].sign = num_house;
	}
	return cnt;
}

int main()
{
	int a;
	int max_house;
	int ans_x,ans_y;
	char ans_d;
	freopen("castle.in","r",stdin);
	freopen("castle.out","w",stdout);
	while(cin>>column>>row)
	{
		num_house = max_house = 0;
		memset(visited,0,sizeof(visited));
		for(int i=1; i<=row; i++)
		{
			for(int j=1; j<=column; j++)
			{
				cin>>a;
				p[i][j].wall[0] = a & 1; 
				p[i][j].wall[1] = a & 2;
				p[i][j].wall[2] = a & 4;
				p[i][j].wall[3] = a & 8;
			}
		}

		for(int i=1; i<=row; i++)
		{
			for(int j=1; j<=column; j++)
			{
				if(!visited[i][j])
				{
					visited[i][j] = 1;
					num_house++;
					int num = bfs(i,j);
					if(num > max_house)
					{
						max_house = num;
					}
				}
			}
		}
		int ans = 0;
		for(int j=1; j<=column; j++) //从左下角开始尝试,因为有多解的时候选择最靠西,再最靠南
		{
			for(int i=row; i>=1; i--)
			{
				for(int k=1; k<=2; k++)  //只往东和北的方向推墙
				{
					if(p[i][j].wall[k])  //有墙
					{
						int a = i + dir[k][0];
						int b = j + dir[k][1];
						if(a >= 1 && a <= row && b >= 1 && b <= column)
						{
							if(p[i][j].sign != p[a][b].sign && p[i][j].cnt + p[a][b].cnt > ans)
							{
								//cout<<i<<" "<<j<<" "<<p[i][j].cnt<<" "<<a<<" "<<b<<" "<<p[a][b].cnt<<endl;
								ans = p[i][j].cnt + p[a][b].cnt;
								ans_x = i;
								ans_y = j;
								if(k == 1)
								{
									ans_d = 'N';
								}
								else
								{
									ans_d = 'E';
								}
							}
						}
					}
				}
			}
		}
		cout<<num_house<<endl<<max_house<<endl<<ans<<endl<<ans_x<<" "<<ans_y<<" "<<ans_d<<endl;
	}
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值