广搜 — — 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.)

输入

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.

输出

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
原创粉丝点击