hdu 题目1242 Rescue (BFS)
来源:互联网 发布:c语言实例编写小游戏 编辑:程序博客网 时间:2024/05/01 22:10
Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 80 Accepted Submission(s) : 39
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
第一次使用priority_queue<>Q;
需要设置优先级
friend bool operator< (node n1,node n2){ return n2.step < n1.step;//注意这里大小关系 }
题目需要从目标地址‘a’ 开始四个方向广搜,(因为‘r’) 可能有好多个,只要搜到 r 则停止返回搜索步数,
注意步数的加减多少,,, ‘x’ 要加 2 ,用到优先队列,将路径最短的作为对头出队
#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>using namespace std;char map[211][211];int n,m,s_x,s_y,e_x,e_y;struct node{ int x,y; int step; friend bool operator< (node n1,node n2){ return n2.step < n1.step; }};int dir[4][2]={1,0,-1,0,0,1,0,-1};int ok(int x,int y){ if(x<0 || x>=n || y<0 || y>=m || map[x][y]==1) return 0; return 1;} bool bfs(){ priority_queue <node> q; node cur,next; int i; cur.x = s_x; cur.y=s_y; cur.step =0; map[cur.x][cur.y] = 1; q.push(cur); while(!q.empty()){ cur = q.top(); q.pop(); if(cur.x== e_x && cur.y== e_y) { printf("%d\n",cur.step); return 1; } for(i=0;i<4;i++){ next.x = cur.x + dir[i][0]; next.y = cur.y + dir[i][1]; if(ok(next.x,next.y)){ if(map[next.x][next.y]==-1) next.step = cur.step+2; else next.step = cur.step+1; map[next.x][next.y] = 1; q.push(next); } } } return 0;}int main(){ int i,j; char s[211]; while(scanf("%d%d",&n,&m)!=-1) { memset(map,0,sizeof(map)); for(i=0;i<n;i++) { scanf("%s",s); for(j=0;s[j];j++) { if(s[j]=='r') {s_x=i;s_y=j;} else if(s[j]=='a'){e_x =i;e_y=j;} else if(s[j]=='#')map[i][j]=1; else if(s[j]=='.')map[i][j]=0; else if(s[j]=='x')map[i][j]=-1; } } if(!bfs()) printf("Poor ANGEL has to stay in the prison all his life.\n" ); } return 0;}
- hdu 题目1242 Rescue (BFS)
- hdu 1242 Rescue(bfs)
- HDU-#1242 Rescue(BFS)
- Rescue(BFS) HDU 1242
- HDOJ 题目1242 Rescue(BFS)
- hdu 1242 Rescue (bfs)
- HDU 1242 Rescue BFS
- HDU--1242:Rescue (BFS)
- HDU 1242:Rescue 【bfs】
- hdu 1242 Rescue(bfs)
- HDU-1242-Rescue【BFS】
- HDU 1242 BFS-Rescue
- 【BFS】hdu 1242 Rescue
- hdu 1242Rescue(bfs+优先队列)
- hdu 1242 Rescue (优先队列+bfs)
- hdu 1242 Rescue(BFS入门)
- HDU 1242 Rescue(BFS +优先队列)
- HDU 1242 Rescue(BFS,优先队列)
- C语言 南阳理工ACM 14 会场安排问题
- (IOS系列)——TextFile属性设置
- 有点意思的C/C++问题及解答:1-5
- HDU--1242 -- Rescue [广搜]
- C语言 南阳理工ACM 47 过河问题
- hdu 题目1242 Rescue (BFS)
- HDU3790:最短路径问题
- Quartz 2.2 Table Sql Script of MySQL InnoDB
- HDU4676 Sum Of Gcd
- hdu 4681 String/杭电多校第八场1006 最长公共子序列长
- The media formats currently supported by JavaFX
- HDU2072 单词数 解题报告--set
- forward和redirect的区别
- 参数化查询----防止SQL注入