练习二1012


Rescue
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 74   Accepted Submission(s) : 22
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 &lt;= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.<br><br>Angel's friends want to save Angel. Their task is: approach Angel. We assume that &quot;approach Angel&quot; 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.<br><br>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.)<br>
 

Input
First line contains two integers stand for N and M.<br><br>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. <br><br>Process to the end of the file.<br>
 

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." <br>
 

Sample Input
  
  
7 8<br>#.#####.<br>#.a#..r.<br>#..#x...<br>..#..#.#<br>#...##..<br>.#......<br>........<br>
 

Sample Output
  
  
13<br>
 

Author
CHEN, Xue
 

Source
ZOJ Monthly, October 2003
 

题意:
在一张地图中,a被困住,r去救a,r不止一个,x代表敌人,r每走一步用时为1,杀掉敌人要格外加1,问最短时间。
思路:
是一个优先队列+bfs,以时间为标准优先,搜索时可以从a开始去搜索r,因为a只有一个,r有多个。没看答案,自己写了好多遍终于过了。注意下面写的注释。
代码:
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}},n,m,sx,sy;
char map[201][201];
bool vis[201][201];
bool inmap(int x,int y)
{
 if(x>=1&&x<=n&&y>=1&&y<=m)
  return true;
 return false;
}
struct point
{
 int x,y,tim;
 friend bool operator<(point a,point b)
 {
  return a.tim>b.tim;
 }
}p1,p2;
int bfs(int a,int b,int c)
{
 p1.x=a;
 p1.y=b;
 p1.tim=c;
 priority_queue<point>q;
 q.push(p1);
 while(!q.empty())
 {
  p1=q.top();
  q.pop();
  if(map[p1.x][p1.y]=='r')
   return p1.tim;
  for(int i=0;i<4;i++)
  {
   int n_x=p1.x+dir[i][0];
      int n_y=p1.y+dir[i][1];
   if(!inmap(n_x,n_y)||vis[n_x][n_y]||map[n_x][n_y]=='#')
    continue;
   if(map[n_x][n_y]=='x')
   {
    vis[n_x][n_y]=1;
    p2.x=n_x;
    p2.y=n_y;
    p2.tim=p1.tim+2;
    q.push(p2);
   }
   if(map[n_x][n_y]=='r'||map[n_x][n_y]=='.'){
   vis[n_x][n_y]=1;
   p2.x=n_x;
   p2.y=n_y;
   p2.tim=p1.tim+1;
   q.push(p2);
   }
  }
 }
 return -1;
}
int main()
{
 while(cin>>n>>m)
 {
  for(int i=1;i<=n;i++){
   for(int j=1;j<=m;j++){
    cin>>map[i][j];
   }
  }
  for(int k=1;k<=n;k++){
   for(int j=1;j<=m;j++){
    if(map[k][j]=='a')
    {sx=k;sy=j;}
   }
  }
  memset(vis,0,sizeof(vis));
  vis[sx][sy]=1;
  int cnt=bfs(sx,sy,0);                                                                                          //有返回值的函数一定要注意第一次调用后再调用就不对了。(if(bfs(....)) cout<<bfs(....)...)就不
  if(cnt==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; // 对。
  else cout<<cnt<<endl;
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值