fzu——Problem 2124 吃豆人(bfs)

 Problem Description

吃豆人是一款非常经典的游戏,游戏中玩家控制吃豆人在地图上吃光所有豆子,并且避免被怪物抓住。

这道题没有怪物,将游戏的画面分成n*m的格子,每格地形可能为空地或者障碍物,吃豆人可以在空地上移动,吃豆人每移动一格需要1s时间,并且只能朝上下左右四个方向移动,特别的是吃豆人还能吐出舌头,舌头每移动一格需要0.1s时间,舌头只可以走直线。不必考虑吃豆人转身所需要的时间。

举例,吃豆人在(1,1)坐标,而豆子在(1,5)坐标,并且中间没有障碍物,此时朝豆子方向吐舌头~,经过0.8s就可以吃到豆子(来回各0.4s,吐出去的舌头要缩回来的嘛)。

游戏中还有加速道具,一旦得到加速道具,吃豆人就获得2倍移动速度,吐舌头的速度没有增加,即走1格用0.5s。现在地图上有且只有一颗豆子。游戏中有.代表空地;X表示障碍,吃豆人不能越过障碍;B代表豆子;S代表加速道具,并且地图上道具总数不超过1个,道具所在的位置为空地,得到道具后立即使用,道具立即消失,地形变为空地,不能用舌头去取道具;P表示吃豆人,吐舌头的时候吃豆人不能移动。

 Input

输入包含多组数据。输入第一行有两个个整数n,m(2<=n,m<=20),接着一个n*m的地图矩阵。

对于50%的数据,地图上没有道具。

 Output

输出一行,最快用多少s吃到豆子,结果保留1位小数,如果吃不到,输出-1。

 Sample Input

2 2
XP
B.
3 2
XP .
S B.

 Sample Output

1.2
1.7

#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
char map[25][25];//地图
bool mark[25][25];//标记
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int si,sj,ei,ej,pi,pj;
int flag;
int n,m;
struct node
{
    int x,y;
    double time;
    bool flag;//是否获得加速
    friend bool operator< (struct node a,struct node b)
    {
        return a.time > b.time;
    }
}s,now;
priority_queue<node> lcm;

int find()//bfs找宝贝
{
    int i;
    if(flag == 0)
        return -1;
    while(!lcm.empty())
        lcm.pop();
    s.x = si;
    s.y = sj;
    s.time = 0;
    lcm.push(s);
    memset(mark,0,sizeof(mark));
    while(!lcm.empty())
    {
        now = lcm.top();
        lcm.pop();
        for(i = 0;i < 4;i ++)
        {
            s = now;
            s.x += dx[i];
            s.y += dy[i];
            if(s.x < 1 || s.y < 1 || s.x > n || s.y > m || mark[s.x][s.y] || map[s.x][s.y] == 'X')
                continue;
            s.time ++;
            if(map[s.x][s.y] == 'S')
                return s.time;
            lcm.push(s);
            mark[s.x][s.y] = 1;
        }
    }
    return -1;
}

int isok()//直接伸舌头一定是最快的
{
    int i;
    if(s.x == ei)
    {
        for(i = min(s.y,ej) + 1;i < max(s.y,ej);i ++)
            if(map[ei][i] == 'X')
                break;
        if(i == max(s.y,ej))
        {
            s.time += 0.2 * abs(s.y - ej);
            return 1;
        }
        return 0;
    }
    if(s.y == ej)
    {
        for(i = min(s.x,ei) + 1;i < max(s.x,ei);i ++)
            if(map[i][ej] == 'X')
                break;
        if(i == max(s.x,ei))
        {
            s.time += 0.2 * abs(s.x - ei);
            return 1;
        }
        return 0;
    }
    return 0;
}

double Bfs(int a,int b,int c)
{
    int i;
    while(!lcm.empty())
        lcm.pop();
    s.x = a;
    s.y = b;
    s.flag = c;
    s.time = 0;
    memset(mark,0,sizeof(mark));
    if(isok())
    {
        //printf("%.1lf\n",s.time);
        return s.time;
    }
    lcm.push(s);
    mark[s.x][s.y] = 1;
    while(!lcm.empty())
    {
        now = lcm.top();
        lcm.pop();
       // printf("%d %d\n",now.x,now.y);
        for(i = 0;i < 4;i ++)
        {
            s = now;
            s.x += dx[i];
            s.y += dy[i];
            if(s.flag)
                s.time += 0.5;
            else
                s.time += 1;
            if(s.x < 1 || s.y < 1 || s.x > n || s.y > m || mark[s.x][s.y] || map[s.x][s.y] == 'X')
                continue;
            if(isok())
            {
                //printf("%.1lf\n",s.time);
                return s.time;
            }
            if(map[s.x][s.y] == 'S')
                s.flag = 1;
            lcm.push(s);
            mark[s.x][s.y] = 1;
        }
    }
    return -1;
}

int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        flag = 0;
        getchar();
        for(i = 1;i <= n;i ++)
        {
            for(j = 1;j <= m;j ++)
            {
                map[i][j] = getchar();
                if(map[i][j] == 'P')
                {
                    si = i;
                    sj = j;
                }
                if(map[i][j] == 'B')
                {
                    ei = i;
                    ej = j;
                }
                if(map[i][j] == 'S')
                {
                    flag = 1;
                    pi = i;
                    pj = j;
                }
            }
            getchar();
        }
        double ans = Bfs(si,sj,0);//直接找
        int tmp = find();
        if(tmp != -1)
        {
            double tp = Bfs(pi,pj,1);
            if(tmp + tp < ans)
                ans = tmp + tp;
        }
        printf("%.1lf\n",ans);
    }
    return 0;
}
/*
6 5
.S...
PXXX.
.XXX.
.XB..
.X.X.
.....
6 5
S....
PXXX.
.XXX.
.X...
.X.X.
..B..
3 3
...
PXB
XXX
3 3
P..
..B
XXX
3 3
S..
PXB
XXX
2 2
XP
B.
3 2
XP
.S
B.
4 3
SPX
..X
..X
..B
*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值