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 ...
随机推荐
- 利用gulp搭建简单服务器,gulp标准版
var gulp = require('gulp'), autoprefixer = require('gulp-autoprefixer'), //自动添加css前缀 rename = requir ...
- VR的发展历程-VR全景智慧城市
从1962年有第一台VR开始,到2014年Oculus被Facebook收购为止,VR经历了一个非常漫长的过程.从鲜为人知,到被广泛认识,逐渐走进我们生活.这就是VR形成的时代.在这个时代里,VR设备 ...
- 欧拉函数之HDU1286找新朋友
找新朋友 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
- PHP如何与搜索引擎Elasticsearch交互?
一:参考官方文档 1. Elasticsearch 5.4.0英文手册:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/sear ...
- RFM模型——构建数据库营销的商业战役!(转)
RFM模型:R(Recency)表示客户最近一次购买的时间有多远,F(Frequency)表示客户在最近一段时间内购买的次数,M (Monetary)表示客户在最近一段时间内购买的金额.一般原始数据为 ...
- cookie创建,删除
Cookie 历来指就着牛奶一起吃的点心.然而,在因特网内,“Cookie”这个字有了完全不同的意思.那么“Cookie”到底是什么呢?“Cookie”是小量信息,由网络服务器发送出来以存储在网络浏览 ...
- 使用nodeJS实现前端项目自动化之项目构建和文件合并
前面的话 一般地,我们使用构建工具来完成项目的自动化操作.本文主要介绍如何使用nodeJS来实现简单的项目结构构建和文件合并 项目构建 假设,最终实现的项目名称为'test',结构如下图所示 那么,首 ...
- css3中强大的filter(滤镜)属性
CSS3中强大的filter(滤镜)属性 博主最近在做网站的过程中发现了一个非常强大的CSS3属性,就是filter(滤镜)属性,喜欢p图的朋友看名字都应该知道这是什么神器了吧.当然,这个属性的效果肯 ...
- C#实现SQLSERVER数据库中有序GUID生成(NewSequentialId)
GUID作为数据库主键由于其无序性所以性能不怎么好,SQL Server中有个函数NewSequentialId可以生成有序的GUID,由于在程序中需要用到,就用C#实现了一下,生成的GUID格式基本 ...
- HTML----网页基础和基本标签
网页分类: 1.静态网页:所有内容全写死,都写在源代码中,若修改必须修改源代码,后缀为.html或htm 2.动态网页:内容大部分来自于数据库,可以修改,后缀为.aspx(c#).jsp(java). ...