usaco2.1.1——castle

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
从现在开始也要附带原文了。。。
读英文题目还是有好处的 。。。。
 
 

描述

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

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

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

请仔细研究下面这个有注解的城堡平面图:

     1   2   3   4   5   6   7
   #############################
 1 #   |   #   |   #   |   |   #
   #####---#####---#---#####---#   
 2 #   #   |   #   #   #   #   #
   #---#####---#####---#####---#
 3 #   |   |   #   #   #   #   #   
   #---#########---#####---#---#
 4 # ->#   |   |   |   |   #   #   
   #############################
# =墙壁    -,| = 没有墙壁
-> =指向一面墙,这面墙推掉的话我们就有一间最大的新房间

友情提示,这个城堡的平面图是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: 城堡的平面图用一个由数字组成的矩阵表示,一个数字表示一个单位,矩阵有N行M列。输入与样例的图一致。

每一个单位的数字告诉我们这个单位的东西南北是否有墙存在。每个数字是由以下四个整数的某个或某几个或一个都没有加起来的。

1: 在西面有墙

2: 在北面有墙

4: 在东面有墙

8: 在南面有墙

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

(file castle.in)

第 1 行: 二个被空格分开的整数: M 和 N(N,M<=50)

第 2 到 N+1 行: M x N 个整数,每行M个。

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
 

分析:由题意,我们先对读入的图进行预处理,

记录墙的位置,然后dfs一下,求一下连通块的数量和最大连通块的面积,print,

然后我们来拆墙,

从左下角向右上角逐列枚举墙,

(因为要求墙最靠西,其次最靠南,即,优先拆北墙,再拆东墙)

并判断记录。

that's all….

本来一直有畏难情绪。。。。没想秒过了。。。

 

{
ID: codeway3
PROG: castle
LANG: PASCAL
}
program castle;
  var
    i,j,n,m,k,l,js,max,recx,recy,recbb:longint;
    a:array[0..51,0..51,1..5]of longint;
    size:array[-1..10000]of longint;

  procedure fill(x,y,val:longint);
    var
      i,j:longint;
    begin
      a[x,y,5]:=val;
      inc(size[val]);
      if (a[x,y,2]=0)and(a[x-1,y,5]=0) then fill(x-1,y,val);
      if (a[x,y,1]=0)and(a[x,y-1,5]=0) then fill(x,y-1,val);
      if (a[x,y,3]=0)and(a[x,y+1,5]=0) then fill(x,y+1,val);
      if (a[x,y,4]=0)and(a[x+1,y,5]=0) then fill(x+1,y,val);
    end;

  begin
    assign(input,'castle.in');
    reset(input);
    assign(output,'castle.out');
    rewrite(output);
    fillchar(a,sizeof(a),0);
    readln(m,n);
    for i:=1 to n do begin a[i,0,5]:=-1;a[i,m+1,5]:=-1;end;
    for i:=1 to m do begin a[0,i,5]:=-1;a[n+1,i,5]:=-1;end;

    for i:=1 to n do
      for j:=1 to m do
        begin
          read(k);
          if k>=8 then begin k:=k-8;a[i,j,4]:=1;end;
          if k>=4 then begin k:=k-4;a[i,j,3]:=1;end;
          if k>=2 then begin k:=k-2;a[i,j,2]:=1;end;
          if k>=1 then begin k:=k-1;a[i,j,1]:=1;end;
        end;

    for i:=1 to n do
      for j:=1 to m do
        if a[i,j,5]=0 then begin
          inc(js);
          fill(i,j,js);
        end;

    writeln(js);
    max:=0;
    for i:=1 to js do if size[i]>max then max:=size[i];
    writeln(max);

    max:=0;
    for j:=1 to m do
      begin
        for i:=n downto 2 do
          if (a[i,j,2]=1)and(a[i,j,5]<>a[i-1,j,5])and(size[a[i,j,5]]+size[a[i-1,j,5]]>max) then
            begin
              max:=size[a[i,j,5]]+size[a[i-1,j,5]];
              recx:=i;recy:=j;
              recbb:=1;
            end;
        if j<>m then
          for i:=n downto 1 do
            if (a[i,j,3]=1)and(a[i,j,5]<>a[i,j+1,5])and(size[a[i,j,5]]+size[a[i,j+1,5]]>max) then
              begin
                max:=size[a[i,j,5]]+size[a[i,j+1,5]];
                recx:=i;recy:=j;
                recbb:=2;
              end;
      end;
    writeln(max);
    write(recx,' ',recy,' ');
    if recbb=1 then writeln('N')
      else writeln('E');
    close(input);
    close(output);
  end.

posted on 2011-12-25 17:48  codeway3  阅读(282)  评论(0编辑  收藏  举报

导航