1754. 逃离洞穴
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
04级ZEH和YYR到一个洞穴探险,其中YYR进入洞穴探险,而ZEH在洞外附近利用自发研制的X-1探测器监测洞穴的喷气口是否开始喷射毒气。洞穴里面有若干个毒气喷射口,已知毒气的扩散速度是每单位时间向上下左右扩散一格,而YYR的逃逸速度也是每单位时间向上下左右移动一格。所有的毒气喷射口都是同时喷发的,当监测到喷射时,ZEH就马上通知YYR逃离,YYR必须在不遇到毒气的情况下尽快逃离洞穴。亲爱的师弟师妹,你们可以帮他们计算一下YYR最快逃离洞穴的时间吗?
Input
洞穴地图用矩阵表示,符号. 表示通路,符号X表示障碍物,符号E表示洞口,符号D表示毒气喷射口所在地,符号P表示YYR当前的位置。输入首先是两个整数m, n(2<=m,n<=1000)表示矩阵的大小。接下来是m行n列的矩阵图。矩阵描述之外的地方都是障碍物且只有一个逃离的洞口,但可能有多个毒气喷射口,且扩散到洞口的毒气立即消散。输入有多组数据,当m,n是0 0时表示输入结束。
Output
如果YYR能够顺利逃离洞穴,则输出最快逃离洞口的时间,否则输出“YYR is extremely dangerous!”
Sample Input
5 10 XXXXX..XXX XXXE...XXX XX.......X X......P.X XD.....XXX 5 10 XXXXX..XXX XXXE...XXX XX.......X X......P.X X.D....XXX 0 0
Sample Output
6YYR is extremely dangerous!
// Problem#: 1754 // Submission#: 3326115 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <string.h> #include <vector> #include <iomanip> #include <map> #include <stack> #include <functional> #include <list> #include <cmath> using namespace std; const int MAX_W = 1005; bool visGas[MAX_W][MAX_W]; bool visP[MAX_W][MAX_W]; char G[MAX_W][MAX_W]; int H, W; int Ei, Ej; queue<pair<int, int> > gas; queue<pair<int, int> > q; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; int step; bool isValidGas(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W && !visGas[i][j] && (G[i][j] == '.' || G[i][j] == 'P'); } bool isValidP(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W && !visP[i][j] && G[i][j] == '.'; } void GAS() { int s = gas.size(); while (s--) { pair<int, int> p = gas.front(); gas.pop(); for (int i = 0; i < 4; i++) { int nextI = p.first + dir[i][0]; int nextJ = p.second + dir[i][1]; if (isValidGas(nextI, nextJ)) { G[nextI][nextJ] = 'D'; visGas[nextI][nextJ] = true; gas.push(make_pair(nextI, nextJ)); } } } } bool BFS() { step = 0; while (!q.empty()) { int s = q.size(); GAS(); step++; while (s--) { pair<int, int> p = q.front(); q.pop(); //if (G[p.first][p.second] == 'D') continue; for (int i = 0; i < 4; i++) { int nextI = p.first + dir[i][0]; int nextJ = p.second + dir[i][1]; if (nextI == Ei && nextJ == Ej) return true; if (isValidP(nextI, nextJ)) { visP[nextI][nextJ] = true; q.push(make_pair(nextI, nextJ)); } } } } return false; } int main() { //std::ios::sync_with_stdio(false); while (1) { cin >> H >> W; if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) { cin >> G[i]; for (int j = 0; j < W; j++) { if (G[i][j] == '.') visP[i][j] = visGas[i][j] = false; if (G[i][j] == 'D') { gas.push(make_pair(i, j)); visGas[i][j] = true; } if (G[i][j] == 'P') { q.push(make_pair(i, j)); visP[i][j] = true; } if (G[i][j] == 'E') { Ei = i; Ej = j; } } } if (BFS()) cout << step << endl; else cout << "YYR is extremely dangerous!" << endl; while (!gas.empty()) gas.pop(); while (!q.empty()) q.pop(); } //getchar(); //getchar(); return 0; }