HDU 1026 Ignatius and the Princess I(优先队列+打印路径)

题意:n*m的迷宫,从(0,0)到(n-1,m-1),遇到怪物停留怪物所在方格中的数字个单位时间,求最短时间并打印路径;

思路:用bfs先搜最短路,搜最短路时一定要用优先队列,不然结果不对;在通过保存上一步的方法保存路径,到达终点时,将路径查询出来,遇到怪物是位置不变;

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,s1,s2,e1,e2;
char mm[505][505];
int vis[505][505];
int  dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y,t;
    bool operator<(const node&xx)const
    {
        return t>xx.t;
    }
};
struct lujin
{
    int x1,y1,x2,y2;
}record[505];
node pre[505][505];
int quan[505][505],route[505][505];
int bfs()
{
    priority_queue<node>q;
    vis[s1][s2]=1;
    node cur,now;
    node s;
    s.x=s1;s.y=s2;s.t=0;
    q.push(s);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            now.x=cur.x+dir[i][0];
            now.y=cur.y+dir[i][1];
            now.t=cur.t+1;
            if(now.x<0||now.x>=n||now.y<0||now.y>=m||vis[now.x][now.y]||mm[now.x][now.y]==‘X‘) continue;
            if(mm[now.x][now.y]>=‘1‘&&mm[now.x][now.y]<=‘9‘)
            {
                now.t+=mm[now.x][now.y]-‘0‘;
            }
            if(now.x==(n-1)&&now.y==(m-1))//到达终点,倒退回去找路径
            {
                int num=0,temp1,temp2;
                e1=now.x;e2=now.y;
                pre[now.x][now.y]=cur;
                while(e1!=0||e2!=0)//或的关系!!不是与,小bug调试了很久。。。
                {

                    if(mm[e1][e2]>=‘1‘&&mm[e1][e2]<=‘9‘)
                    {
                       for(int j=0;j<mm[e1][e2]-‘0‘;j++)
                       {
                           record[now.t-num].x1=pre[e1][e2].x;
                           record[now.t-num].y1=pre[e1][e2].y;
                           record[now.t-num].x2=e1;
                           record[now.t-num].y2=e2;
                           num++;
                       }
                    }
                        record[now.t-num].x1=pre[e1][e2].x;
                        record[now.t-num].y1=pre[e1][e2].y;
                        record[now.t-num].x2=e1;
                        record[now.t-num].y2=e2;
                        temp1=pre[e1][e2].x;
                        temp2=pre[e1][e2].y;
                        //printf("(%d,%d)->(%d,%d)\n",e1,e2,temp1,temp2);
                        e1=temp1;e2=temp2;
                    num++;
                }
                return now.t;
            }
            vis[now.x][now.y]=1;
            q.push(now);
            pre[now.x][now.y]=cur;//保存上一步
        }
    }
    return -1;
}
int main()
{
    int i,j,k,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mm,0,sizeof(mm));
        memset(vis,0,sizeof(vis));
        memset(quan,0,sizeof(quan));
        memset(pre,0,sizeof(pre));
        memset(route,0,sizeof(route));
        memset(record,0,sizeof(record));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf(" %c",&mm[i][j]);
                s1=0,s2=0;
            }
        }
        ans=bfs();
        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);
        if(ans!=-1)
        {
            for(i=1;i<=ans;i++)
            {
                printf("%ds:",i);
                if(record[i-1].x2==record[i].x2&&record[i-1].y2==record[i].y2)
                    printf("FIGHT AT (%d,%d)\n",record[i].x2,record[i].y2);
                else
                printf("(%d,%d)->(%d,%d)\n",record[i].x1,record[i].y1,record[i].x2,record[i].y2);
            }
        }
        printf("FINISH\n");
    }
}
时间: 2024-11-11 13:08:19

HDU 1026 Ignatius and the Princess I(优先队列+打印路径)的相关文章

hdu 1026 Ignatius and the Princess I (bfs打印路径)

Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrint

HDU 1026 Ignatius and the Princess I(BFS+记录路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15576    Accepted Submission(s): 4937 Special Judge Problem Description The Princess has been abducted by the BEelzeb

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间. 比较麻烦的是需要记录路径.还要记录是在走路还是在打怪. 因为求最短路,所以可以使用bfs. 因为进过每一个点花费时间不同,所以可以使用优先队列. 因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点.当然,记录方法不止一种. 上代码—— 1 #include <c

hdu 1026 Ignatius and the Princess I (BFS+优先队列)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11700    Accepted Submission(s): 3653Special Judge Problem Description The Princess has been abducted by the BEelzebub

HDU 1026 Ignatius and the Princess I 迷宫广搜剪枝问题

本题是个经典的迷宫广搜问题类型了.网上看到好多解法. 很多解题报告都没什么分析,更不会指出其中的关键点.代码更加像一大抄.有人分析也一大篇分析,不过全部都不切中关键,甚至在分析什么广搜和深搜区别,广搜为什么快之类的,还有喊什么暴搜之类的,全错了.估计这些代码都是抄过的. 通过一大段的时间研究,终于搞通了. 本题虽然可以说是广搜,但是其中的关键却是剪枝法,为什么呢? 因为迷宫并不能简单地广搜就能搜索出所有路径的,甚至只要迷宫大点就不能搜索出是否有路径,如果没有条件剪枝的情况下:不信,你严格写一个广

HDU 1026 Ignatius and the Princess I (基本算法-BFS)

Ignatius and the Princess I Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem si

hdu 1026 Ignatius and the Princess I 广搜+优先队列+记录路径

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13414    Accepted Submission(s): 4232 Special Judge Problem Description The Princess has been abducted by the BEelzeb

HDU 1026 Ignatius and the Princess I(BFS+路径输出)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19800    Accepted Submission(s): 6452Special Judge Problem Description The Princess has been abducted by the BEelzebub

HDU - 1026 Ignatius and the Princess I

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Special Judge Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Prin