救援行动(save) (BFS)

时间限制: 1 Sec  内存限制: 64 MB
提交: 42  解决: 9
[提交][状态][讨论版]

题目描述

Angel被人抓住关在一个迷宫了!迷宫的长、宽均不超过200,迷宫中有不可以越过的墙以及监狱的看守。Angel的朋友带了一个救援队来到了迷宫中。他们的任务是:接近Angel。我们假设接近Angel就是到达Angel所在的位置。
假设移动需要1单位时间,杀死一个看守也需要1单位时间。到达一个格子以后,如果该格子有看守,则一定要杀死。交给你的任务是,最少要多少单位时间,才能到达Angel所在的地方(只能向上、下、左、右4个方向移动)?

输入

第1行两个整数n,m。表示迷宫的大小为n×m。
以后n行,每行m个字符。其中“#”代表墙,“.”表示可以移动,“x”表示看守,“a”表示Angel,“r”表示救援队伍。字母均为小写。

输出

l行,代表救出Angel的最短时间。如果救援小组永远不能达到Angel处,则输出“NO ANSWER”。

样例输入

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

样例输出

13

【分析】 前期做一些小小的处理,后面就是简单的BFS一下。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
using namespace std;
int n,m,flag,a,b;
int dis[8][2]= {-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};
int vis[15][15];
int w[15][15];
string str;
struct man
{
    int x,y,step;
};
queue<man>q;
void bfs(man s,int enx,int eny)
{
    q.push(s);
    vis[s.x][s.y]=1;
    while(!q.empty())
    {
        man t=q.front();
        q.pop();
        //printf("%d %d %d\n",t.x,t.y,t.step);system("pause");
        if(t.x==enx&&t.y==eny)
        {
            printf("%d moves\n",t.step);
            flag=1;
            return;
        }
        for(int i=0; i<8; i++)
        {
            int xx=t.x+dis[i][0];
            int yy=t.y+dis[i][1];//
            //printf("%d %d\n",xx,yy);
            if(xx>=0&&xx<8&&yy>=0&&yy<8&&w[xx][yy]!=1&&vis[xx][yy]==0)
            {
                man k;
                k.x=xx;
                k.y=yy;
                k.step=t.step+1;
                q.push(k);
                vis[xx][yy]=1;
            }
        }
    }
}
int main()
{
    int g,h;int cnt=0;
    while(~scanf("%d",&n))
    {cnt++;flag=0;
        while(!q.empty())q.pop();
        int enx;int eny;

        if(n==-1)break;
        memset(vis,0,sizeof(vis));
        memset(w,0,sizeof(w));
        while(n--)
        {
            cin>>str;
            w[str[0]-‘a‘][str[1]-‘1‘]=1;
        }
        string sta,en;
        cin>>sta>>en;
        man s;s.x=sta[0]-‘a‘;s.y=sta[1]-‘1‘;s.step=0;q.push(s);
        enx=en[0]-‘a‘;eny=en[1]-‘1‘;
        printf("Board %d: ",cnt);
        bfs(s,enx,eny);
        if(flag==0)puts("not reachable");
    }

    return 0;
}

帅到没朋友

时间: 2024-10-17 22:35:33

救援行动(save) (BFS)的相关文章

【BFS】营救(save)

描述 铁塔尼号遇险了!他发出了求救信号.距离最近的哥伦比亚号收到了讯息,时间就是生命,必须尽快 赶到那里. 通过侦测,哥伦比亚号获取了一张海洋图.这张图将海洋部分分化成 n*n 个比较小的单位,其中用 1 标明的是陆地,用 0 标明是海洋.船只能从一个格子,移到相邻的四个格子. 题目 为了尽快赶到出事地点,哥伦比亚号最少需要走多远的距离. 输入 第一行为 n(n≤1000),下面是一个 n*n 的 0.1 矩阵,表示海洋地图 最后一行为四个小于 n 的整数,分别表示哥伦比亚号和铁塔尼号的位置.

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

HDU 1242 Rescue(BFS)

题目链接 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: a

hdu 1242 Rescue(bfs+优先队列)

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

hdu1242 优先队列+bfs

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22034    Accepted Submission(s): 7843 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is d

(BFS) acdream 1191

Dragon Maze Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description You are the prince of Dragon Kingdom and your kingdom is in danger of running out of power. You must find p

zoj1649-Rescue (迷宫最短路径)【bfs 优先队列】

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649 Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 a

[ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 941    Accepted Submission(s): 352 Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Clas