*Hdu 1026-Ignatius and the Princess I

题目思想比较简单,但是题目要求输出路径比较麻烦,学习了 Ice_Crazy博主的方法,感觉不错。
#include <iostream>
#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
const int maxn = 100 + 5;
struct node
{
    int x,y;
    int step;
    friend bool operator<(node n1,node n2)
    {
        return n1.step>n2.step;
    }
};
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int map[maxn][maxn];
int flag[maxn][maxn];
int blood[maxn][maxn];
int n,m;
int ptime;

int BFS()
{
    priority_queue<node> Q;
    node s;
    s.x = 0;
    s.y = 0;
    s.step = 0;
    map[0][0] = -1;
    Q.push(s);

    while(!Q.empty())
    {
        if(Q.top().x==n-1 && Q.top().y==m-1)
            return Q.top().step;
        node next;
        for(int i=0;i<4;i++)
        {
            next.x = Q.top().x + dir[i][0];
            next.y = Q.top().y + dir[i][1];

            if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && map[next.x][next.y]!=-1)
            {
                next.step = Q.top().step+1+map[next.x][next.y];

                flag[next.x][next.y] = i+1;
                map[next.x][next.y] = -1;
                Q.push(next);
            }
        }
        Q.pop();
    }
    return -1;
}
void Path(int x,int y)
{
    int nx_x;
    int nx_y;
    if(flag[x][y] == 0)
        return ;
    nx_x = x - dir[flag[x][y]-1][0];
    nx_y = y - dir[flag[x][y]-1][1];
    Path(nx_x,nx_y);
    printf("%ds:(%d,%d)->(%d,%d)\n",ptime++,nx_x,nx_y,x,y);
    while(blood[x][y]--)
        printf("%ds:FIGHT AT (%d,%d)\n",ptime++,x,y);
}
int main()
{
    char str[maxn];
    int ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(flag,0,sizeof(flag));
        memset(blood,0,sizeof(blood));
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            for(int j=0;str[j];j++)
            {
                if(str[j]=='.')
                    map[i][j]=0;
                else if(str[j]=='X')
                    map[i][j]=-1;
                else
                    map[i][j]=blood[i][j]=str[j]-'0';
            }
        }

        ans=BFS();
        //printf("AA\n");
        if(ans==-1)
            printf("God please help our poor hero.\n");
        else
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
            ptime=1;
            Path(n-1,m-1);
        }
        printf("FINISH\n");
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值