广搜 — — ZOJ 1649 Rescue
来源:互联网 发布:电视直播软件下线了? 编辑:程序博客网 时间:2024/05/16 05:08
Rescue
题目描述
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.)
输入
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.
输出
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."
样例输入
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
样例输出
13
可以参考拯救公主一题
从天使的朋友的位置开始进行广搜
运行时间 :15ms
#include<stdio.h>#include<iostream>#include<string.h>#include<queue>using namespace std;int n,m,vis[201][201];char Map[201][201];const int c[4][2]={-1,0,1,0,0,1,0,-1};struct node{int x,y;int step;friend bool operator < (node a,node b){return a.step > b.step;}};priority_queue<node> q;int bfs(){node tmp;int px,py,kx,ky,res;while(!q.empty()){tmp=q.top();q.pop();px=tmp.x;py=tmp.y;res=tmp.step;for(int i=0;i<4;i++){kx=px+c[i][0];ky=py+c[i][1];if(!vis[kx][ky] && kx>=0 && kx<n && ky>=0 && ky<m && Map[kx][ky]!='#'){vis[kx][ky]=1;if(Map[kx][ky]=='a')return res+1;if(Map[kx][ky]=='.'){tmp.step=res+1;tmp.x=kx;tmp.y=ky;q.push(tmp);}if(Map[kx][ky]=='x'){tmp.step=res+2;tmp.x=kx;tmp.y=ky;q.push(tmp);}}}}return -1;}int main(){node tmp;while(~scanf("%d%d",&n,&m)){int sx,sy;memset(Map,0,sizeof(Map));memset(vis,0,sizeof(vis));for(int i=0;i<n;i++)scanf("%s",Map[i]);while(!q.empty())q.pop();for(int i=0;i<n ;i++){for(int j=0;j<m;j++){if(Map[i][j]=='r'){Map[i][j]='.';tmp.x=i;tmp.y=j;tmp.step=0;q.push(tmp);vis[i][j]=1;}}}int ans=bfs();if(ans!=-1)printf("%d\n",ans);else printf("Poor ANGEL has to stay in the prison all his life.\n");}return 0;}
找到天使的位置,从天使的位置开始搜索,若找到营救者,则结束;否则,向四个方向进行搜索,判断是否走过、是否能走,分情况进行步数的增减,若遇到 ‘ . ’ 或是 ‘ r ’ ,步数 + 1;若遇到 ‘ x ’,步数 + 2
走过的路径保存到优先队列中去
运行时间 :0ms
#include<stdio.h>#include<iostream>#include<string.h>#include<queue>using namespace std;int n,m,vis[201][201];char Map[201][201];const int c[4][2]={-1,0,1,0,0,1,0,-1};struct node{int x,y;int step;friend bool operator < (node a,node b){return a.step > b.step;}};priority_queue<node> q;int bfs(int x1,int y1){node tmp;tmp.x=x1;tmp.y=y1;tmp.step=0;q.push(tmp);int px,py,kx,ky,res;while(!q.empty()){tmp=q.top();q.pop();px=tmp.x;py=tmp.y;res=tmp.step;if(Map[px][py]=='r')return res;for(int i=0;i<4;i++){tmp.x=px+c[i][0];tmp.y=py+c[i][1];tmp.step=0;if(!vis[tmp.x][tmp.y] && tmp.x>=0 && tmp.x<n && tmp.y>=0 && tmp.y<m){if(Map[tmp.x][tmp.y]=='.' || Map[tmp.x][tmp.y]=='r')tmp.step=res+1;else if(Map[tmp.x][tmp.y]=='x')tmp.step=res+2;if(tmp.step>0 && !vis[tmp.x][tmp.y]){vis[tmp.x][tmp.y]=1;q.push(tmp);}}}}return 0;}int main(){while(~scanf("%d%d",&n,&m)){int sx,sy;memset(Map,0,sizeof(Map));memset(vis,0,sizeof(vis));for(int i=0;i<n;i++)scanf("%s",Map[i]);for(int i=0;i<n ;i++){for(int j=0;j<m;j++){if(Map[i][j]=='a'){sx=i;sy=j;break;}}}vis[sx][sy]=1;int ans=bfs(sx,sy);if(ans)printf("%d\n",ans);else printf("Poor ANGEL has to stay in the prison all his life.\n");}return 0;}
0 0
- 广搜 — — ZOJ 1649 Rescue
- zoj 1649(hdu 1242)Rescue(广搜)
- hdu1242——Rescue(广搜)
- Rescue——广搜、优先队列
- HDU——1242 Rescue(广搜)
- 1242 rescue 广搜
- Rescue 1242 广搜
- HDU--1242 -- Rescue [广搜]
- Rescue 优先队列+广搜
- 4.2.3--广搜--Rescue
- zoj 1649 Rescue
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- ZOJ 1649 Rescue (BFS)
- zoj 1649 rescue (bfs)
- ZOJ-1649-Rescue
- zoj 1649 Rescue
- ZOJ:1649 Rescue
- java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
- 设计模式六大原则(2):里氏替换原则
- eclipse生成Hibernate注解类【续】
- 使用VC创建多级目录
- C语言文件指针和文件描述符
- 广搜 — — ZOJ 1649 Rescue
- Codeforces Round #345 (Div. 2) D,E
- 防盗链处理
- HDU 2503 a/b + c/d
- 数组和List之间相互转换的方法
- 自定义控件ScrollRelativeLayout
- Java 反转单链表
- 宏元编程——逻辑控制——实现循环二
- 安卓BroadcastReceiver组件使用系列3:使用广播来判断数据网络和无线网络的连接情况