The Castle OpenJ_Bailian - 1164

Posted hi3254014978

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Castle OpenJ_Bailian - 1164相关的知识,希望对你有一定的参考价值。

The Castle OpenJ_Bailian - 1164

     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.
Input 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. Output Your program is to write to standard output: First the number of rooms, then the area of the largest room (counted in modules).

Sample Input

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

Sample Output

5
9

分析:

这个题目其实是一个图的遍历统计连通分量个数和最大连通分量结点个数的题目,其难点就是怎将 代表 四面有墙的数字转换成 各个方向有墙,

通过观察可以发现 1/2/4/8 刚好是 二进制 0000 数 在对应位为 1,所以 可以通过采用 该数字与 1, 2,4,8做与运算的方式来判断结点之间是否连通

完整代码如下:

#pragma warning(disable:4996)

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>

using namespace std;


// n*m个单位房间
int n, m;
int room[51][51];
int vis[51][51];

bool inArea(int x, int y)
{
    return (x >= 0 && y >= 0 && x < n && y < m);
}

int area;
void dfs(int i, int j)
{
    vis[i][j] = 1;
    area++;
    if (((room[i][j] & 1) == 0) && inArea(i, j - 1) && !vis[i][j - 1]) dfs(i, j - 1);  // 西面墙,对应了向西探索是否连通
    if (((room[i][j] & 2) == 0) && inArea(i - 1, j) && !vis[i - 1][j]) dfs(i - 1, j);  // 北面墙,对应了向北探索是否连通
    if (((room[i][j] & 4) == 0) && inArea(i, j + 1) && !vis[i][j + 1]) dfs(i, j + 1);  // 东面墙,对一个了向东探索是否连通
    if (((room[i][j] & 8) == 0) && inArea(i + 1, j) && !vis[i + 1][j]) dfs(i + 1, j);  // 南面墙,对应了向南探索是否连通
}


int main()
{
    // freopen("in.txt", "r", stdin);
    while (scanf("%d%d", &n, &m) != EOF)
    {
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                scanf("%d", &room[i][j]);
            }
        }

        int roomCnt = 0;
        int maxRoomArea = -1;
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                if (!vis[i][j])
                {
                    roomCnt++;
                    area = 0;
                    dfs(i, j);
                    if (maxRoomArea < area)
                        maxRoomArea = area;
                }

            }
        }

        cout << roomCnt << endl << maxRoomArea << endl;
    }


    return 0;
}

 

以上是关于The Castle OpenJ_Bailian - 1164的主要内容,如果未能解决你的问题,请参考以下文章

The Castle

USACO 2.1The Castle

p1457 The Castle

搜索DFS之城堡 The Castle

USACO The Castle

洛谷P1457 城堡 The Castle