castle_usaco2.1.1_codevs3102_dfs

来源:互联网 发布:html5 javascript 联系 编辑:程序博客网 时间:2024/06/07 04:52

DESCRIPTION

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 west2: wall to the north4: wall to the east8: 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.

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.

这里是翻译

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

  1. 在西面有墙为1
  2. 在北面有墙为2
  3. 在东面有墙为4
  4. 在南面有墙为8

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

求:

  1. 城堡的房间数目。
  2. 最大的房间的大小
  3. 移除一面墙能得到的最大的房间的大小
  4. 移除哪面墙可以得到面积最大的新房间。

选择最靠西的,最靠南的墙。同一格子北墙比东墙更优。
用该墙的南邻单位的北墙或西邻单位的东墙来表示这面墙,输出邻近单位的行数、列数和墙的方位(”N”(北)或者”E”(东))。

题解

一开始就被样例解释吓到了,内心:这尼玛不应该是2.0章的水题合集么!!!

仔细看了一下,先bfs或dfs搜出前两个答案,至于3和4就记录一下每个格子所属的房间,以及每个房间的大小。枚举格子北边和东边的墙,找两边加起来最大的就好了

c++神坑,忘了数组是从0开始的了,@re:zero

源码

/*ID:wjp13241PROG:castleLANG:C++*/#include <stdio.h>#include <cstring>using namespace std;struct Answer{    int x,y,num,dir;};int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0},d[4]={1,2,4,8};int wall[51][51][4];int map[51][51];int t[2601];int n,m;void dfs(int x,int y){    t[t[0]]++;    map[x][y]=t[0];    for (int k=0;k<4;k++)    {        int i=x+dx[k],j=y+dy[k];        if (!map[i][j]&&!wall[x][y][k])            dfs(i,j);    }}int main(){    freopen("castle.in","r",stdin);    freopen("castle.out","w",stdout);    memset(map,-1,sizeof(map));    memset(t,0,sizeof(t));    scanf("%d%d",&m,&n);    for (int i=1;i<=n;i++)        for (int j=1;j<=m;j++)        {            int tmp;            scanf("%d",&tmp);            for (int k=3;k>=0;k--)                if (tmp>=d[k])                {                    tmp-=d[k];                    wall[i][j][k]=1;                }            map[i][j]=0;        }    Answer ans=(Answer){0,0,0,0};    for (int i=1;i<=n;i++)        for (int j=1;j<=m;j++)            if (!map[i][j])            {                t[0]++;                dfs(i,j);                ans.num=t[t[0]]>ans.num?t[t[0]]:ans.num;            }    printf("%d\n",t[0]);    printf("%d\n",ans.num);    ans=(Answer){0,0,0,0};    for (int j=1;j<=m;j++)        for (int i=n;i>=1;i--)            for (int k=1;k<=2;k++)            {                int x=i+dx[k];                int y=j+dy[k];                if ((x>0)&&(y<=m)                  &&(map[x][y]!=map[i][j])                  &&(t[map[x][y]]+t[map[i][j]]>ans.num))                {                    ans=(Answer){i,j,t[map[x][y]]+t[map[i][j]],k};                }            }    printf("%d\n",ans.num);    printf("%d %d ",ans.x,ans.y);    if (ans.dir==1)        printf("N\n");    else        printf("E\n");    fclose(stdin);    fclose(stdout);    return 0;}
1 0