2021-01-07 USACO The Castle

这篇博客介绍了USACO的一道编程挑战,任务是帮助Farmer John计算城堡的房间数量和最大房间大小,并找出能最大化房间面积的墙壁进行拆除。城堡由MxN的模块组成,每个模块根据其墙壁描述来确定连接关系。博客提供了输入格式、样例输入和输出格式,并详细解释了如何解析地图和编码每个模块的墙壁。解决方案建议采用枚举拆墙并使用广度优先搜索的方法来解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ("room"), 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 descriptive-number tells which 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

INPUT DETAILS

First, the map is partitioned like below. Note that walls not on the outside borders are doubled:

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

Let's talk about the squares with a (row, column) notation such that the lower right corner is denoted (4, 7).

The input will have four lines, each with 7 numbers. Each number describes one 'room'. >Walls further toward the top are 'north', towards the bottom are 'south', towards the left are 'west', and towards the right are 'east'.

Consider square (1,1) which has three walls: north, south, and west. To encode those three walls, we consult the chart:

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

and sum the numbers for north (2), south (8), and west (1). 2 + 8 + 1 = 11, so this room is encoded as 11.

The next room to the right (1,2) has walls on the north (2) and east (4) and thus is encoded as 2 +4 = 6.

The next room to the right (1,3) is the same as (1,1) and thus encodes as 11.

Room (1,4) is the same as (1,2) and thus encodes as 6.

Room (1,5) has rooms on the west (1) and north (2) and thus encodes as 1 + 2 = 3.

Room (1,6) has rooms on the north (2) and south (8) and thus encodes as 2 + 8 = 10.

Room (1,7) is the same as room (1,2) and thus encodes as 6.

This same method continues for rooms (2,1) through (4,7).

 

甚至不用建图,直接枚举拆墙,暴力广搜就好了。只是for房间和拆墙的时候注意一下顺序就好了。

/*
ID: traysen1
TASK: castle
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;

struct Vertex {
	int x, y;
	Vertex(int x, int y) : x(x), y(y) {}
};
struct Wall {
	int x, y;
	char side;
	Wall(int x, int y, char side) : x(x), y(y), side(side) {}
}wall = Wall(0, 0, 'N');
int M, N, max_size, room;
int castle[55][55], visited[55][55];

int find_room(int x, int y) {
	queue<Vertex> q;
	q.push(Vertex(x, y));
	visited[x][y] = 1;
	
	int siz = 1;
	while (!q.empty()) {
		Vertex v = q.front();
		q.pop();
		
		for (int i = 1; i <= 8; i <<= 1)
			if (!(castle[v.x][v.y] & i)) { //No wall
				if (i == 1 && (!visited[v.x][v.y - 1])) {
					q.push(Vertex(v.x, v.y - 1));
					visited[v.x][v.y - 1] = 1;
					siz++;
				} else if (i == 2 && (!visited[v.x - 1][v.y])) {
					q.push(Vertex(v.x - 1, v.y));
					visited[v.x - 1][v.y] = 1;
					siz++;
				} else if (i == 4 && (!visited[v.x][v.y + 1])) {
					q.push(Vertex(v.x, v.y + 1));
					visited[v.x][v.y + 1] = 1;
					siz++;
				} else if (i == 8 && (!visited[v.x + 1][v.y])){
					q.push(Vertex(v.x + 1, v.y));
					visited[v.x + 1][v.y] = 1;
					siz++;
				}
			}
	}
	
	return siz;
}

int break_walls(int x, int y) {
	int siz = 0;
	
	if ((castle[x][y] & 2) && (x != 1)) {//Wall on the North side
		castle[x][y] -= 2;
		castle[x - 1][y] -= 8;
		memset(visited, 0, sizeof(visited));
		siz = find_room(x, y);
		if (siz > max_size) {
			max_size = siz;
			wall = Wall(x, y, 'N');
		}
		castle[x][y] += 2;
		castle[x - 1][y] += 8;
	}
	
	if ((castle[x][y] & 4) && (y != M)) {//Wall on the East side
		castle[x][y] -= 4;
		castle[x][y + 1] -= 1;
		memset(visited, 0, sizeof(visited));
		siz = find_room(x, y);
		if (siz > max_size) {
			max_size = siz;
			wall = Wall(x, y, 'E');
		}
		castle[x][y] += 4;
		castle[x][y + 1] += 1;
	}
	
	return siz;
}

int main() {
	ifstream fin("castle.in");
	fin >> M >> N;// N rows, M colomns
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= M; j++)
			fin >> castle[i][j];
	fin.close();
	
	//Find original rooms and the largest room
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= M; j++)
			if (!visited[i][j]) {
				room++;
				max_size = max(max_size, find_room(i, j));
			}
	
	ofstream fout("castle.out");
	fout << room << endl;
	fout << max_size << endl;
	
	//Break wall
	max_size = 0;
	for (int j = 1; j <= M; j++)
		for (int i = N; i >= 1; i--) {
			//fout << i << " " << j << endl;
			break_walls(i, j);
		}
	
	fout << max_size << endl;
	fout << wall.x << " " << wall.y << " " << wall.side << endl;
	fout.close();
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值