Rescue 3解法:(1.DFS 2. BFS 3.BFS+优先队列模板)
来源:互联网 发布:淘宝卖家信用度怎么看 编辑:程序博客网 时间:2024/06/07 02:02
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
#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std; int n, m;char map[205][205];int sx, sy;bool flag;struct node { int x, y, step; bool operator <(const node & t) const { return step>t.step; }};int dx[]= {-1,0,0,1};int dy[]= {0,-1,1,0}; void bfs() { node now, tmp; int i,xx,yy; priority_queue<node> q; now.x = sx, now.y = sy, now.step = 0; map[sx][sy] = '#'; q.push(now); while(!q.empty()) { now = q.top(); q.pop();// cout<<now.x<<" "<<now.y<<" "<<now.step<<endl; for(i=0; i<4; i++) { xx = now.x +dx[i]; yy = now.y +dy[i]; if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue; if(map[xx][yy]=='r') { cout<<now.step+1<<endl; flag = true; return ; } if(map[xx][yy]=='x') { tmp.x =xx, tmp.y = yy, tmp.step = now.step+2; q.push(tmp); } else { tmp.x =xx, tmp.y = yy, tmp.step = now.step+1; q.push(tmp); } map[xx][yy] = '#'; } }} int main() { int i, j; while(~scanf("%d%d",&n,&m)) { for(i=0; i<n; i++) for(j=0; j<m; j++) { cin>>map[i][j]; if(map[i][j]=='a') sx=i,sy=j; } flag = false; bfs(); if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std; int n, m;char map[205][205];int sx, sy;bool flag;struct node { int x, y, step; bool operator <(const node & t) const { return step>t.step; }};int dx[]= {-1,0,0,1};int dy[]= {0,-1,1,0}; void bfs() { node now, tmp; int i,xx,yy; priority_queue<node> q; now.x = sx, now.y = sy, now.step = 0; map[sx][sy] = '#'; q.push(now); while(!q.empty()) { now = q.top(); q.pop();// cout<<now.x<<" "<<now.y<<" "<<now.step<<endl; for(i=0; i<4; i++) { xx = now.x +dx[i]; yy = now.y +dy[i]; if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue; if(map[xx][yy]=='r') { cout<<now.step+1<<endl; flag = true; return ; } if(map[xx][yy]=='x') { tmp.x =xx, tmp.y = yy, tmp.step = now.step+2; q.push(tmp); } else { tmp.x =xx, tmp.y = yy, tmp.step = now.step+1; q.push(tmp); } map[xx][yy] = '#'; } }} int main() { int i, j; while(~scanf("%d%d",&n,&m)) { for(i=0; i<n; i++) for(j=0; j<m; j++) { cin>>map[i][j]; if(map[i][j]=='a') sx=i,sy=j; } flag = false; bfs(); if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
BFS+优先队列,这是效率相对最高的算法,因为利用小根堆,每次都是让最小的出队列判断,所以只要找到r即为答案
#include <iostream>#include <stdio.h>#include <cstring>#include <queue>using namespace std;char map[205][205];int vis[205][205];int m,n;int d[4][2]= {0,-1,0,1,-1,0,1,0};struct node{ int x,y; int time; friend bool operator<(const node &a,const node &b)//运算符重载对优先队列里的小于号进行的重载 { return a.time>b.time;//时间小的先出队 }};int bfs(int x,int y){ node st,nt; priority_queue<node> q; st.x=x; st.y=y; st.time=-1; q.push(st); vis[st.x][st.y]=1; while(!q.empty()) { st=q.top(); q.pop(); if(map[st.x][st.y]=='r') return st.time; for(int i=0; i<4; i++) { nt.x=st.x+d[i][0]; nt.y=st.y+d[i][1]; if(!vis[nt.x][nt.y] && nt.x>=0 && nt.x<m && nt.y>=0 && nt.y<n && map[nt.x][nt.y]!='#') { vis[nt.x][nt.y]=1; if(map[nt.x][nt.y]=='.') nt.time=st.time+1; else nt.time=st.time+2; q.push(nt); } } } return -1;}int main(){ int x,y,count; while(scanf("%d%d",&m,&n)!=EOF) { memset(vis,0,sizeof(vis)); count=-1; for(int i=0; i<m; i++) scanf("%s",map[i]); for(int i=0; i<m; i++) for(int j=0; j<n; j++) if(map[i][j]=='a') { x=i; y=j; break; } count=bfs(x,y); if(count==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",count); } return 0;}
反思:多对题目进行思考,采用时间效率较高的算法,不要因为题目的AC排名而顾虑太多,知识需要掌握,个人能力有所差距
- Rescue 3解法:(1.DFS 2. BFS 3.BFS+优先队列模板)
- Rescue (BFS 优先队列)
- Rescue(优先队列+bfs)
- rescue(BFS+优先队列)
- hdu 1242Rescue(bfs+优先队列)
- HDU1242 Rescue(优先队列加BFS)
- hdu 1242 Rescue (优先队列+bfs)
- HDU 1242 Rescue(BFS +优先队列)
- 1242Rescue (优先队列BFS)
- HDU 1242 Rescue(BFS,优先队列)
- hdu 1242 Rescue(BFS优先队列)
- HDU 1242 Rescue(优先队列+bfs)
- hdu 1242 Rescue(BFS+优先队列)
- HDU 1242 Rescue(BFS+优先队列)
- hdoj 1242 Rescue (bfs+优先队列)
- hdu 1242 Rescue (BFS+优先队列)
- HDU 1242 Rescue (BFS+优先队列)
- hdu1242 Rescue(优先队列bfs)
- jdk8中的forEach使用return执行下一次遍历
- FTP自动化C#(Microsoft.Web.Administration)开发
- MySQL各版本的区别
- Problem 8 Largest product in a series
- springMVC_基本开发及配置
- Rescue 3解法:(1.DFS 2. BFS 3.BFS+优先队列模板)
- 【在线演示】专业Excel 组件Spire.XLS 在线演示合集
- 修改MySQL的默认字符编码
- FFMPEG合两帧YUV420P为一帧
- E
- MySQL 慢查询设置和分析工具
- iOS平台Socket编程实践(一)
- vs 工作技巧
- 备忘