玉树搜救行动
玉樹搜救行動
時間限制(普通/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; }
總結
- 上一篇: 自适应模糊神经网络的设计
- 下一篇: 高新技术企业认定四大要素知多少?