HDOJ HDU 1026 Ignatius and the Princess I

HDOJ 1026 Ignatius and the Princess I

题目

点此查看 HDOJ 1026 Ignatius and the Princess I

分类

bfs

题意

英雄 Ignatius 救 公主 的故事背景
本题是第一部分
走迷宫 大小 n * m

迷宫构成
起点 (0, 0)
终点 (n, m)
‘.’ 空白位 可以走
‘X’ 墙 不能行走
‘数字 a’ 怪兽 Ignatius 需要在此花费 a 时间

题解

bfs求最短路 并输出过程
但是 此题不同在于打怪兽需要花费时间 节点不能同步进行
所以每一步需要记录时间信息 用优先队列决定最先需要移动的点

技巧

题目需要输出过程
倒序bfs比较方便顺序输出过程

代码

#include <iostream>
#include <cstring>
#include <queue>
#define max 110
 
using namespace std;
 
int dir[5][2] = {0,0,-1,0,1,0,0,-1,0,1};
char map[max][max];
int vis[max][max];
 
//结点结构体 
struct pst
{
    int x;
    int y;
    int t;
    pst()
    {
        x = 0;
        y = 0;
        t = 0;
    }
    pst(int nx,int ny,int nt)
    {
        x = nx;
        y = ny;
        t = nt;
    }
    pst(const pst & b)
    {
        x = b.x;
        y = b.y;
        t = b.t;
    }
    bool operator < (const pst & b) const
    {
        if(t > b.t)
            return true;
        else return false;
    }
    //重载<用于优先队列
    pst & operator = (const pst & b)
    {
        x = b.x;
        y = b.y;
        t = b.t;
        return * this;
    }
};
 
int main()
{
    int m,n,nx,ny,rst,tms;
    priority_queue<pst> q;
    pst pn,lst;
    int stx,sty,ltx,lty;
    while(cin >> n >> m)
    {
        rst = 0;
        for(int i = 0;i < n;i++)
        {
            for(int j = 0;j < m;j++)
                cin >>  map[i][j];
        }
        memset(vis,0,sizeof(vis));
        pst start(n-1,m-1,0);
        vis[n-1][m-1] = 6;
        q.push(start);
        while(!q.empty())
        {
            lst = q.top();
//          cout << "lst " << lst.x << " " << lst.y << " " << lst.t  << " rst " << rst << endl;
            if(lst.x == 0 && lst.y == 0)
            {
                rst = lst.t;
                q.pop();
                break;
            }
            q.pop();
            for(int i = 1;i < 5;i++)
            {
                pn.x = lst.x + dir[i][0];
                pn.y = lst.y + dir[i][1];
                if(pn.x < 0 || pn.y < 0 || pn.x > n - 1|| pn.y > m - 1)
                    continue;
                if(vis[pn.x][pn.y] || map[pn.x][pn.y] == 'X')
                    continue;
//              cout <<"nxy " << pn.x << " " << pn.y << endl;
                vis[pn.x][pn.y] = i;
//              cout <<"vis " << vis[pn.x][pn.y] << endl;
                //记录上一节点的信息
                map[pn.x][pn.y] == '.' ? pn.t = lst.t + 1 : pn.t = lst.t + (map[pn.x][pn.y] - '0') + 1;
                // '.' : 时间 + 1 | 数字 : 时间 + 数字
                q.push(pn);
            }
        }
//      cout << "rst " << rst << endl;
        if(rst)
        {
            if(map[n-1][m-1] != '.' && map[n-1][m-1] != 'X' )
                rst += (map[n-1][m-1] - '0');
            cout << "It takes " << rst << " seconds to reach the target position, let me show you the way." << endl;
            stx = 0;
            sty = 0;
            ltx = 0;
            lty = 0;
            tms = 1;
            if(map[0][0] != '.' || map[0][0] != 'X' )
                for(int i = 0;i < (map[0][0] - '0');i++)
                    cout << tms++ << "s:FIGHT AT (0,0)" << endl;
            while(stx != n-1 || sty != m-1)
            {
            //  cout << "st " << stx << " " << sty << endl;
                switch(vis[stx][sty])
                {
                    case 1 : stx++; break;
                    case 2 : stx--; break;
                    case 3 : sty++; break;
                    case 4 : sty--; break;
                }
                cout << tms++ << "s:(" << ltx << "," << lty << ")->(" << stx << "," << sty << ")" << endl;
                if(map[sty][sty] != '.')
                    for(int i = 0;i < (map[stx][sty] - '0');i++)
                        cout << tms++ << "s:FIGHT AT (" << stx << "," << sty << ")" << endl;
                ltx = stx;
                lty = sty; 
            }
            cout << "FINISH" << endl;
        }
        else
            cout << "God please help our poor hero." << endl << "FINISH" << endl;
        while(!q.empty())
        {
            q.pop();
        }
    } 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值