玉树搜救行动

玉树搜救行动

时间限制(普通/Java):3000MS/10000MS          运行内存限制:65536KByte
总提交:39            测试通过:29

描述

自从玉树受灾以来,有关部门一直在现场抢救落难的人。他们用个种方法搜救,用上了搜救犬,有了搜救犬找到生命迹象就容易了。
 假设现场用一个矩阵表示,抢救的有多条搜救犬,受灾的人也有多个可能。
例子:
#p.d#p#
#####.#
d……..#
######p
d表示搜救狗,p表示受灾的人,点表示可以通行的路,#表示石头挡住的路,不能通行。
搜救狗只能上下左右走,不能越过障碍物。
上面的那个例子最多可以救到2个人。因为第三个人被四周包围搜救狗无法到达。

输入

输入数据有多组。每组两个整数R,C, 2=<R,C<=100, R表示矩阵的行数,C表示矩阵的列数,然后输入相应矩阵,矩阵保证有搜救狗和受灾的人。当输入R=0且C=0时候输入结束。

输出

输出搜救狗最多能救多少受灾的人的数量。

样例输入

4 7
#p.d#p#
#####.#
d.....#
######p
0 0

样例输出

2

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<vector>
using std::vector;

const size_t MAX_SIZE_X = 100;                                //地图大小
const size_t MAX_SIZE_Y = 100;                                //地图大小
const short int MAX_DIRECTION = 4;                            //遍历方向个数
const short int DIRECTION_X[MAX_DIRECTION] = { 0, 0, 1, -1 };
const short int DIRECTION_Y[MAX_DIRECTION] = { -1, 1, 0, 0 };

class Point{
public:
    int x, y;
    Point(size_t& t_x, size_t& t_y){
        x = t_x;
        y = t_y;
    }
};

class Map{
public:
    char sign;
    bool has_reached;
    void set(void){
        cin >> sign;
        has_reached = false;
    }
};

Map map[MAX_SIZE_X][MAX_SIZE_Y];
size_t size_x, size_y;
vector<Point> dog_list;
vector<Point> people_list;

void set_map(void){
    size_t x, y;
    for (x = 0; x != size_x; ++x){
        for (y = 0; y != size_y; ++y){
            map[x][y].set();
            if (map[x][y].sign == 'p'){
                people_list.push_back(Point(x, y));
            }
            else if (map[x][y].sign == 'd'){
                dog_list.push_back(Point(x, y));
            }
        }
    }
}

void dfs(const size_t& x, const size_t& y){
    size_t current_x, current_y;
    for (short int direction(0); direction != MAX_DIRECTION; ++direction){
        current_x = x + DIRECTION_X[direction];
        current_y = y + DIRECTION_Y[direction];
        if ((current_x >= 0 && current_x < size_x)
            && (current_y >= 0 && current_y < size_y)
            && map[current_x][current_y].sign != '#'
            && !map[current_x][current_y].has_reached){
            map[current_x][current_y].has_reached = true;
            dfs(current_x, current_y);
        }
    }
}

int main(void){
    int sum;
    while (cin >> size_x >> size_y, size_x && size_y){
        dog_list.clear();
        people_list.clear();
        sum = 0;
        set_map();
        for (vector<Point>::iterator it(dog_list.begin()); it != dog_list.end(); ++it){
            dfs(it->x, it->y);
        }
        for (vector<Point>::iterator it(people_list.begin()); it != people_list.end(); ++it){
            if (map[it->x][it->y].has_reached){
                ++sum;
            }
        }
        cout << sum << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值