首先看一下题目。

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. Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦

    个人博客 对于技术人员来说,拥有自己的个人博客应该是一件令人向往的事情,可以记录和分享自己的观点,想到这件事就觉得有意思,但是刚开始写博客的时候脑海中是没有搭建个人博客这一想法的,因为刚起步的时候连我 ...

  2. DNA比对算法:BWT

    DNA比对算法:BWT BWT算法,实质上是前缀树的一种实现.那么什么是前缀树呢? 一.前缀树 对于问题p in S?如果S=rpq,那么p为S前缀rp的一个后缀. 于是,为了判断p in S 是否成 ...

  3. Springboot基础篇

    Springboot可以说是当前最火的java框架了,非常适合于"微服务"思路的开发,大幅缩短软件开发周期. 概念 过去Spring充满了配置bean的xml文件,随着spring ...

  4. Spring——Web应用中的IoC容器创建(WebApplicationContext根应用上下文的创建过程)

    基于Spring-4.3.7.RELEASE Spring的配置不仅仅局限在XML文件,同样也可以使用Java代码来配置.在这里我使用XML配置文件的方式来粗略地讲讲WebApplicationCon ...

  5. Eclipse设置问题:字体大小、修改注释内容、修改快捷键

    一.设置字体大小,看下图,包括了设计代码字体大小和控制台输出字体大小 二.修改注释内容 选择window---->>preferences 选择Java---->>code s ...

  6. PHP如何与搜索引擎Elasticsearch交互?

    一:参考官方文档 1. Elasticsearch 5.4.0英文手册:https://www.elastic.co/guide/en/elasticsearch/reference/5.4/sear ...

  7. 【二】python内置类型

    1.布尔类型 表示真假的类型(true和false) 数字 0.None,以及元素为空的容器类对象都可视作False,反之为 True. In [1]: bool(0) Out[1]: False I ...

  8. Angular随笔第二课

    一.  列表表格以及其它迭代型元素 ng-repeat 可能是最有用的angular指令了,它可以根据集合中的项目一次创建一组元素的多份拷贝.不管在什么地方,只要你想创建一组事物的列表,你就可以使用这 ...

  9. angular自定义验证 ngModel的一些理解

    每次使用自定义校验都不记得具体详情,故而记录之 1.数据流向 初始化 -->$formatters -->modelValue-->用户操作-->viewValue--> ...

  10. java基础(四章)

    一.             switch结构(开关语句)的语法 switch(表达式 ){ ------- [dream1]类型为int.char case  常量1 :    ---------[ ...