poj 1164 The Castle

/*
B:城堡问题
poj 1164 the castle
    查看
    提交
    统计
    提问

总时间限制:
    1000ms
内存限制:
    65536kB

描述


         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
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).

*/

#include <cstdio>
#include <cstdlib>
using namespace std;

int r = 0, c = 0;
int wall[55][55] = {0};
int colored[55][55] = {0};

int color(int a, int b)
{
    int sum = 1;
    colored[a][b] = 1;
    if(wall[a][b] >= 8) wall[a][b] -= 8;
    else if(!colored[a + 1][b]) sum += color(a + 1, b);
    if(wall[a][b] >= 4) wall[a][b] -= 4;
    else if(!colored[a][b + 1]) sum += color(a, b + 1);
    if(wall[a][b] >= 2) wall[a][b] -= 2;
    else if(!colored[a - 1][b]) sum += color(a - 1, b);
    if(wall[a][b] >= 1) wall[a][b] -= 1;
    else if(!colored[a][b - 1]) sum += color(a, b - 1);
    return sum;
}


int main()
{
    int num = 0, m = 0;
    scanf("%d%d", &r, &c);
    for(int i = 0; i < r; ++i)
        for(int j = 0; j < c; ++j)
            scanf("%d", &wall[i][j]);
    for(int i = 0; i < r; ++i)
    for(int j = 0; j < c; ++j)
    {
        if(!colored[i][j])
        {
            num++;
            int t = color(i, j);
            if(m < t) m = t;
        }
    }
    printf("%d\n%d\n", num, m);
    return 0;
}

转载于:https://my.oschina.net/locusxt/blog/134882

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值