寒假前刷题(10)搜索系列 hdu 1242

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9912    Accepted Submission(s): 3626


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.)
 

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
 
//这是一道广搜的题,因为需要求最短的时间 ,本来看了指导,知道要用优先队列+bfs,后来在小健健学长的指导下,知道其实可以不用优先队列,可以同一个结点弹出后,在往队列里压入,以此来代替优先队列,膜拜小健健!
下面是代码:

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<queue>

using namespace std;

char map[205][205]; int dir[4][2]={1,0,0,-1,-1,0,0,1};//有些人喜欢分方向遍历,我还是喜欢这种顺序遍历 int visit[205][205]; //标记数组,标记是否经过结点 int n,m;

struct node//这点没想到啊,老是记不得用结构体,这样一用,在表示上就简单多了 {      int x;   int y;     int time; };

void bfs(node vs) {      int i;      int flag=0;      node vn,vw;   queue<node> q;      vn.x=vs.x;      vn.y=vs.y;      vn.time=0;      visit[vn.x][vn.y]=1;      q.push(vn);      while(!q.empty())      {       vn=q.front();      q.pop();            if(map[vn.x][vn.y]=='x') //守卫这里是难点,因为这里杀死守卫还需要1个单位的时间,然后从这里在搜索下一个地方,又需要一个单位的时间。      {

                   map[vn.x][vn.y]='.';                    vn.time++;                    q.push(vn);                     continue; //这个题的关键就在于这里,这里不能跳出循环,而要继续在压入堆栈。      }             if(map[vn.x][vn.y]=='a')      {                  flag=1;                  break; //这里一找到就退出,说明结束了      }      else              {                   for(i=0;i<4;i++)                   {                       vw.x=vn.x+dir[i][0];                       vw.y=vn.y+dir[i][1];                       if(vw.x>=0&&vw.x<n&&vw.y>=0&&vw.y<m&&!visit[vw.x][vw.y]&&map[vw.x][vw.y]!='#')                          {                               vw.time=vn.time+1;                               visit[vw.x][vw.y]=1;                               q.push(vw);                          }                   }       }   }   if(flag)           printf("%d\n",vn.time);      else           printf("Poor ANGEL has to stay in the prison all his life.\n"); }

int main() {       int i,j;    node vs;    while(scanf("%d%d",&n,&m)!=EOF)    {            for(i=0;i<n;i++)      {                scanf("%s",map[i]);              for(j=0;j<m;j++)      {                        if(map[i][j]=='r')        {         vs.x=i;                             vs.y=j;                        }      }       }     memset(visit,0,sizeof(visit));     bfs(vs);       }    return 0; }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值