POJ1164——The Castle

2021/4/17 18:55:19

本文主要是介绍POJ1164——The Castle,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目来自:http://poj.org/problem?id=1164

Description

     1   2   3   4   5   6   7  

   #############################

#   |   #   |   #   |   |   #

   #####---#####---#---#####---#

#   #   |   #   #   #   #   #

   #---#####---#####---#####---#

#   |   |   #   #   #   #   #

   #---#########---#####---#---#

#   #   |   |   |   |   #   #

   #############################

(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

Source

IOI 1994   作者解释:输入一个长m,宽n的矩阵,每个矩阵中的数代表当前房间的墙,1(西墙),2(北墙),4(东墙),8(南墙),数字有可能是这4个数的和。求出房间个数和最大房间大小。 作者分析:深度优先搜索(DFS),朝着连通的地方搜索。
#include <iostream>
#include <cstring>
using namespace std;

int n,m,a[51][51],ans = 0,maxn = -1,cnt = 0,id[51][51];
bool vis[51][51];
void dfs(int x,int y){
    if (vis[x][y]) return ;
    if (x > n || x < 1 || y > m || y < 1) return ;
    vis[x][y] = true;
    ans++;
    id[x][y] = cnt;
    if (a[x][y] == 14){dfs(x,y - 1);}
    if (a[x][y] == 13){dfs(x - 1,y);}
    if (a[x][y] == 12){dfs(x,y - 1);dfs(x - 1,y);}
    if (a[x][y] == 11){dfs(x,y + 1);}
    if (a[x][y] == 10){dfs(x,y - 1);dfs(x,y + 1);}
    if (a[x][y] == 9){dfs(x - 1,y);dfs(x,y + 1);}
    if (a[x][y] == 8){dfs(x,y - 1);dfs(x - 1,y);dfs(x,y + 1);}
    if (a[x][y] == 7){dfs(x + 1,y);}
    if (a[x][y] == 6){dfs(x,y - 1);dfs(x + 1,y);}
    if (a[x][y] == 5){dfs(x - 1,y);dfs(x + 1,y);}
    if (a[x][y] == 4){dfs(x - 1,y);dfs(x,y - 1);dfs(x + 1,y);}
    if (a[x][y] == 3){dfs(x,y + 1);dfs(x + 1,y);}
    if (a[x][y] == 2){dfs(x,y - 1);dfs(x,y + 1);dfs(x + 1,y);}
    if (a[x][y] == 1){dfs(x - 1,y);dfs(x,y + 1);dfs(x + 1,y);}
    if (a[x][y] == 0){dfs(x - 1,y);dfs(x,y + 1);dfs(x + 1,y);dfs(x,y - 1);}
}
int main(){
    memset(vis,false,sizeof(vis));
    cin >> n >> m;
    for (int i = 1;i <= n;i++){
        for (int j = 1;j <= m;j++){
            cin >> a[i][j];
        }
    }
    for (int i = 1;i <= n;i++){
        for (int j = 1;j <= m;j++){
            if (!vis[i][j]){
                cnt++;
                ans = 0;
                dfs(i,j);
                maxn = max(ans,maxn);
            }
        }
    }
    cout << cnt << endl << maxn;
    return 0;
}


这篇关于POJ1164——The Castle的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程