HDU 1242.Rescue

Rescue
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. 

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards. 

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.) 
 

Input

First line contains two integers stand for N and M. 

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

Process to the end of the file. 
 

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
 

Sample Input

7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
 

Sample Output

13
 

 

根据friend和friends,我们可以推知angel有多个朋友

也就是说,这是一个有一个入口多个出口的迷宫

从angel所在位置开始BFS,队列使用优先队列,确保按照距离层数访问节点

 

(第一次蜜汁RE)

  1 /*
  2 By:OhYee
  3 Github:OhYee
  4 Email:oyohyee@oyohyee.com
  5 Blog:http://www.cnblogs.com/ohyee/
  6 
  7 かしこいかわいい?
  8 エリーチカ!
  9 要写出来Хорошо的代码哦~
 10 */
 11 
 12 #include <cstdio>
 13 #include <algorithm>
 14 #include <cstring>
 15 #include <cmath>
 16 #include <string>
 17 #include <iostream>
 18 #include <vector>
 19 #include <list>
 20 #include <queue>
 21 #include <stack>
 22 #include <map>
 23 using namespace std;
 24 
 25 //DEBUG MODE
 26 #define debug 0
 27 
 28 //循环
 29 #define REP(n) for(int o=0;o<n;o++)
 30 
 31 
 32 const int maxn = 210;
 33 
 34 int N,M;
 35 char Map[maxn][maxn];
 36 int dis[maxn][maxn];
 37 
 38 struct point {
 39     int x,y;
 40     int dis;
 41     point(int a,int b,int c) {
 42         x = a;
 43         y = b;
 44         dis = c;
 45     }
 46     bool operator < (const point &rhs)const {
 47         return dis > rhs.dis;
 48     }
 49 };
 50 
 51 
 52 const int delta[] = {1,-1,0,0};
 53 
 54 int BFS(int s1,int s2) {
 55     priority_queue<point> Q;
 56     bool visited[maxn][maxn];
 57     memset(visited,false,sizeof(visited));
 58     memset(dis,-1,sizeof(dis));
 59 
 60     Q.push(point(s1,s2,0));
 61     dis[s1][s2] = 0;
 62 
 63     while(!Q.empty()) {
 64         int x = Q.top().x;
 65         int y = Q.top().y;
 66         int dist = Q.top().dis;
 67 
 68         Q.pop();
 69 
 70         if(visited[x][y] == true)
 71             continue;
 72         visited[x][y] = true;
 73 
 74         //拓展节点
 75         REP(4) {
 76             int xx = x + delta[o];
 77             int yy = y + delta[3 - o];
 78 
 79             if(xx >= 0 && xx < N && yy >= 0 && yy < M && Map[xx][yy] != '#' && visited[xx][yy] == false) {
 80                 int dd = dist + 1;
 81 
 82                 if(Map[xx][yy] == 'x')
 83                     dd++;
 84 
 85                 dis[xx][yy] = ((dis[xx][yy] == -1) ? dd : min(dd,dis[xx][yy]));
 86 
 87                 if(Map[xx][yy] == 'r')
 88                     return dis[xx][yy];
 89 
 90                 Q.push(point(xx,yy,dis[xx][yy]));
 91 
 92             }
 93 
 94         }
 95 
 96     }
 97 
 98     return -1;
 99 
100 }
101 
102 bool Do() {
103     if(scanf("%d%d",&N,&M) == EOF)
104         return false;
105 
106     int s1,s2;
107     for(int i = 0; i < N; i++)
108         for(int j = 0; j < M; j++) {
109             scanf("\n%c\n",&Map[i][j]);
110             if(Map[i][j] == 'a') {
111                 Map[i][j] = '.';
112                 s1 = i;
113                 s2 = j;
114             }
115         }
116 
117     #if debug
118     for(int i = 0; i < N; i++) {
119         for(int j = 0; j < M; j++)
120             printf("%c",Map[i][j]);
121         printf("\n");
122     }
123     #endif
124 
125     int ans = BFS(s1,s2);
126 
127     if(ans == -1)
128         printf("Poor ANGEL has to stay in the prison all his life.\n");
129     else
130         printf("%d\n",ans);
131 
132     return true;
133 }
134 
135 int main() {
136     while(Do());
137     return 0;
138 }
蜜汁RE的代码

 

 

下面是重新写了一遍的AC代码

  1 /*
  2 By:OhYee
  3 Github:OhYee
  4 Email:oyohyee@oyohyee.com
  5 Blog:http://www.cnblogs.com/ohyee/
  6 
  7 かしこいかわいい?
  8 エリーチカ!
  9 要写出来Хорошо的代码哦~
 10 */
 11 
 12 #include <cstdio>
 13 #include <algorithm>
 14 #include <cstring>
 15 #include <cmath>
 16 #include <string>
 17 #include <iostream>
 18 #include <vector>
 19 #include <list>
 20 #include <queue>
 21 #include <stack>
 22 #include <map>
 23 using namespace std;
 24 
 25 //DEBUG MODE
 26 #define debug 0
 27 
 28 //循环
 29 #define REP(n) for(int o=0;o<n;o++)
 30 
 31 const int maxn = 250;
 32 int n,m;
 33 char Map[maxn][maxn];
 34 
 35 int s1,s2;
 36 
 37 int dis[maxn][maxn];
 38 
 39 const int delta[] = {1,-1,0,0};
 40 
 41 struct point {
 42     int x,y;
 43     int dis;
 44 
 45     point(int a,int b,int c) {
 46         x = a;
 47         y = b;
 48         dis = c;
 49     }
 50 
 51     bool operator < (const point &rhs)const {
 52         return dis>rhs.dis;
 53     }
 54 };
 55 
 56 int BFS() {
 57     priority_queue<point> Q;
 58     memset(dis,-1,sizeof(dis));
 59 
 60     Q.push(point(s1,s2,0));
 61     dis[s1][s2] = 0;
 62     while(!Q.empty()) {
 63         int x = Q.top().x;
 64         int y = Q.top().y;
 65         int dist = Q.top().dis;
 66 
 67         Q.pop();
 68 
 69         REP(4) {
 70             int xx = x + delta[o];
 71             int yy = y + delta[3 - o];
 72             int dd = dist + 1;
 73 
 74             if(xx >= 0 && xx < n && yy>=0 && yy < m) {
 75                 if(Map[xx][yy] != '#' && dis[xx][yy] == -1) {
 76                     if(Map[xx][yy] == 'x')
 77                         dd++;
 78                     if(Map[xx][yy] == 'r')
 79                         return dd;
 80                     dis[xx][yy] = dd;
 81                     Q.push(point(xx,yy,dd));
 82                 }
 83             }
 84         }
 85     }
 86     return -1;
 87 }
 88 
 89 bool Do() {
 90     if(scanf("%d%d",&n,&m) == EOF)
 91         return false;
 92     for(int i = 0;i < n;i++)
 93         for(int j = 0;j < m;j++) {
 94             scanf("\n%c\n",&Map[i][j]);
 95             if(Map[i][j] == 'a') {
 96                 s1 = i;
 97                 s2 = j;
 98             }
 99         }
100 
101     int ans = BFS();
102     if(ans == -1)
103         printf("Poor ANGEL has to stay in the prison all his life.\n");
104     else
105         printf("%d\n",ans);
106 
107     return true;
108 }
109 
110 int main() {
111     while(Do());
112     return 0;
113 }

 

posted @ 2016-04-21 00:14  OhYee  阅读(149)  评论(0编辑  收藏  举报