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
这其中我们可以看出来可以分成两部分求解,第一部分先求解城堡的房间数目和最大的房间的大小。
首先我们可以看出可以用图来解决的。这里我一开始是把每个房间当成一个点,每个墙当成一条边,后来发现并没有用到点,于是就删掉了。
这里的边就是我代码里面的wall,wall是一个三维数组,前两位是表示坐标,第三位是表示方向。可以看出来0w,1n,2e,3s(第0号元素表示west方向,诸如此类)。
这里的顺序是算出来的。具体是这样的,第一次%2得到的值是用来判断是否是2的倍数,如果是2的倍数,则表示不含有1,
同理,在temp除以2之后,temp%2的值就表示是否是4的倍数,如果是4的倍数,则不含2……同理推下去。
此时把wall[i][j][4]赋值为0,为的是之后给这个变量赋值颜色。
此时我们对所有的元素遍历,进行flood_fill。
至此前两问就解决了。 至于问合并后的房间大小,这个也很好解决。由于考虑到优先级的问题,所以这里遍历的顺序需要注意一下,另外只需要看两个方向就好了。 我认为本题的主要难点在于对于x,y不要写反掉。
最后放一下代码。
/**
ID: njuwz151
TASK: castle
LANG: C++
**/
#include <bits/stdc++.h> #define MAXN 55 using namespace std; int wall[MAXN][MAXN][] = {};
bool visit[MAXN][MAXN] = {false};
int m, n;
int color = ;
int roomSize[MAXN * MAXN] = {};
int dx[] = {, -, , };
int dy[] = {-, , , };
int max_after = -;
int x_a;
int y_a;
char dir; void flood_fill(int x, int y); void merge(int x, int y); int main() {
freopen("castle.in", "r", stdin);
freopen("castle.out", "w", stdout); cin >> m >> n;
int t;
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
cin >> t;
/**
1: wall to the west
2: wall to the north
4: wall to the east
8: wall to the south
*/
for(int k = ; k < ; k++) {
// cout << i << " " << j << " " << k << " " << t%2 << endl;
wall[i][j][k] = t%;
t /= ;
}
wall[i][j][] = ;
}
} for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
if(!visit[i][j]) {
roomSize[color] = ;
flood_fill(i, j);
color++;
}
}
}
int max = ;
for(int i = ; i < color; i++) {
if(roomSize[i] > max) {
max = roomSize[i];
}
// cout << roomSize[i] << " ";
}
cout << color - << endl;
cout << max << endl; for(int i = ; i < m; i++) {
for(int j = n-; j >= ; j--) {
merge(j, i);
}
}
cout << max_after << endl;
cout << x_a+ << " " << y_a+ << " " << dir << endl;
} void flood_fill(int x, int y) {
visit[x][y] = true;
roomSize[color]++;
wall[x][y][] = color;
for(int i = ; i < ; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < || ny < ) {
continue;
}
if(nx >= n || ny >= m) {
continue;
}
if(visit[nx][ny]) {
continue;
}
if(wall[x][y][i]) {
continue;
}
flood_fill(nx, ny);
} } void merge(int x, int y) {
int sum;
if(x - >= && wall[x][y][] != wall[x-][y][]) {
sum = roomSize[wall[x][y][]] + roomSize[wall[x-][y][]];
if(sum > max_after) {
max_after = sum;
x_a = x;
y_a = y;
dir = 'N';
}
}
if(y + < m && wall[x][y][] != wall[x][y+][]) {
sum = roomSize[wall[x][y][]] + roomSize[wall[x][y+][]];
if(sum > max_after) {
max_after = sum;
x_a = x;
y_a = y;
dir = 'E';
}
}
}
castle
USACO The Castle的更多相关文章
- USACO Section2.1 The Castle 解题报告
castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- 【USACO 2.1】The Castle
/* TASK: castle LANG: C++ SOLVE: 深搜,注意每个方向对应值.枚举去掉的墙,然后再dfs,注意墙要复原,并且dfs里要判断是否超出边界. */ #include<c ...
- USACO Section 2.1 The Castle
/* ID: lucien23 PROG: castle LANG: C++ */ /********************************************************* ...
- USACO Section 2.1 The Castle 解题报告
题目 题目描述 有一个城堡,城堡中有若干个房间,房间与房间之间用墙来进行分隔.现在我们需要统计这个城堡有多少个房间,并且还要找出最大的房间的面积是多少(一个单元格就代表一个单元面积).城堡的主人现在想 ...
- USACO 2.1 The Castle
题目大意:给你一个城堡让你求有多少房间,最大房间有多大,敲掉一堵墙后最大的房间有多大,敲掉那座墙 思路:比较恶心的bfs题,反正就是bfs使劲敲 /*{ ID:a4298442 PROB:castle ...
- USACO castle
<pre name="code" class="cpp"><pre>USER: Kevin Samuel [kevin_s1] TASK ...
- [USACO Section 2.1]城堡 The Castle (搜索)
题目链接 Solution 比较恶心的搜索,思路很简单,直接广搜找联通块即可. 但是细节很多,要注意的地方很多.所以直接看代码吧... Code #include<bits/stdc++.h&g ...
- Usaco Training [2.1] The Castle 搜索
传送门 题目的输出的4个信息 前两个很容易,dfs,bfs都可以,图怎么建都可以 后两个在搜索的时候记录belong[i][j]和已有的size即可 代码应该比不少题解清晰吧 #include < ...
- 洛谷P1457 城堡 The Castle
P1457 城堡 The Castle 137通过 279提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 我们憨厚的USACO ...
随机推荐
- Java学习笔记——序列化和反序列化
寒雨连江夜入吴,平明送客楚山孤. 洛阳亲友如相问,一片冰心在玉壶. --芙蓉楼送辛渐 持久化数据的第一种方式.在序列化之前也可以把数据打散逐行存储在文件中,然后在逐行读取. 比如定Student类 用 ...
- NodeJS安装第一个工程
一.刚接触Node.js,下载好安装包后,一路Next,安装好后,结构目录如下 在命令行窗口输入node -v 和npm -v 二.建立一个Node.js工程 1.(控制台窗口)全局安装了expres ...
- 无限极分类class
class Category { /** * 返回一维数组 * @static * @param array $cate 要递归的数组 * @param string $html 子级分类前要显示的缩 ...
- dbunit进行DAO层Excel单元测试
DAO层测试难点 可重复性,每次运行单元测试,得到的数据是重复的 独立性,测试数据与实际数据相互独立 数据库中脏数据预处理 不能给数据库中数据带来变化 DAO层测试方法 使用内存数据库,如H2.优点: ...
- 【译】Envoy with Nomad and Consul (一)
原文: http://timperrett.com/2017/05/13/nomad-with-envoy-and-consul 在过去的许多年我的职业生涯一直是围绕着数据中心和平台基础设施.工作范围 ...
- iOS 原生模块 给 Javascript(ReactNative) 发送事件 (通知监听)
官方中文文档是这样描述的: 就给我们这几句话 就打发我们了. 按照上面的写法,根本不知道 - (void)calendarEventReminderReceived:(NSNotificatio ...
- 原生js实现图片网格式渐显、渐隐效果
写正文前先吐槽一下:端午放假完第一天去某千人以上公司面试前端工程师,第一轮是我应聘职位的部门小领导,谈的不错,面试主要围绕要用到的技术来:第二轮来了我要说的正主,我了个去,问的问题一个和前端无关,问我 ...
- RabbitMQ系列教程之二:工作队列(Work Queues)
今天开始RabbitMQ教程的第二讲,废话不多说,直接进入话题. (使用.NET 客户端 进行事例演示) 在第一个教程中,我们编写了一个从命名队列中发送和接收消息的程序. ...
- php+mysql 除了设置主键防止表单提交内容重复外的另一种方法
感觉好久没有更新博客了,一直在做网站及后台,也没有遇到让我觉得可以整理的内容,之前做的一个系统,已经完成了,后来客户又要求加一个功能,大概就是表单提交的时候,约束有一项不能和以前的内容重复,如图 比如 ...
- javaScript 设计模式系列之二:适配器模式
介绍 适配器模式将一个类的接口转接成用户所期待的,有助于避免大规模改写现有客户代码. In software engineering, the adapter pattern is a softwar ...