(简单)搜索 HOJ 1075 The Castle

The Castle

My Tags  (Edit)
Source : IOI 1994
Time limit : 1 secMemory limit : 32 M

Submitted : 492, Accepted : 134

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.

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.

There are multiple testcase, process to the end of file.

SAMPLE INPUT

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 wall farther to the west (and then, if still tied, farthest to the south). 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. 

Print a blank line after each testcase.

SAMPLE OUTPUT

5
9
16
4 1 E


题意:有一个大房间,里面用墙分割了很多个小房间,求房间个数,最大房间面积, 删除那一面墙能使新形成的房间的面积最大

思路:随便从一个点开始dfs,dfs结束后,那么他遍历的点就是一个房间,在深搜的时候把节点做好相应的标记,然后对该标记统计面积。这样前两个问题技能解决了,最后一个问题,因为要输出的墙是最西且最南,所以我们枚举墙的时候从西到东,从南到北,每个位置,先看北面的墙,再看东面的墙,注意这里顺序不要反了。

代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<deque>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
#define MAX 100+10
#define MOD 99997
const int inf = 0xfffffff;

int graph[MAX][MAX];
bool vis[MAX][MAX];
int size[MAX][MAX];
int num[MAX][MAX];


int N,M;
int cnt;
int sz;
void find_room(int r,int c)
{
if (vis[r][c]) return;
vis[r][c] = true;
++sz;
num[r][c] = cnt;
if (!(graph[r][c]&1))
find_room(r,c-1);
if (!(graph[r][c]&2))
find_room(r-1,c);
if (!(graph[r][c]&4))
find_room(r,c+1);
if (!(graph[r][c]&8))
find_room(r+1,c);
}

int wall_r,wall_c;
char wall_direct;
int max_combine_sz;
void find_wall()
{
for (int j = 1 ; j <= M ; ++j)
{
for (int i = N ; i >= 1 ; --i)
{
if (num[i][j]!=num[i-1][j] && graph[i][j]&2 && max_combine_sz < size[i][j]+size[i-1][j])
{
max_combine_sz = size[i][j]+size[i-1][j];
wall_r = i;
wall_c = j;
wall_direct = 'N';
}
if (num[i][j]!=num[i][j+1] && graph[i][j]&4 && max_combine_sz < size[i][j]+size[i][j+1])
{
max_combine_sz = size[i][j]+size[i][j+1];
wall_r = i;
wall_c = j;
wall_direct = 'E';
}
}
}
}

int main()
{
while (scanf("%d%d",&M,&N)==2)
{
memset(graph,0,sizeof(graph));
for (int i = 1 ; i <= N ; ++i)
for (int j = 1 ; j <= M ; ++j)
scanf("%d",&graph[i][j]);
cnt = 0;
int max_sz = 0;
memset(size,0,sizeof(size));
memset(vis,0,sizeof(vis));
for (int i = 1;  i <= N ; ++i)
{
for (int j = 1 ; j <= M ; ++j) if (!vis[i][j])
{
sz = 0;
++cnt;
find_room(i,j);
for (int ii = 1 ; ii <= N ; ++ii)
for (int jj = 1 ; jj <= M ; ++jj) if (num[ii][jj]==cnt)
size[ii][jj] = sz;
if (max_sz < sz) max_sz = sz;
}
}

printf("%d\n%d\n",cnt,max_sz);
max_combine_sz = 0;
find_wall();
printf("%d\n%d %d %c\n\n",max_combine_sz,wall_r,wall_c,wall_direct);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值