zoukankan      html  css  js  c++  java
  • POJ 1164

    The Castle
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5557   Accepted: 3129

    Description

         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 #include <iostream>
     3 #include <cstring>
     4 using namespace std;
     5 int map[55][55];
     6 bool vis[55][55];
     7 int area = 0,ans = 0;
     8 int col,row;
     9 void dfs(int i,int j)
    10 {
    11     if(i<1||j<1||i>row||j>col)
    12         return ;
    13     if(vis[i][j])
    14         return ;
    15     else
    16     {
    17         vis[i][j] = true;
    18         ++ans;
    19         //可能好几个方向均没墙,所以不是级联式if语句 
    20         if(map[i][j]%2 == 0) // 1 2 4 8 西边肯定没墙 
    21         {
    22             if(j>=2)
    23                 dfs(i,j-1);
    24         }
    25         map[i][j] /= 2;
    26         if(map[i][j]%2 == 0) // 0 1 2 4 北边肯定没墙 
    27         {
    28             if(i>=2)
    29                 dfs(i-1,j);
    30         }
    31         map[i][j] /= 2;
    32         if(map[i][j]%2 == 0) // 0 0 1 2 东边肯定没墙
    33         {
    34             if(j<=col-1)
    35                 dfs(i,j+1);
    36         }
    37         map[i][j] /= 2;
    38         if(map[i][j]%2 == 0) // 0 0 0 1 南边肯定没墙
    39         {
    40             if(i<=row-1)
    41                 dfs(i+1,j);
    42         }
    43     }
    44 }
    45 int main()
    46 {
    47     while(cin>>row>>col)
    48     {
    49         memset(map,0,sizeof(map));
    50         int i,j;        
    51         for(i=1 ;i<=row ;i++)
    52             for(j=1 ;j<=col ;j++)
    53                 cin>>map[i][j];
    54         ans = 0;
    55         int count = 0;
    56         memset(vis,false,sizeof(vis));
    57         for(i=1 ;i<=row ;i++)
    58             for(j=1 ;j<=col ;j++)
    59             {
    60                 if(!vis[i][j])
    61                 {
    62                      ans = 0;
    63                      dfs(i,j);
    64                      if(ans>area)
    65                          area=ans;
    66                     //联通的小方格算一个房间 
    67                      if(ans)
    68                          ++count;
    69                  }
    70              }
    71         cout<<count<<endl;
    72         cout<<area<<endl;
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    转:一道笔试题-将int型数组强制转换为char*,再求strlen,涉及大小端
    转:git windows中文 乱码问题解决汇总
    git教程1
    刘汝佳黑书 pku等oj题目
    C/C++ qsort()快速排序用法
    char s[]字串和char *s字串有什麼区别?
    c语言‘’ ,‘0’, “0” ,0之间的区别
    带符号的char类型取值范围为什么是-128——127
    c语言memset详解
    【线性规划与网络流24题】孤岛营救问题 分层图
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2644111.html
Copyright © 2011-2022 走看看