您的位置:首页 > 其它

USACO Section 2.1 The Castle (dfs)

2012-08-30 13:54 369 查看
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

题意:输入一个地图,统计里面有多少连通的房间房间,最大的连通数,如果拆一道墙之后最大的连通数是多少并且输出所拆的房间矩阵的编号和拆的那面墙。规定拆的房间满足时最西边的然后满足最南边的,对于每间房间首先考虑拆西边,然后再拆南边,再然后拆北边,最后考虑拆东边。1表示西边又墙,2表示北边有墙,4表示东边有墙,8表示南边有墙。每个房间的有几面墙就是这些数字之和。

分析:使用二进制运算
第一步:使用dfs标记整个地图 flag[][] 属于哪个连通图;
第二步:统计每个连通图的大小存入 size[];
第三步:枚举删墙。

心得:这个题目搞了一个上午了,就是这个输出的方向没搞明白

View Code

/*
ID: dizzy_l1
LANG: C++
TASK: castle
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 60

using namespace std;

bool visited[MAXN][MAXN];
int map[MAXN][MAXN],flag[MAXN][MAXN],size[MAXN*MAXN];
int n,m,cnt;

void dfs_flag(int i,int j)
{
flag[i][j]=cnt;
visited[i][j]=true;
if(j>1&&!visited[i][j-1]&&(map[i][j]&1))
dfs_flag(i,j-1);
if(i<n&&!visited[i+1][j]&&(map[i][j]&8))
dfs_flag(i+1,j);
if(j<m&&!visited[i][j+1]&&(map[i][j]&4))
dfs_flag(i,j+1);
if(i>1&&!visited[i-1][j]&&(map[i][j]&2))
dfs_flag(i-1,j);
}

int count_size()
{
int i,j,ans;
memset(size,0,sizeof(size));
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
size[flag[i][j]]++;
ans=0;
for(i=1; i<=cnt; i++)
if(ans<size[i])
ans=size[i];
return ans;
}

int ans_cnt,ans_max1,ans_max2,ans_i,ans_j;
char ans_p;

void work_ans()
{
int i,j;
ans_max2=0;
for(j=1; j<=m; j++)
{
for(i=n; i>=1; i--)
{
int t;
map[i][j]^=15;
if(j>1&&(map[i][j]&1))
{
if(flag[i][j-1]!=flag[i][j])
t=size[flag[i][j-1]]+size[flag[i][j]];
else
t=size[flag[i][j]];
if(ans_max2<t)
{
ans_max2=t;
ans_i=i;
ans_j=j;
ans_p='W';
}
}
if(i<n&&(map[i][j]&8))
{
if(flag[i+1][j]!=flag[i][j])
t=size[flag[i+1][j]]+size[flag[i][j]];
else
t=size[flag[i][j]];
if(ans_max2<t)
{
ans_max2=t;
ans_i=i;
ans_j=j;
ans_p='S';
}
}
if(i>1&&(map[i][j]&2))
{
if(flag[i-1][j]!=flag[i][j])
t=size[flag[i-1][j]]+size[flag[i][j]];
else
t=size[flag[i][j]];
if(ans_max2<t)
{
ans_max2=t;
ans_i=i;
ans_j=j;
ans_p='N';
}
}
if(j<m&&(map[i][j]&4))
{
if(flag[i][j+1]!=flag[i][j])
t=size[flag[i][j+1]]+size[flag[i][j]];
else
t=size[flag[i][j]];
if(ans_max2<t)
{
ans_max2=t;
ans_i=i;
ans_j=j;
ans_p='E';
}
}
}
}
}

int main()
{
freopen("castle.in","r",stdin);
freopen("castle.out","w",stdout);
int i,j;
while(scanf("%d %d",&m,&n)==2)
{
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
scanf("%d",&map[i][j]),map[i][j]^=15;
memset(flag,0,sizeof(flag));
memset(visited,false,sizeof(visited));
cnt=0;
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
if(!visited[i][j])
{
cnt++;
dfs_flag(i,j);
}
ans_cnt=cnt;
ans_max1=count_size();
work_ans();
printf("%d\n",ans_cnt);
printf("%d\n%d\n",ans_max1,ans_max2);
printf("%d %d %c\n",ans_i,ans_j,ans_p);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: