USACO Section2.1 The Castle

本文深入探讨了Unity3D和Unreal Engine两款顶级游戏引擎的开发技术,从动画制作、游戏逻辑实现到性能优化等方面进行全面解析。通过实际案例,帮助开发者掌握这两款引擎的核心能力,提升游戏开发效率。

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

The Castle
IOI'94 - Day 1

In a stroke of luck almost beyond imagination, Farmer John wassent a ticket to the Irish Sweepstakes (really a lottery) for hisbirthday. This ticket turned out to have only the winning numberfor the lottery! Farmer John won a fabulous castle in the Irishcountryside.

Bragging rights being what they are in Wisconsin, Farmer Johnwished to tell his cows all about the castle. He wanted to knowhow 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 andsizes.

The castle floorplan is divided into M (wide) by N (1 <=M,N<=50)square modules. Each such module can have between zero and fourwalls. Castles always have walls on their "outer edges" to keepout 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" includesany set of connected "squares" in the floor plan. This floorplan containsfive 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 tomake the largest possible room that can be made by removing a singlewall.

The castle always has at least two rooms and always has a wallthat 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 inputorder corresponds to the numbering in the example diagram above.

Each module number tells how many of the four walls exist andis 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,1is 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 thelargest room possible

Choose the optimal wall to remove from the set of optimal wallsby choosing the module farthest to the west (and then, if stilltied, farthest to the south). If still tied, choose 'N' before 'E'.Name that wall by naming the module that borders it on either thewest or south, along with a direction of N or E giving the locationof the wall with respect to the module.

SAMPLE OUTPUT (file castle.out)

5
9
16
4 1 E
/*
ID: wxx54331
PROG: castle
LANG: C++
*/
/*
首先使用floodFill算法得出这个图的所有连通块 
之后再按题目要求枚举可以拆掉的墙对房间的大小进行扩充,找到最大的扩充方案 
*/ 
#include<iostream>
#include<fstream>
using namespace std;

const int MAX = 50;
int row, column;     //整个图的大小 
int wall[MAX][MAX][5]; //0->west, 1->north, 2->east, 3->south, 4->group
bool visited[MAX][MAX];  //记录某个单元格是否已经被访问过 
int  dx[] = {-1, 0, 1, 0}; /*按←↑→↓的顺序移动*/
int  dy[] = {0, -1, 0, 1};
int group;    //记录房间数 
int roomSize[MAX*MAX];   //记录每个房间的大小 
int maxSize;
int r, c;  //记录要拆除的墙所在的单元格的行和列位置 
char dir;  //记录要拆除的墙的方位 

void readData();
void floodFill(int x, int y);
void merge(int x, int y);
int main()
{
	ofstream fout("castle.out");
	readData();
	/*floodFill算法找出房间的数量以及大小*/
	for(int i = 0; i < row; ++i)  
		for(int j = 0; j < column; ++j)
			if(!visited[i][j])
			{
				roomSize[group] = 0;
				floodFill(i, j);
				group++;
			}
	fout << group << endl;
	for(int i = 0; i < group; ++i)
		maxSize = max(maxSize, roomSize[i]);
	fout << maxSize << endl;
	/*从最西南边的墙开始枚举,拆除后面积是否为最大
	西边优先级最高,南边优先级次之,N墙优先级>E墙*/
	maxSize = 0;
	for(int i = 0; i < column; ++i)
		for(int j = row-1; j >= 0; --j)
			merge(j,i);
	fout << maxSize << endl;
	fout << r << " " << c << " " << dir << endl;
	fout.close();
	return 0;
}

void readData()
{
	ifstream fin("castle.in");
	int number;
	fin >> column >> row;
	for(int i = 0; i < row; ++i)
		for(int j = 0; j < column; ++j)
		{
			fin >> number;
			/*思考的时候将数字想为二进制,比如6是0110,每次从右边取出一位*/
			for(int k = 0; k <= 3; ++k) 
			{
				wall[i][j][k] = number % 2;
				number = number >> 1;
			}
			wall[i][j][4] = 0;
		}
	fin.close();
}

void floodFill(int y, int x)  /*按照坐标轴来考虑*/
{
	visited[y][x] = true;
	wall[y][x][4] = group;
	roomSize[group]++;
	for(int i = 0; i <= 3; ++i) //遍历4堵墙
	{
		int nx = x + dx[i];   //向上下左右的房间移动 
		int ny = y + dy[i]; 
		if(nx < 0 || ny < 0 || nx >= column || ny >= row)  //越界
			continue;
		if(visited[ny][nx] || wall[y][x][i])  //新的房间已经访问过,或者现在要移动到的方向有墙
			continue;
		floodFill(ny, nx); 
	} 
}

void merge(int y, int x)
{
	int sum = 0;
	if(y>=1 && wall[y][x][4]!=wall[y-1][x][4])  //拆除N边的墙 
	{
		sum = roomSize[wall[y][x][4]] + roomSize[wall[y-1][x][4]];
		if(sum > maxSize)
		{
			maxSize = sum;
			r = y+1;
			c = x+1;
			dir = 'N';
		}
	}
	if(x<column-1 && wall[y][x][4]!=wall[y][x+1][4])  //拆除E边的墙 
	{
		sum = roomSize[wall[y][x][4]] + roomSize[wall[y][x+1][4]];
		if(sum > maxSize)
		{
			maxSize = sum;
			r = y+1;
			c = x+1;
			dir = 'E';
		}
	}
}



资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值