hdu 1026 Ignatius and the Princess I题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026

菜鸟第一发。。。

题目大意:给出一张n*m的图,'.'表示道路,'X'表示不能通过的地方,数字n表示怪物的血量(打死需要花费n秒),每走一步需花费一秒,只能往上下左右四个方向走。要求找出从(0, 0)点到(n-1, m-1)点用时最短的路径,并按照样例把路径输出来。

思路:用bfs寻找用时最少的路径,定义一个path数组把路径保存下来。

注意一下(0, 0)点不可能出现墙和怪物,(n-1, m-1)点可能出现怪物。

代码

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;

int n, m;
int path[10010], vis[105][105], ans[10000];
int fx[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
char Map[105][105];

struct node
{
    int x, y, Tpl, unt;//x,y位置,Tpl总时间长度,unt通过该单位所需时间
    friend bool operator <(node a, node b)
    {
        return a.Tpl > b.Tpl;
    }
};

int cal_unt(int x, int y)//计算通过(x, y)点需要的时间
{
    int sum = 1;
    if (Map[x][y] <= '9' && Map[x][y] >= '0')
        sum += Map[x][y] - '0';
    return sum;
}

int cal_wei(int x, int y)//计算(x, y)点在图中的位置,方便存到path数组
{
    return x * m + y;
}

int bfs()
{
    priority_queue<node> d;
    node p1, p2, p3;
    p1.x = p1.y = p1.unt = 0;
    p1.Tpl = 0;
    d.push(p1);
    vis[0][0] = 1;
    path[0] = -1;
    while (!d.empty())
    {
        p2 = d.top();
        d.pop();
        for (int i = 0; i < 4; i++)
        {
            int x = p2.x + fx[i][0];
            int y = p2.y + fx[i][1];
            if (x >= 0 && x < n && y >= 0 && y < m && Map[x][y] != 'X' && !vis[x][y])
            {
                p3.unt = cal_unt(x, y);
                p3.Tpl = p2.Tpl + p3.unt;
                p3.x = x; p3.y = y;
                d.push(p3);
                //cout << cal_wei(x,y) << "    " << cal_wei(p2.x, p2.y) << endl;
                path[cal_wei(x,y)] = cal_wei(p2.x, p2.y);
                vis[x][y] = 1;
                if (x == n-1 && y == m-1)
                {
                    return p3.Tpl;
                }
            }
        }
    }
    return -1;
}

int main()
{
    while (cin >> n >> m)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%s", Map[i]);
        }
        memset(vis, 0, sizeof(vis));
        int sum = bfs();
        if (sum == -1)
        {
            printf("God please help our poor hero.\nFINISH\n");
        }
        else
        {
            printf("It takes %d seconds to reach the target position, let me show you the way.\n", sum);
            int k = 0;
            ans[0] = cal_wei(n-1, m-1);
            while (path[ans[k]] > -1)
            {
                ans[k + 1] = path[ans[k]];
                k++;
            }
            int j = 1;
            for (int i = k; i > 0; i--)
            {
                int a, b, c, d;
                a = ans[i] / m;
                b = ans[i] % m;
                c = ans[i-1] / m;
                d = ans[i-1] % m;
                printf("%ds:(%d,%d)->(%d,%d)\n", j++, a, b, c, d);
                int v = cal_unt(c, d);
                if (v > 1)
                {
                    for (int x = v; x > 1; x--)
                    {
                        printf("%ds:FIGHT AT (%d,%d)\n", j++, c, d);
                    }
                }
            }
            printf("FINISH\n");
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值