博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1242 Rescue(BFS)
阅读量:4670 次
发布时间:2019-06-09

本文共 2678 字,大约阅读时间需要 8 分钟。

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8065    Accepted Submission(s): 2959

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
 

 

Author
CHEN, Xue
 

 

Source
 

 

Recommend
Eddy

 

//广搜的应用

#include <iostream>

#include <stdio.h>
#include <queue>
#include <string.h>
#define N 203
using namespace std;
char map[N][N];
int r[N][N];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int n,m;
struct node
{
    int x,y;
};
bool is_ok(node &t)
{
    if(t.x<1||t.x>n||t.y<1||t.y>m||map[t.x][t.y]=='#')
       return false;
    return true;
}
void bfs(int x,int y)
{
    queue<node> Q;
    node a,b;
    int i,l;
    a.x=x;a.y=y;
    Q.push(a);
    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();
       for(i=0;i<4;i++)
       {
           b.x=a.x+dir[i][0];
           b.y=a.y+dir[i][1];
           if(!is_ok(b)||b.x==x&&b.y==y)
             continue;
             l=1;
            if(map[b.x][b.y]=='x')
                l=2;
             if(r[b.x][b.y]==0)
              {
                r[b.x][b.y]=r[a.x][a.y]+l;
                Q.push(b);
              }
              else
              {
                  if(r[b.x][b.y]>r[a.x][a.y]+l)//发现有更近的、就替换掉
                    {
                        r[b.x][b.y]=r[a.x][a.y]+l;
                        Q.push(b);
                    }
              }
       }
    }
}
int main()
{
    int i,j;
    int x,y;
    int a_x,a_y;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        for(i=1;i<=n;getchar(),i++)
          for(j=1;j<=m;j++)
            {
                scanf("%c",&map[i][j]);
                if(map[i][j]=='r')
                 x=i,y=j;
                if(map[i][j]=='a')
                 a_x=i,a_y=j;
               r[i][j]=0;
            }
       bfs(x,y);
       if(r[a_x][a_y]!=0)
         printf("%d\n",r[a_x][a_y]);
        else
         printf("Poor ANGEL has to stay in the prison all his life.\n");//开始忘记写这个了,又贡献了次//WA,唉、、自己都无语了
    }
    return 0;
}

转载于:https://www.cnblogs.com/372465774y/archive/2012/07/08/2581650.html

你可能感兴趣的文章
python生成器
查看>>
flannel vxlan工作基本原理及常见排障方法
查看>>
曼昆经济学原理(微经部分)笔记整理
查看>>
React 入门
查看>>
eclipse plugins
查看>>
更改TFS项目中的SharePoint网站端口
查看>>
C#属性
查看>>
大道至简 第二章 读后随笔
查看>>
Python多线程报错之RuntimeError
查看>>
EOS1.1版本新特性介绍
查看>>
EOS生产区块:解析插件producer_plugin
查看>>
重新打包system.img
查看>>
MySQL user表详解
查看>>
Http常见状态码
查看>>
centos7 安装pip
查看>>
Java之JDBC①
查看>>
date()---求N个月后的1号
查看>>
机器学习九大挑战(转载)
查看>>
加密流量分析
查看>>
Python字符串方法
查看>>