USACO Chapter2 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



这道题把可以把每个格点都抽象为一个顶点,建图,然后进行连通分量的测试。 

直接用DFS更为简单,对每一个没有检查过的格点进行深搜,给每一个连通分量标记一个独立的标号(房间号),并记下每间房间的大小。

以下源码:

#include <fstream>
using namespace std;

int m, n;
int roomN;			// room Number
int room[51][51];		// room[1][1] == 1 means vertex(1,1) belongs to room1
int size[50*50+1];		// size[1] = 5; means room1 consists of 5 vertices
int Smax;			// max Room Size
int wall[51][51][4];	 // wall[1][1][0] means if there is a wall in the west , similar to east, north and south
int check[51][51];		// check[1][1] means vertex(1,1) has been visited or not
void dfs(int i, int j);


int main()
{
	ifstream fin("castle.in");
	ofstream fout("castle.out");

	fin >> m >> n;
	
	int tmp;
	int i, j, k;
	for(i=1; i<=n; i++)		
		for(j=1; j<=m; j++)
		{
			fin >> tmp;
			for(k=0; k<4; k++) wall[i][j][k] = (tmp >> k) & 1;		//set the wall
		}

	for(i=1; i<=n; i++)
		for(j=1; j<=m; j++)
		{
			if(!check[i][j])
			{
				++roomN;	//update room Number
				dfs(i,j);	//dfs
				Smax = (size[roomN] > Smax) ? size[roomN]:Smax;
			}
		}

	fout << roomN << endl << Smax << endl;

	Smax = 0;
	int roomA, roomB, roomC;		//roomB is the vertex upon roomA, roomC is the vertex in the right of roomA
	int ans_x, ans_y;
	char di;
	//traverse all the vertex and try to remove north wall and east wall
	for(i=1; i<=m; i++)
		for(j=n; j>=1; j--)
		{
			roomA = room[j][i];
			roomB = room[j-1][i];	
			roomC = room[j][i+1];	
			if(j >1 && wall[j][i][1] && roomA!=roomB && size[roomA]+size[roomB] > Smax)
			{
				Smax = size[roomA]+size[roomB];
				ans_x = j;
				ans_y = i;
				di = 'N';
			}
			else if(i<m && wall[j][i][2]  && roomA!=roomC && size[roomA]+size[roomC] > Smax)
			{
				Smax = size[roomA]+size[roomC];
				ans_x = j;
				ans_y = i;
				di = 'E';
			}
		}

	fout << Smax << endl << ans_x << " " << ans_y << " " << di << endl;

	return 0;
}

void dfs(int i, int j)
{
	if(room[i][j] == roomN) return;
	check[i][j] = 1;
	size[roomN]++;
	room[i][j] = roomN;
	if(!wall[i][j][0]) dfs(i,j-1);
	if(!wall[i][j][1]) dfs(i-1,j);
	if(!wall[i][j][2]) dfs(i,j+1);
	if(!wall[i][j][3]) dfs(i+1,j);
}



你可能感兴趣的:(USACO Chapter2 The Castle)