USACO The Castle


The Castle
IOI'94 - Day 1

In a stroke of luck almost beyond imagination, Farmer John was sent a ticket to the Irish Sweepstakes (really a lottery) for his birthday. This ticket turned out to have only the winning number for the lottery! Farmer John won a fabulous castle in the Irish countryside.

Bragging rights being what they are in Wisconsin, Farmer John wished to tell his cows all about the castle. He wanted to know how many rooms it has and how big the largest room was. In fact, he wants to take out a single wall to make an even bigger room.

Your task is to help Farmer John know the exact room count and sizes.

The castle floorplan is divided into M (wide) by N (1 <=M,N<=50) square modules. Each such module can have between zero and four walls. Castles always have walls on their "outer edges" to keep out the wind and rain.

Consider this annotated floorplan of a castle:

     1   2   3   4   5   6   7
   #############################
 1 #   |   #   |   #   |   |   #
   #####---#####---#---#####---#   
 2 #   #   |   #   #   #   #   #
   #---#####---#####---#####---#
 3 #   |   |   #   #   #   #   #   
   #---#########---#####---#---#
 4 # ->#   |   |   |   |   #   #   
   ############################# 

#  = Wall     -,|  = No wall
-> = Points to the wall to remove to
     make the largest possible new room

By way of example, this castle sits on a 7 x 4 base. A "room" includes any set of connected "squares" in the floor plan. This floorplan contains five rooms (whose sizes are 9, 7, 3, 1, and 8 in no particular order).

Removing the wall marked by the arrow merges a pair of rooms to make the largest possible room that can be made by removing a single wall.

The castle always has at least two rooms and always has a wall that can be removed.

PROGRAM NAME: castle

INPUT FORMAT

The map is stored in the form of numbers, one number for each module, M numbers on each of N lines to describe the floorplan. The input order corresponds to the numbering in the example diagram above.

Each module number tells how many of the four walls exist and is the sum of up to four integers:

  • 1: wall to the west
  • 2: wall to the north
  • 4: wall to the east
  • 8: wall to the south

Inner walls are defined twice; a wall to the south in module 1,1 is also indicated as a wall to the north in module 2,1.

Line 1:Two space-separated integers: M and N
Line 2..:M x N integers, several per line.

SAMPLE INPUT (file castle.in)

7 4
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13

OUTPUT FORMAT

The output contains several lines:

Line 1:The number of rooms the castle has.
Line 2:The size of the largest room
Line 3:The size of the largest room creatable by removing one wall
Line 4:The single wall to remove to make the largest room possible

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.

SAMPLE OUTPUT (file castle.out)

5
9
16
4 1 E


Submission file Name:   
USACO Gateway  |    Comment or Question


/* 
ID: qhn9992 
PROG: castle 
LANG: C++11 
*/  
/* ***********************************************
Author :码代码的猿猿CKboss
Created Time :2014/2/7 22:21:01
File Name :TheCastle.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <string>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1.0);
typedef long long int LL;

int n,m,cnt=1;
int g[100][100],area[10000];

struct gezhi
{
	int west,north,east,south;
}node[100][100];

void done(int x,int i,int j)
{
	if(x&(1<<0)) node[i][j].west=1;
	if(x&(1<<1)) node[i][j].north=1;
	if(x&(1<<2)) node[i][j].east=1;
	if(x&(1<<3)) node[i][j].south=1;
}

bool ck(int x,int y)
{
	if((x>=0&&x<n)&&(y>=0&&y<m)) return true;
	return false;
}

void dfs(int x,int y,int color)
{
	g[x][y]=color;
	area[color]++;
	int X,Y;
	///to north
	X=x-1;Y=y;
	if(ck(X,Y)&&g[X][Y]==0&&node[x][y].north==0&&node[X][Y].south==0)
	{
		dfs(X,Y,color);	
	}
	///to south
	X=x+1;Y=y;
	if(ck(X,Y)&&g[X][Y]==0&&node[X][Y].north==0&&node[x][y].south==0)
	{
		dfs(X,Y,color);
	}
	///to west
	X=x;Y=y-1;
	if(ck(X,Y)&&g[X][Y]==0&&node[X][Y].east==0&&node[x][y].west==0)
	{
		dfs(X,Y,color);
	}
	///to east
	X=x;Y=y+1;
	if(ck(X,Y)&&g[X][Y]==0&&node[X][Y].west==0&&node[x][y].east==0)
	{
		dfs(X,Y,color);
	}
} 

int main()
{
    freopen("castle.in","r",stdin);
    freopen("castle.out","w",stdout);
	scanf("%d%d",&m,&n);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			int sign;
			scanf("%d",&sign);
			done(sign,i,j);
		}
	}
	int num=0;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(g[i][j]==0)
			{
				num++;
				dfs(i,j,cnt++);
			}
		}
	}
	int MAXAREA=-INF;
	for(int i=1;i<cnt;i++)
	{
		MAXAREA=max(MAXAREA,area[i]);
	}

	///cash wall
	int MAXdouble=-INF;
	int posx,posy;
	char dir;
	for(int j=0;j<m;j++)
	{
		for(int i=n-1;i>=0;i--)
		{
			///to north
			if(node[i][j].north)
			{
				int X=i-1,Y=j;
				if(ck(X,Y))
				{
					int color1=g[i][j];
					int color2=g[X][Y];
					if(color1!=color2)
					{
						if(area[color1]+area[color2]>MAXdouble)
						{
							MAXdouble=area[color1]+area[color2];
							posx=i;posy=j;
							dir='N';
						}
					}
				}
			}
			///to east
			if(node[i][j].east)
			{
				int X=i,Y=j+1;
				if(ck(X,Y))
				{
					int color1=g[i][j];
					int color2=g[X][Y];
					if(color1!=color2)
					{
						if(area[color1]+area[color2]>MAXdouble)
						{
							MAXdouble=area[color1]+area[color2];
							posx=i;posy=j;
							dir='E';
						}	
					}
				}
			}
		}
	}
	printf("%d\n%d\n%d\n%d %d %c\n",num,MAXAREA,MAXdouble,posx+1,posy+1,dir);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值