首先看一下题目。

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. MySQL的SELECT ...for update

    最近的项目中,因为涉及到Mysql数据中乐观锁和悲观锁的使用,所以结合项目和网上的知识点对乐观锁和悲观锁的知识进行总结. 悲观锁介绍 悲观锁是对数据被的修改持悲观态度(认为数据在被修改的时候一定会存在 ...

  2. Automated Front End Test - Xvfb, Chromedriver, Selenium, Jenkins

    1. Install Xvfbm, google-chrome-stable and chromedriver in Jenkins sudo apt-get install -y xvfb goog ...

  3. OpenCV探索之路(十四):绘制点、直线、几何图形

    绘制点和圆 void cvCircle( CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int li ...

  4. List集合数据太多进行分批,List的subList方法应用

    List<String> mStrings=new ArrayList<>(); //初始化 for (int i = 0; i < 1020; i++) { mStri ...

  5. URL的标准格式

    URL的标准格式 scheme://host:port/path?query#fragment 1.  scheme:协议 2. host:主机 3. port:端口 4. path:路径 5. qu ...

  6. TWaver 2D+GIS+3D的试用和在线Demo

    TWaver 2D for HTML5试用下载: http://download.servasoft.com/dl/twaver/sssyuwyeriUR/k/twaver-html5-5.4.7.z ...

  7. 不知道Linux的mysql的root密码

    用了好几次了这个方法.记一下: 1.停止Mysql /etc/init.d/mysql stop 或者(根据安装配置的位置不同,而不同) /etc/init.d/mysqld stop 2.进入Mys ...

  8. python小工具:用python操作HP的Quality Center (二)----- 用异步方式提高速度

    上接第一篇 http://www.cnblogs.com/sdet/p/6874631.html 在python中,很简单地能把http请求通过异步的方式发送,以下代码在python 3.6.0上运行 ...

  9. C语言集成开发环境vs2017的使用技巧之修改快捷键

    首先这里是说编辑C语言内容,其次开发环境是vs2017(全称:visual studio 2017).像这个开发环境体积大,但你安装的时候不要安装到C盘,然后安装的时候选择模块,比如你不开发网站,就先 ...

  10. 从RGB色转为灰度色算法

    一.基础  对于彩色转灰度,有一个很著名的心理学公式: Gray = R*0.299 + G*0.587 + B*0.114 二.整数算法 而实际应用时,希望避免低速的浮点运算,所以需要整数算法. 注 ...