zoukankan      html  css  js  c++  java
  • 城堡问题

    题目链接http://noi.openjudge.cn/ch0205/1817/

    描述


    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
    

    来源1164

    每一个方块看成是一个节点,两个方块之间没有门,就代表连个节点是相连的。

    此处不需要建图,因为每个方块都保存了与相邻方块的信息,并且每个方块连出去的边最多有四条

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<vector>
    #define DEBUG(x) cout<<#x<<" = "<<x<<endl
    using namespace std;
    int R,C;
    int rooms[60][60];
    int color[60][60];
    int roomSize;
    int maxRoomSize=-1;
    void Dfs(int i,int j)
    {
        if(color[i][j])return;
        roomSize++;
        color[i][j]=1;
        if((rooms[i][j]&1)==0)Dfs(i,j-1);
        if((rooms[i][j]&2)==0)Dfs(i-1,j);
        if((rooms[i][j]&4)==0)Dfs(i,j+1);
        if((rooms[i][j]&8)==0)Dfs(i+1,j);
    }
    int main()
    {
    //    freopen("in.txt","r",stdin);
        scanf("%d%d",&R,&C);
        for(int i=0;i<R ;i++ ){
                for(int j=0;j<C ;j++ ){
                    scanf("%d",&rooms[i][j]);
                }
        }
        int roomNum=0;
        memset(color,0,sizeof(color));
        for(int i=0;i<R ;i++ ){
            for(int j=0;j<C ;j++ ){
                roomSize=0;
                if(!color[i][j]){
                    roomNum++;
                    Dfs(i,j);
                    maxRoomSize=max(roomSize,maxRoomSize);
                }
            }
        }
        printf("%d
    ",roomNum);
        printf("%d
    ",maxRoomSize);
    }
  • 相关阅读:
    Saltstack module acl 详解
    Saltstack python client
    Saltstack简单使用
    P5488 差分与前缀和 NTT Lucas定理 多项式
    CF613D Kingdom and its Cities 虚树 树形dp 贪心
    7.1 NOI模拟赛 凸包套凸包 floyd 计算几何
    luogu P5633 最小度限制生成树 wqs二分
    7.1 NOI模拟赛 dp floyd
    springboot和springcloud
    springboot集成mybatis
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/9204194.html
Copyright © 2011-2022 走看看