hdu1026(bfs+优先队列+打印路径)

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14577    Accepted Submission(s): 4613
Special Judge

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 labyrinth is a N*M two-dimensional array which left-top corner is
(0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0),
and the door to feng5166‘s room is at (N-1,M-1), that is our target.
There are some monsters in the castle, if Ignatius meet them, he has to
kill them. Here is some rules:

1.Ignatius can only move in four
directions(up, down, left, right), one step per second. A step is
defined as follow: if current position is (x,y), after a step, Ignatius
can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your
task is to give out the path which costs minimum seconds for Ignatius
to reach target position. You may assume that the start position and the
target position will never be a trap, and there will never be a monster
at the start position.

Input

The
input contains several test cases. Each test case starts with a line
contains two numbers N and M(2<=N<=100,2<=M<=100) which
indicate the size of the labyrinth. Then a N*M two-dimensional array
follows, which describe the whole labyrinth. The input is terminated by
the end of file. More details in the Sample Input.

Output

For
each test case, you should output "God please help our poor hero." if
Ignatius can‘t reach the target position, or you should output "It takes
n seconds to reach the target position, let me show you the way."(n is
the minimum seconds), and tell our hero the whole path. Output a line
contains "FINISH" after each test case. If there are more than one path,
any one is OK in this problem. More details in the Sample Output.

Sample Input

5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.

Sample Output

It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH

Author

Ignatius.L

Recommend

We have carefully selected several similar problems for you:  1072 1175 1010 1180 1016

这题其实可以倒着搜,就不用栈了.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 210
#define inf  0x3f3f3f3f3f3f
char Map[maxn][maxn];
int visit[maxn][maxn];
int dir[4][2]={0,-1,0,1,-1,0,1,0};
int n,m;
int pre_x[maxn][maxn],pre_y[maxn][maxn];
int rear_x[maxn][maxn],rear_y[maxn][maxn];
int cnt;
struct node
{
    int x,y,dist=0;
};
struct cmp
{
    bool operator () (node a,node b)
    {
        if(a.dist>b.dist)   //从小到大排序
            return true;
        else
            return false;
    }
};

priority_queue <node,vector <node>,cmp>  pq;

void init()
{
        cnt=1;
     memset(visit,0,sizeof(visit));
     memset(pre_x,-1,sizeof(pre_x));
     memset(pre_y,-1,sizeof(pre_y));
     memset(rear_x,-1,sizeof(rear_x));
     memset(rear_y,-1,sizeof(rear_y));
     pre_x[0][0]=0;
     pre_y[0][0]=0;
     rear_x[n-1][m-1]=n-1;
     rear_y[n-1][m-1]=m-1;
   // for(int i=1;i<maxn;i++)
       // printf("%d",pre_x[i]);
  //   printf("%d %d\n\n",pre_x[1],pre_y[1]);
     while(!pq.empty())
        pq.pop();
}
int bfs(node start)
{
         pq.push(start);  //值传递
         node cur;
         visit[start.x][start.y]=1;
         while(!pq.empty())
         {
             cur=pq.top();
             pq.pop();
             if(cur.x==n-1 && cur.y==m-1)
             {
                 printf("It takes %d seconds to reach the target position, let me show you the way.\n",cur.dist);
                   int father_x=cur.x,father_y=cur.y,xx,yy;
                   while(pre_x[father_x][father_y]!=father_x || pre_y[father_x][father_y]!=father_y)
                   {
                      xx=father_x; yy=father_y;
                      father_x=pre_x[xx][yy];
                      father_y=pre_y[xx][yy];
                      rear_x[father_x][father_y]=xx;
                      rear_y[father_x][father_y]=yy;
                   }
                    int son_x=0,son_y=0;
                    while(rear_x[son_x][son_y]!=son_x || rear_y[son_x][son_y]!=son_y)
                   {
                      printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,son_x,son_y,rear_x[son_x][son_y],rear_y[son_x][son_y]);
                      xx=son_x; yy=son_y;
                      son_x=rear_x[xx][yy];
                      son_y=rear_y[xx][yy];
                      if(Map[son_x][son_y]!=‘.‘ && Map[son_x][son_y]!=‘X‘)
                        for(int i=1;i<=(Map[son_x][son_y]-‘0‘);i++)
                          printf("%ds:FIGHT AT (%d,%d)\n",cnt++,son_x,son_y);
                   }
                   printf("FINISH\n");
                 return 1;
             }
             for(int i=0;i<4;i++)
             {
                 node next;  int x,y;
                 next.x=x=cur.x+dir[i][0];
                 next.y=y=cur.y+dir[i][1];
                 if(0<=x && x<n && 0<=y && y<m && visit[x][y]==0 && Map[x][y]!=‘X‘)
                 //只被更新一次,与优先队列优化的Dijkstra的区别在于
                 //更新点到每个有可能更新它的点的权值是固定的,所以弹出来最短的点,然后更新,就是最短的。
                 //优先队列优化的Dijkstra,它多次被松弛的原因是在于弹出来的点是最短路径。但是
                 //更新点可能有多条路径到达更新点,假设更新点是最后一个点,那么倒数第二个点到这个更新点的权值不一样,
                 //所以每条路径上的到倒数第二个点的最短路径加倒数第二个点与倒数第一个点的边的权值就可能会让更新点被多次更新
                 //然而如果在类似于hdu1026,这样的图中,由于更新点到每个有可能更新它的点的权值是固定的,所以
                 //只需要用到倒数第二个点的最短路径去更新它即可,从优先队列里弹出来的点就是到达每个点的最短路径,
                 //所以只需要把bfs中的普通队列换成优先队列即可。
                 {
                     if(Map[x][y]==‘.‘)
                     {
                         next.dist=cur.dist+1;
                         pq.push(next);
                         pre_x[next.x][next.y]=cur.x;
                         pre_y[next.x][next.y]=cur.y;
                         visit[next.x][next.y]=1;
                     }
                     else
                     {
                         int step=Map[x][y]-‘0‘+1;
                         next.dist=cur.dist+step;
                         pq.push(next);
                         pre_x[next.x][next.y]=cur.x;
                         pre_y[next.x][next.y]=cur.y;
                         visit[next.x][next.y]=1;
                     }
                 }
             }
         }
          return 0;
}

int main()
{
   // freopen("test.txt","r",stdin);
    while(~scanf("%d%d%*c",&n,&m))
    {
        init();
        for(int i=0;i<n;i++)
        {
            scanf("%s",Map[i]);
        }
            node start;
            start.x=0;
            start.y=0;
            start.dist=0;
            if(bfs(start)==0)
              printf("God please help our poor hero.\nFINISH\n");

    }
    return 0;
}
时间: 2024-10-20 16:16:45

hdu1026(bfs+优先队列+打印路径)的相关文章

hdu--1026--bfs&amp;&amp;优先队列&amp;&amp;打印路径

这题 是被我自己搞复杂了.... 太SB了.... 还是porker的关于输出路径的简洁 有效多了 touch  me #include <iostream> #include <cstring> #include <queue> #include <stack> using namespace std; int ans, n, m; const int size = 110; char maze[size][size]; bool vis[size][si

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

HDU ACM 1026 Ignatius and the Princess I -&gt; BFS+优先队列+路径打印

分析:在BFS中使用优先队列即可获取最小值. #include<iostream> #include<queue> using namespace std; //BFS+优先队列(打印路径) #define N 101 struct Node //节点 { int x,y,time; friend bool operator<(const Node& a,const Node& b) //有限队列根据时间做依据 { return a.time>b.tim

POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A.B,经过6种操作使获得C升水,求最少操作数: 思路:广搜.最少操作数很简单,练习一下打印路径:打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int A,B,C; int shortest[150][150];//更新最短步数

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

HDU 1242 -Rescue (双向BFS)&amp;amp;&amp;amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚開始并没有认为像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们居然用BFS+优

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

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,