HDU 1242 Rescue 简单bfs 好题

点击打开链接题目链接

Rescue

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


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

你在a的位置 需要去r救人

x是士兵 打败他需要1秒

求最短时间

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
    int x,y,step;
};
int n,m,x1,y1,x2,y2;
int mark[210][210];
int vis[210][210];
int dx[4]={-1,0,0,1};
int dy[4]={0,-1,1,0};
void bfs()
{
    queue<node> q;
    while(!q.empty())
        q.pop();
    node fr,ed;
    int i;
    fr.x=x1,fr.y=y1,fr.step=0;
    q.push(fr);
    while(!q.empty())
    {
        fr=q.front();
        q.pop();
        if(fr.x==x2&&fr.y==y2)
        {
            printf("%d\n",fr.step);
            return ;
        }
        if(mark[fr.x][fr.y]==2)
        {
            fr.step++;
            mark[fr.x][fr.y]=1;
            q.push(fr);//入队(下一层)
            continue;
        }
        for(i=0;i<4;i++)
        {
            ed.x=fr.x+dx[i];
            ed.y=fr.y+dy[i];
            if(vis[ed.x][ed.y]==1||mark[ed.x][ed.y]==0)
                continue;
            ed.step=fr.step+1;
            vis[ed.x][ed.y]=1;
            q.push(ed);
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
    int i,j;
    char s[210];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mark,0,sizeof(mark));
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
            scanf(" %s",s+1);
            for(j=1;j<=m;j++)
            {
                if(s[j]=='r')
                {
                    x1=i,y1=j;
                    mark[i][j]=1;
                    vis[i][j]=1;
                }
                else if(s[j]=='a')
                {
                    x2=i,y2=j;
                    mark[i][j]=1;
                }
                else if(s[j]=='x')
                {
                    mark[i][j]=2;
                }
                else if(s[j]=='.')
                    mark[i][j]=1;
            }
        }
        bfs();
    }
    return 0;
}


此题打怪时间为一秒
如果打怪需要n秒 就可能需要用到优先队列了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值