HDU 1242 rescue (优先队列模板题)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24205 Accepted Submission(s): 8537
Problem 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.)
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.
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
Author
CHEN, Xue
Source
Recommend
这题有很多坑点,
1朋友不止一个,所以r有多个,好解决,只要从a搜,找到第一个r结束即可
2警卫会消耗2点时间,但是队列是最先入队的, 最先入队的是步数最少的, 并不是时间最少的,所以必须要用优先队列,下面这段代码虽然AC但是是不正确的
//伪ac代码 #include<math.h> #include<stdio.h> #include<queue> #include<string.h> #include<iostream> #include<algorithm> using namespace std; #define N 1234 struct point { int x,y,t; }st; int n,m; int dx[]={1,-1,0,0}; int dy[]={0,0,1,-1}; char mat[N][N]; int vis[N][N]; int bfs() { queue<point>q; q.push(st);vis[st.x][st.y]=1; while(!q.empty()) { point cur=q.front(); q.pop(); for(int i=0;i<4;i++) { point next=cur; next.x+=dx[i];next.y+=dy[i]; if(next.x<1||next.x>n||next.y<1||next.y>m)continue; if(mat[next.x][next.y]=='#'||vis[next.x][next.y]==1)continue; if(mat[next.x][next.y]=='.')next.t=next.t+1; if(mat[next.x][next.y]=='x')next.t=next.t+2; if(mat[next.x][next.y]=='r')return next.t+1; q.push(next);vis[next.x][next.y]=1; } } return -1; } int main() { while(~scanf("%d%d",&n,&m)) { memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf(" %c",&mat[i][j]); if(mat[i][j]=='a') st.x=i,st.y=j,st.t=0; } int ans=bfs(); if(ans==-1)puts("Poor ANGEL has to stay in the prison all his life."); else cout<<ans<<endl; } return 0; }
但面对这组数据:
2 10 axxxxxxxxr ..........
结果就不对了
这个问题能够用优先队列解决。 详见代码
优先队列版:
#include<queue> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define N 1234 int n, m, a, b, x, y, t, cnt, ans; struct node { int x, y, time; bool operator < (const node & t)const { return time > t.time; //改成<号 则较大的先出队 } }st; priority_queue<node>q; //加上前缀 priority_ char mat[N][N]; int v[N][N]; int dx[]={1, -1, 0, 0}; int dy[]={0, 0, 1, -1}; int bfs() { memset(v ,0 , sizeof(v)); while(!q.empty()) q.pop(); q.push(st); v[st.x][st.y] = true; while(!q.empty()) { st = q.top(); //优先队列用q.top() 代替 q.front(); q.pop(); for(int i = 0; i < 4; i++) { node next = st; next.x += dx[i], next.y +=dy[i]; if(v[next.x][next.y] || mat[next.x][next.y]=='#') continue; if(next.x<1 || next.y<1 || next.x>n || next.y>m) continue; if(mat[next.x][next.y] == '.')next.time++; if(mat[next.x][next.y] == 'x')next.time+=2; if(mat[next.x][next.y] == 'r') return next.time+1; q.push(next); v[next.x][next.y] = true; } } return -1; } int main() { while(cin>>n>>m) { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { scanf(" %c", &mat[i][j]); if(mat[i][j] == 'a') st.x = i, st.y = j, st.time = 0; } ans = bfs(); if(ans == -1)puts("Poor ANGEL has to stay in the prison all his life."); else cout<<ans<<endl; } return 0; } /* 7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........ 2 10 axxxxxxxxr .......... 3 10 axxxxxxxxr .########. .......... */
这个可以作为模板。