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;
}


 

你可能感兴趣的:(c,struct)