优先队列和BFS hdu1026 记录路径

15 篇文章 0 订阅
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<vector>
#include<string.h>
#include<map>
#include<cmath>
#include<queue>
#define ll long long
#define INF 0x7fffffff
#define MAX 0x3f3f3f3f
#define maxn 1000005
#define ull unsigned long long
using namespace std;

int t[105][105],n,m;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

struct node
{
    int x,y,time;
    friend operator < (node a, node b)//time小的优先级高
    {
        return a.time>b.time;
    }
};//优先队列类型,存坐标和时间

struct ff
{
    int x,y;
    char c;
};
ff f[105][105];//刚开始存符号

int bfs()
{
    priority_queue<node> q;//优先队列
    node a,b;
    a.x=n-1;//从终点开始
    a.y=m-1;
    if(f[n-1][m-1].c>='1'&&f[n-1][m-1].c<='9')//记录终点打怪用的时间,即初始化
    {
        a.time=f[n-1][m-1].c-'0';
        t[n-1][m-1]=a.time;
    }
    else  a.time=0;
    f[n-1][m-1].c='X';//标记为不能走了
    q.push(a);
    while(!q.empty())//优先队列会把时间短的先出队
    {
        a=q.top();
        q.pop();
        if(a.x==0&&a.y==0)
            return a.time;
        b=a;
        int xx,yy;
        for(int i=0;i<4;i++)
        {
            xx=b.x=a.x+dir[i][0];
            yy=b.y=a.y+dir[i][1];
            if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&f[b.x][b.y].c!='X')
            {
                if(f[b.x][b.y].c>='1'&&f[b.x][b.y].c<='9')//有妖怪
                {
                    t[b.x][b.y]=f[b.x][b.y].c-'0';//有妖怪的时候记一下所耗费的对应时间
                    b.time=a.time+t[b.x][b.y]+1;//勿忘加一
                }
                else
                    b.time=a.time+1;//正常情况下没有怪就是要加一
                q.push(b);
                f[xx][yy].c='X';//标记成不能走
                f[xx][yy].x=a.x;//把储存符号的数组储存上一个历经点,即一会的顺序历经的点
                f[xx][yy].y=a.y;
            }
        }
    }
    return -1;
}
int main()
{
    int i,j,num;
    while(~scanf("%d%d",&n,&m))
    {
        getchar();
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                scanf("%c",&f[i][j].c);
            getchar();
        }
        memset(t,0,sizeof(t));
        num=bfs();
        int em,u,v;
        if(num!=-1)
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",num);
            em=1;u=0;v=0;
            while(em!=num+1)
            {
                printf("%ds:(%d,%d)->(%d,%d)\n",em++,u,v,f[u][v].x,f[u][v].y);
                for(int h=0;h<t[f[u][v].x][f[u][v].y];h++)//在这个数组存的时间内一直在同一地方打怪
                    printf("%ds:FIGHT AT (%d,%d)\n",em++,f[u][v].x,f[u][v].y);
                int tmp1=f[u][v].x;//注意利用中介变量重置u,v,,,让循环得以继续
                int tmp2=f[u][v].y;
                u=tmp1;
                v=tmp2;
            }
        }
        else
            printf("God please help our poor hero.\n");
        printf("FINISH\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

When_3*four

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值