您的位置:首页 > 其它

[USACO2.1]城堡 The Castle

2017-06-16 14:47 330 查看
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


The Castle城堡

IOI'94 - Day 1


目录

 [隐藏
1 描述
2 格式
3 SAMPLE
INPUT
4 SAMPLE
OUTPUT


[编辑]描述

我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张“幸运爱尔兰”(一种彩票)。结果这张彩票让他获得了这次比赛唯一的奖品——坐落于爱尔兰郊外的一座梦幻般的城堡!

喜欢吹嘘的农夫约翰立刻回到有着吹嘘传统的威斯康辛老家开始吹嘘了, 农夫约翰想要告诉他的奶牛们关于他城堡的一切。他需要做一些吹嘘前的准备工作:比如说知道城堡有多少个房间,每个房间有多大。另外,农夫约翰想要把一面单独的墙(指两个单位间的墙)拆掉以形成一个更大的房间。 你的工作就是帮农夫约翰做以上的准备,算出房间数与房间的大小。

城堡的平面图被划分成M*N(1 <=M,N<=50)个正方形的单位,一个这样的单位可以有0到4面墙环绕。城堡周围一定有外墙环绕以遮风挡雨。(就是说平面图的四周一定是墙。)

请仔细研究下面这个有注解的城堡平面图:
0   1   2   3   4   5   6
#############################
0 #
4000
|   #   |   #   |   |   #
#####---#####---#---#####---#
1 #   #   |   #   #   #   #   #
#---#####---#####---#####---#
2 #   |   |   #   #   #   #   #
#---#########---#####---#---#
3 # ->#   |   |   |   |   #   #
#############################

# =墙壁    -,| = 没有墙壁
-> =指向一面墙,这面墙推掉的话我们就有一间最大的新房间


友情提示,这个城堡的平面图是7×4个单位的。一个“房间”的是平面图中一个由“#”、“-”、“|”围成的格子(就是图里面的那一个个的格子)。比如说这个样例就有5个房间。(大小分别为9、7、3、1、8个单位(排名不分先后))

移去箭头所指的那面墙,可以使2个房间合为一个新房间,且比移去其他墙所形成的房间都大。(原文为: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. )

城堡保证至少有2个房间,而且一定有一面墙可以被移走。


[编辑]格式

PROGRAM NAME: castle

INPUT FORMAT: 第一行有两个整数:M和N 城堡的平面图用一个由数字组成的矩阵表示,一个数字表示一个单位,矩阵有N行M列。输入与样例的图一致。

每一个单位的数字告诉我们这个单位的东西南北是否有墙存在。每个数字是由以下四个整数的某个或某几个加起来的(四面都没有墙的话,这个数字应该为0)。
1: 在西面有墙
2: 在北面有墙
4: 在东面有墙
8: 在南面有墙


城堡内部的墙会被规定两次。比如说(1,1)南面的墙,亦会被标记为(2,1)北面的墙。

OUTPUT FORMAT:

(file castle.out)

输出包含如下4行:

第 1 行: 城堡的房间数目。

第 2 行: 最大的房间的大小

第 3 行: 移除一面墙能得到的最大的房间的大小

第 4 行: 移除哪面墙可以得到面积最大的新房间。

选择最佳的墙来推倒。有多解时选最靠西的,仍然有多解时选最靠南的。同一格子北边的墙比东边的墙更优先。

用该墙的南邻单位的北墙或西邻单位的东墙来表示这面墙,方法是输出邻近单位的行数、列数和墙的方位("N"(北)或者"E"(东))。


[编辑]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


[编辑]SAMPLE
OUTPUT

5
9
16
4 1 E


【题解】
呜呜~~..错误的写法(没写完).希望下次不再走眼。。
/*~~~~(>_<)~~~~ 手残了题意理解错误,一个单位不是指一(空)个(格)字(数)符(量)...我去我去,面积单位居然是“一个单位”。我去。。我的手...*/
/*
ID:luojiny1
LANG:C++
TASK:castle
*/
#include<cstdio>
const int maxn=50+10;
char buf[maxn*2+1][maxn*4+1];
int n,m,input,cnt=1,color[maxn*2+1][maxn*4+1]={0},size[maxn]={0},MaxSize=0;
void show(){
for(int i=0;i<n*2+1;i++)
{
for(int j=0;j<m*4+1;j++)printf("%c",buf[i][j]);
printf("\n");
}
}
void init()
{
for(int i=0;i<n*2+1;i++)
for(int j=0;j<m*4+1;j++)if(i==0||j==0||j==m*4||i==n*2)buf[i][j]='#';else buf[i][j]=' ';
}
int dr[4]={1,-1,0,0};
int dc[4]={0,0,1,-1};
void dfs(int r,int c,int id)
{
color[r][c]=id;
size[id]++;
printf("%d\n",size[id]);
for(int i=0;i<4;i++)
{
int x=r+dr[i],y=c+dc[i];
if(x>=0&&x<n*2+1&&y>=0&&y<m*4+1&&buf[x][y]==' '&&color[x][y]==0)dfs(x,y,id);
}
}
int main()
{
//freopen("castle.in","r",stdin);
//freopen("castle.out","w",stdout);
scanf("%d%d",&m,&n);
init();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
scanf("%d",&input);
switch(input)
{
case 1:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
break;
case 2:
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
break;
case 3:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
break;
case 4:
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
break;
case 5:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
break;
case 6:
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
break;
case 7:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
break;
case 8:
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
case 9:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
case 10:
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
case 11:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
case 12:
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
case 13:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
case 14:
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
case 15:
buf[i*2+1][(j-1)*4]='#';
buf[i*2][(j-1)*4]='#';
buf[i*2+2][(j-1)*4]='#';
buf[i*2][j*4]='#';
buf[i*2][j*4+1]='#';
buf[i*2][j*4+2]='#';
buf[i*2][j*4+3]='#';
buf[i*2][j*4+4]='#';
buf[i*2][j*4+4]='#';
buf[i*2+1][j*4+4]='#';
buf[i*2+2][j*4+4]='#';
buf[i*2+2][j*4]='#';
buf[i*2+2][j*4+1]='#';
buf[i*2+2][j*4+2]='#';
buf[i*2+2][j*4+3]='#';
buf[i*2+2][j*4+4]='#';
break;
}
}
show();
for(int i=0;i<n*2+1;i++)
for(int j=0;j<m*4+1;j++){
if(buf[i][j]==' '&&color[i][j]==0)
{
printf("i=%d j=%d\n",i,j);
dfs(i,j,cnt);
if(size[cnt]>MaxSize)MaxSize=size[cnt];
cnt++;
}
}
printf("%d\n",MaxSize);
return 0;
}正确解

求联通分块,大概这就是flood fill 。。恐怖。lrj的书里有... switch写的有点长。水平有限,我看看能否简化了。。

/*
ID:luojiny1
LANG:C++
TASK:castle
*/
#include<cstdio>
const int maxn=50+10;
bool up[maxn][maxn],down[maxn][maxn],left[maxn][maxn],right[maxn][maxn];
int n,m,input,cnt=1,color[maxn][maxn]={0},size[maxn*maxn]={0},MaxSize=0,ans=0,x,y,ans2=0;
int dr[4]={0,1,-1,0};
int dc[4]={-1,0,0,1};
char dir[]="WSNE";
void dfs(int r,int c,int id)
{
if(color[r][c])return;
color[r][c]=id;
size[id]++;
// printf("%d\n",size[id]);
if(up[r][c]==0&&r-1>=0)dfs(r-1,c,id);
if(down[r][c]==0&&r+1<n)dfs(r+1,c,id);
if(left[r][c]==0&&c-1>=0)dfs(r,c-1,id);
if(right[r][c]==0&&c+1<m)dfs(r,c+1,id);
}

int main()
{
freopen("castle.in","r",stdin);
freopen("castle.out","w",stdout);
scanf("%d%d",&m,&n);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
scanf("%d",&input);
switch(input)
{
case 1:
left[i][j]=1;
break;
case 2:
up[i][j]=1;
break;
case 3:
left[i][j]=1;
up[i][j]=1;
break;
case 4:
right[i][j]=1;
break;
case 5:
left[i][j]=1;
right[i][j]=1;
break;
case 6:
up[i][j]=1;
right[i][j]=1;
break;
case 7:
left[i][j]=1;
up[i][j]=1;
right[i][j]=1;
break;
case 8:
down[i][j]=1;
break;
case 9:
left[i][j]=1;
down[i][j]=1;
break;
case 10:
up[i][j]=1;
down[i][j]=1;
break;
case 11:
left[i][j]=1;
up[i][j]=1;
down[i][j]=1;
break;
case 12:
right[i][j]=1;
down[i][j]=1;
break;
case 13:
left[i][j]=1;
right[i][j]=1;
down[i][j]=1;
break;
case 14:
up[i][j]=1;
right[i][j]=1;
down[i][j]=1;
break;
case 15:
left[i][j]=1;
right[i][j]=1;
down[i][j]=1;
up[i][j]=1;
break;
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(color[i][j]==0)
{
dfs(i,j,cnt++);
if(size[cnt-1]>MaxSize)MaxSize=size[cnt-1];
}

}
for(int j=0;j<m;j++)
for(int i=n-1;i>=0;i--)
{
for(int k=0;k<4;k++)
{
int r=i+dr[k],c=j+dc[k];
if(r<0||r>=n||c<0||r>=m||color[i][j]==color[r][c])continue;
int sum=size[color[i][j]]+size[color[r][c]];
if(sum>ans){
ans=sum;x=i;y=j;ans2=k;
}
}
}
printf("%d\n%d\n%d\n%d %d %c\n",cnt-1,MaxSize,ans,x+1,y+1,dir[ans2]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: