【OPENJUDGE】【搜索】2.5-166,1718 城堡问题

题目

166

描述

             1   2   3   4   5   6   7  
           #############################
         1 #   |   #   |   #   |   |   #
           #####---#####---#---#####---#
         2 #   #   |   #   #   #   #   #
           #---#####---#####---#####---#
         3 #   |   |   #   #   #   #   #
           #---#########---#####---#---#
         4 #   #   |   |   |   |   #   #
           #############################
                (Figure 1)
                #= Wall   
                |= No wall
                -= No wall

Figure 1 shows the map of a castle.Write a program that calculates
1. how many rooms the castle has
2. how big the largest room is
The castle is divided into m * n (m<=50, n<=50) square modules. Each such module can have between zero and four walls.

输入

Your program is to read from standard input. The first line contains the number of modules in the north-south direction and the number of modules in the east-west direction. In the following lines each module is described by a number (0 <= p <= 15). This number is the sum of: 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. The castle always has at least two rooms.

输出

Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).

样例

输入

4
7
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

输出

5
9

1718

描述

     1   2   3   4   5   6   7  
   #############################
 1 #   |   #   |   #   |   |   #
   #####---#####---#---#####---#
 2 #   #   |   #   #   #   #   #
   #---#####---#####---#####---#
 3 #   |   |   #   #   #   #   #
   #---#########---#####---#---#
 4 #   #   |   |   |   |   #   #
   #############################
           (图 1)

   #  = Wall   
   |  = No wall
   -  = No wall

图1是一个城堡的地形图。请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。城堡被分割成mn(m≤50,n≤50)个方块,每个方块可以有0~4面墙。

输入

程序从标准输入设备读入数据。第一行是两个整数,分别是南北向、东西向的方块数。在接下来的输入行里,每个方块用一个数字(0≤p≤50)描述。用一个数字表示方块周围的墙,1表示西墙,2表示北墙,4表示东墙,8表示南墙。每个方块用代表其周围墙的数字之和表示。城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。输入的数据保证城堡至少有两个房间。

输出

城堡的房间数、城堡中最大房间所包括的方块数。结果显示在标准输出设备上。

样例

输入

4
7
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

输出

5
9

看着两道题是否很相近(look like the same)呢?
没错,它们是同一道题目(IOI 1994)只不过,1817是166的翻译版本,不信你去翻一下。

解法

首先,这两道题的每一个房间都表示的是它四面墙的总和。
其次,这道题涉及到求连通块和和最大连通块所包含的房间数。
最后,每面内墙被计算了两次。

所以,我们要使用dfs。

First Step

我们需要处理墙。

最好的办法是使用一个bool数组来记录墙出现的次数。
我们还要记录房间的编号,第几个房间。

综上所述,我们要使用结构体。

struct node{
    bool f[5];
    int t,count;
}map[M][M];

Second Step

不知道大家发现没有,

西墙--1(10)=00000000 00000000 00000000 00000001(2)
北墙--2(10)=00000000 00000000 00000000 00000010(2)
东墙--4(10)=00000000 00000000 00000000 00000100(2)
南墙--8(10)=00000000 00000000 00000000 00001000(2)

所以,我们可以用位运算来计算墙。
我们可以使用&与>>计算。

&运算符特点
只有对应二进制位都为1时,结果中对应的二进制位为1。
奇数&1=1,偶数&1=0。

综上所述,我们可以得出下面的代码:

void change(node &x)
{
    int k=0;
    while(x.t)
    {
        x.f[k]=x.t&1;
        x.t=x.t>>1;
        k++;
    }
}

Last Step

这道题要求最大连通块所占的方块数,所以我们要记录最大值。
我们可以在dfs结束时比较,也可以最后查找一遍。

代码

由上面的三步我们可得出下面的代码。

#include<cstdio>
const int M=52;
struct node{
    bool f[5];
    int t,count;
}map[M][M];
int dr[4][2]={{0,-1},{-1,0},{0,1},{1,0}},n,m,cnt,max=-1e8,s;
void change(node &x)
{
    int k=0;
    while(x.t)
    {
        x.f[k]=x.t&1;
        x.t=x.t>>1;
        k++;
    }
}
void dfs(int x,int y,int step)
{
    if(map[x][y].t)return;
    map[x][y].t=step;
    map[x][y].count=s;
    s++;
    for(int i=0;i<4;i++)
        if(!map[x][y].f[i])
            dfs(x+dr[i][0],y+dr[i][1],step);
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&map[i][j].t);
            change(map[i][j]);
        }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(!map[i][j].t)
            {
                s=1;
                dfs(i,j,++cnt);
            }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            max=max>map[i][j].count?max:map[i][j].count;
    printf("%d\n%d\n",cnt,max);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37656398/article/details/74637838