Rescue(优先队列+bfs)
来源:互联网 发布:社会生存法则 知乎 编辑:程序博客网 时间:2024/05/21 09:40
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31144 Accepted Submission(s): 10926
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
/**************************************************************Problem: RescueLanguage: C/C++method: 优先队列+bfs date:2017 7 18create by Songdan_Lee****************************************************************/#include<stdio.h>#include<string.h>#include<queue>using namespace std;int dir[4][2]={-1,0,1,0,0,-1,0,1};char map[205][205];int vis[205][205];int n,m; typedef struct Node{ int x; int y; int step; bool operator < (const Node &a) const { return step>a.step;//最小值优先 } }ss; typedef struct Node1{ int x;int y; }position; position start,end1;void bfs(int x,int y,int total){priority_queue<ss>q;ss hd,hd1;memset(vis,0,sizeof(vis));vis[x][y]=1;hd.x=x;hd.y=y;hd.step=total;q.push(hd);while(!q.empty()){hd1=q.top();q.pop();if(map[hd1.x][hd1.y]=='a'){printf("%d\n",hd1.step+1);return ;}for(int i=0;i<4;i++){hd.x=hd1.x+dir[i][0];hd.y=hd1.y+dir[i][1];if(hd.x<0||hd.y<0||hd.x>=m||hd.y>=n||vis[hd.x][hd.y]||map[hd.x][hd.y]=='#')continue;if(map[hd.x][hd.y]=='.')hd.step=hd1.step+1;if(map[hd.x][hd.y]=='x')hd.step+hd.step+2;vis[hd.x][hd.y]=1;q.push(hd);}} printf("Poor ANGEL has to stay in the prison all his life.\n"); } int main(){ while(scanf("%d%d",&m,&n)!=EOF){ if(m==0&&n==0) break; 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') {end1.x=i; end1.y=j;}if(map[i][j]=='r'){start.x=i;start.y=j;} }bfs(start.x,start.y,0); } }
阅读全文
0 0
- 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)
- HDU-1242-Rescue(优先队列+BFS)
- Play framework 响应response类型
- PYTHON解析XML的多种方式效率对比实测
- linux上环境的搭建
- Spring Boot项目部署(Linux、maven)
- 几种C++ std::string和std::wstring相互转换的转换方法
- Rescue(优先队列+bfs)
- 访问有授权验证的 Redis
- mybatis oracle下不同表空间表名重复与批量删除,新增
- 微信小程序初学一周
- [UVa1625]颜色的长度
- php无法获取angularjs post数据
- ubuntu16.04 x64上nginx源码安装
- 每日一发Python---Python中双冒号的作用[::]
- 动态内存