首先看一下题目。

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的更多相关文章

  1. USACO Section2.1 The Castle 解题报告

    castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  2. 【USACO 2.1】The Castle

    /* TASK: castle LANG: C++ SOLVE: 深搜,注意每个方向对应值.枚举去掉的墙,然后再dfs,注意墙要复原,并且dfs里要判断是否超出边界. */ #include<c ...

  3. USACO Section 2.1 The Castle

    /* ID: lucien23 PROG: castle LANG: C++ */ /********************************************************* ...

  4. USACO Section 2.1 The Castle 解题报告

    题目 题目描述 有一个城堡,城堡中有若干个房间,房间与房间之间用墙来进行分隔.现在我们需要统计这个城堡有多少个房间,并且还要找出最大的房间的面积是多少(一个单元格就代表一个单元面积).城堡的主人现在想 ...

  5. USACO 2.1 The Castle

    题目大意:给你一个城堡让你求有多少房间,最大房间有多大,敲掉一堵墙后最大的房间有多大,敲掉那座墙 思路:比较恶心的bfs题,反正就是bfs使劲敲 /*{ ID:a4298442 PROB:castle ...

  6. USACO castle

    <pre name="code" class="cpp"><pre>USER: Kevin Samuel [kevin_s1] TASK ...

  7. [USACO Section 2.1]城堡 The Castle (搜索)

    题目链接 Solution 比较恶心的搜索,思路很简单,直接广搜找联通块即可. 但是细节很多,要注意的地方很多.所以直接看代码吧... Code #include<bits/stdc++.h&g ...

  8. Usaco Training [2.1] The Castle 搜索

    传送门 题目的输出的4个信息 前两个很容易,dfs,bfs都可以,图怎么建都可以 后两个在搜索的时候记录belong[i][j]和已有的size即可 代码应该比不少题解清晰吧 #include < ...

  9. 洛谷P1457 城堡 The Castle

    P1457 城堡 The Castle 137通过 279提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 我们憨厚的USACO ...

随机推荐

  1. 第三章 霍夫变换(Hough Transform)

    主要内容: 霍夫变换的作用 霍夫变换检测直线的原理 霍夫变换检测圆的原理 OpenCV中的霍夫变换 1.霍夫变换检测直线原理 霍夫变换,英文名称Hough Transform,作用是用来检测图像中的直 ...

  2. maven(03)

    修改本地库路径 windows下maven默认路径应该是${user.home}/.m2/repository 修改方法:找到maven安装的根路径,里面有一个conf的文件夹,打开里面有一个sett ...

  3. spring-boot整合dubbo:Spring-boot-dubbo-starter

    为什么要写这个小工具 如果你用过Spring-boot来提供dubbo服务,相信使用中有很多"不爽"的地方.既然使用spring boot,那么能用注解的地方绝不用xml配置,这才 ...

  4. JAVA的HashTable源码分析

    Hashtable简介 Hashtable同样是基于哈希表实现的,同样每个元素是一个key-value对,其内部也是通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长.Hashtable ...

  5. C#在属性中用Lambda语法

    今天看代码改功能的时候遇到了个比较奇怪的地方,在属性里也能用Lambda,我看了好久,也不是很理解,我都开始怀疑这是不是属性了,又在群里讨论了下这个问题,觉得有必要记下来,因为又涨知识了. 问题1:这 ...

  6. linux通配符与正则表达式

    通配符   *  任意字符,可重复多次     ? 任意字符,重复一次     [] 代表一个字符 举例: [a,b,c] 表示abc中任意一个 通配符的作用是用来匹配文件名的 正则表达式 正则表达式 ...

  7. Java基础知识二次学习--第八章 流

    第八章 流   时间:2017年4月28日11:03:07~2017年4月28日11:41:54 章节:08章_01节 视频长度:21:15 内容:IO初步 心得: 所有的流在java.io包里面 定 ...

  8. 关于微信分享JSSDK使用需注意的几点问题

    微信公众平台技术文档中有微信JS-SDK说明文档,详情见地址https://mp.weixin.qq.com/wiki 官方给出了使用步骤和DEMO,下面说一下几点需要注意的问题. 1.登录微信公众平 ...

  9. 纯css实现多标签浮动居中(任意个数)

    在做的一个网页上有一块要用浮动标签,具体就是网页底部有未知数量,未知尺寸的元素要水平居中,有点类似于分页器. 首先,我们先新建一个容器con,就是标签的爸爸,用来控制标签在页面的位置,.father{ ...

  10. CCS Debug Assertion Failed

    下载安装CCS7.1后编译工程时报错,如下: 本来以为这种情况是由于CCS没有安装成功所导致的,但尝试安装其他版本时也发生同样的问题. 于是登录到 TI的wiki 上查找原因,在安装栏下说明: Ens ...