zoukankan      html  css  js  c++  java
  • The Castle OpenJ_Bailian

    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;
    }
  • 相关阅读:
    recon-ng打开后显示No modules enabled/installed
    mysql的floor()报错注入方法详细分析
    视频合并时使用python批量修改文件名
    ThinkPHP5框架引入的css等外部资源文件没有生效
    用session实现的用户登陆,客户端是怎样获取到cookie信息的
    关于Linux系统下基于Tomcat部署和升级war包的详细过程
    nginx代理的页面性能优化大全
    linux下MySQL数据库的定时备份与定时删除
    关于linux查询内存,CPU,存储空间和日志查询的的常用命令及参数讲解
    linux 查看cpu核心数
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/12401439.html
Copyright © 2011-2022 走看看