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

这题 是被我自己搞复杂了....

太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][size];
int arr[10010];
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };

struct data
{
    int x, y;
    int step;
    bool operator<(const data& p)const
    {
        return step>p.step;
    }
}st;

void bfs()
{
    data now, next;
    int xx, yy;
    priority_queue<data> q;
    while (!q.empty())
        q.pop();
    st.x = st.y = st.step = 0;
    q.push(st);
    vis[0][0] = true;
    while (!q.empty())
    {
        now = q.top();
        q.pop();
        if (now.x == n - 1 && now.y == m - 1)
        {
            ans = now.step;
            return;
        }
        for (int i = 0; i < 4; i++)
        {
            xx = now.x + dir[i][0];
            yy = now.y + dir[i][1];
            if (xx >= 0 && xx < n && yy >= 0 && yy < m && maze[xx][yy] != ‘X‘ && !vis[xx][yy])
            {
                vis[xx][yy] = true;
                next.x = xx;
                next.y = yy;
                arr[xx*m + yy] = now.x*m + now.y;
                if (maze[xx][yy] == ‘.‘)
                    next.step = now.step + 1;
                else
                    next.step = now.step + (maze[xx][yy] - ‘0‘) + 1;
                q.push(next);
            }
        }
    }
    return;
}

int main()
{
    cin.sync_with_stdio(false);
    stack<int>s;
    int num1, x1, y1, num2, x2, y2;
    int k, flag;
    while (cin >> n >> m)
    {
        flag = 0;
        while (!s.empty())
            s.pop();
        memset(vis, false, sizeof(vis));
        memset(arr, -1, sizeof(arr));
        ans = 0;
        k = 1;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> maze[i][j];
            }
        }
        bfs();
        if (ans)
        {
            cout << "It takes " << ans << " seconds to reach the target position, let me show you the way." << endl;
            for (int i = n*m - 1; i != 0; i = arr[i])
            {
                s.push(i);
            }
            int prex = 0, prey = 0;
            while( !s.empty() )
            {
                num1 = s.top();
                s.pop();
                x1 = num1 / m;
                y1 = num1 % m;
                cout << k++ << "s:(" << prex << "," << prey << ")->(" << x1 << "," << y1 << ")" << endl;
                if (maze[x1][y1] != ‘.‘)
                {
                    for (int i = 0; i < maze[x1][y1] - ‘0‘; i++)
                    {
                        cout << k++ << "s:FIGHT AT (" << x1 << "," << y1 << ")" << endl;
                    }
                }
                prex = x1;
                prey = y1;
            }
        }
        else
        {
            cout << "God please help our poor hero." << endl;
        }
        cout << "FINISH" << endl;
    }
    return 0;
}

这是他帮我修改后过的  贴下起初没过的。。。--纪念下自己的白痴的k ans 做法=-=

  1 #include <iostream>
  2 #include <cstring>
  3 #include <queue>
  4 #include <stack>
  5 using namespace std;
  6
  7 int ans , n , m;
  8 const int size = 110;
  9 char maze[size][size];
 10 bool vis[size][size];
 11 int arr[10010];
 12 int dir[4][2] = {1,0,-1,0,0,1,0,-1};
 13
 14 struct data
 15 {
 16     int x , y;
 17     int step;
 18     bool operator<(const data& p )const
 19     {
 20         return step>p.step;
 21     }
 22 }st;
 23
 24 void bfs( )
 25 {
 26     data now , next;
 27     int xx , yy;
 28     priority_queue<data> q;
 29     while( !q.empty() )
 30         q.pop();
 31     st.x = st.y = st.step = 0;
 32     q.push( st );
 33     vis[0][0] = true;
 34     while( !q.empty() )
 35     {
 36         now = q.top();
 37         q.pop();
 38         if( now.x == n-1 && now.y == m-1 )
 39         {
 40             ans = now.step;
 41             return;
 42         }
 43         for( int i = 0 ; i<4 ; i++ )
 44         {
 45             xx = now.x + dir[i][0];
 46             yy = now.y + dir[i][1];
 47             if( xx>=0 && xx<n && yy>=0 && yy<m && maze[xx][yy]!=‘X‘ && !vis[xx][yy] )
 48             {
 49                 vis[xx][yy] = true;
 50                 next.x = xx;
 51                 next.y = yy;
 52                 arr[xx*m+yy] = now.x*m+now.y;
 53                 if( maze[xx][yy] == ‘.‘ )
 54                     next.step = now.step + 1;
 55                 else
 56                     next.step = now.step + (maze[xx][yy] -‘0‘)+1;
 57                 q.push(next);
 58             }
 59         }
 60     }
 61     return;
 62 }
 63
 64 int main()
 65 {
 66     stack<int>s;
 67     int num1 , x1 , y1 , num2 , x2 , y2;
 68     int k , flag;
 69     while( cin >> n >> m )
 70     {
 71         flag = 0;
 72         while( !s.empty() )
 73             s.pop();
 74         memset( vis , false , sizeof(vis) );
 75         memset( arr , -1 , sizeof(arr) );
 76         ans = 0;
 77         k = 1;
 78         for( int i = 0 ; i<n ; i++ )
 79         {
 80             for( int j = 0 ; j<m ; j++ )
 81             {
 82                 cin >> maze[i][j];
 83             }
 84         }
 85         bfs( );
 86         if( ans )
 87         {
 88             cout << "It takes " << ans << " seconds to reach the target position, let me show you the way." << endl;
 89             for( int i = n*m-1 ; i!=-1 ; i = arr[i] )
 90             {
 91                 s.push( i );
 92             }
 93             /*
 94             while(!s.empty())
 95             {
 96                 cout << s.top() <<endl;
 97                 s.pop();
 98             }
 99             */
100             while( k<ans )
101             {
102                 num1 = s.top();
103                 x1 = num1/m;
104                 y1 = num1%m;
105                 s.pop();
106                 if( maze[x1][y1] ==‘.‘ )
107                 {
108                     if( !flag )
109                     {
110                         num2 = s.top();
111                         x2 = num2/m;
112                         y2 = num2%m;
113                         cout<<k<<"s: ("<<x1<<","<<y1<<")->("<<x2<<","<<y2<<")"<<endl;
114                     }
115                     else
116                     {
117                         x2 = flag/m;
118                         y2 = flag%m;
119                         cout<<k<<"s: ("<<x2<<","<<y2<<")->("<<x1<<","<<y1<<")"<<endl;
120                         flag = 0;
121                     }
122                     k++;
123                 }
124                 else
125                 {
126                     for( int j = 0 ; j<(maze[x1][y1]-‘0‘) ; j++ )
127                     {
128                         cout<<k+j<<"s:FIGHT AT ("<<x1<<","<<y1<<")"<<endl;
129                     }
130                     k+=( maze[x1][y1]-‘0‘ );
131                     flag = num1;
132                 }
133             }
134         }
135         else
136         {
137             cout << "God please help our poor hero." << endl;
138         }
139         cout << "FINISH" << endl;
140     }
141     return 0;
142 }

洗澡去了~再看部电影 碎觉 =-=

hdu--1026--bfs&&优先队列&&打印路径,布布扣,bubuko.com

时间: 2024-07-31 14:35:25

hdu--1026--bfs&&优先队列&&打印路径的相关文章

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): 4613Special Judge Problem Description The Princess has been abducted by the BEelzebub

HDU 1026 BSF+优先队列+记录路径、

#include<iostream> #include<cmath> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<stack> using namespace std; const int qq=110; const int MAX=10000; char map[qq][qq]; int pre[qq][q

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 1026 (BFS搜索+优先队列+记录方案)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解题思路: 要是没有输出方案,就是一个简单粗暴的BFS. 一开始解决输出方案问题时,简单粗暴地在每次状态里加个vector,然后连带vector一起转移. 结果vector的push_back实在太慢,无论怎么优化都是T. 于是参考了ACMan同学的方案,path[X][Y]记录下父亲点. 最后输出的

hdu 1026 bfs+记录路径

题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9

HDU 2822 (BFS+优先队列)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同,应该先搜cost小的.直接退化为最短路问题. 优先队列优化. 卡输入姿势.如果O(n^2)逐个读的话会T掉.要用字符串读一行. #include "cstdio" #include "queue" #include "cstring" using

HDU 3619 BFS+优先队列

点击打开链接 题意:给一个地图,从S走到T,然后给了钥匙的位置,地图上数字点代表如果走这个点则要消耗数字的能量,而A到E是门,一个钥匙可以开一类门,问最少消耗多少能量就可以走到T 思路:对于钥匙来说,直接用状态压缩判断钥匙是否取过,然后因为是要走最小的花费,那么要用优先队列,没什么可以注意的,就是一个钥匙可以开一类门,而不是只能开一个门,注意着谢谢就应该能过,并不难的一道BFS #include <queue> #include <stdio.h> #include <ios

HDU 4308 BFS+优先队列

点击打开链接 题意:从Y走到C,#代表不能走,走*的话要花费C元,P是传送门可以到达任意一个P,问最小花费 思路:直接优先队列模拟一下就行,BFS搜一下,P直接记录,遇到了就判断它能到达的点能不能走就行了,easy #include <queue> #include <stdio.h> #include <iostream> #include <string.h> #include <stdlib.h> #include <algorith

HDU 1026 bfs与dfs

一般来说,广搜常用于找单一的最短路线,或者是规模小的路径搜索,它的特点是"搜到就是最优解", 而深搜用于找多个解或者是"步数已知(比如3步就必须达到条件)"的问题,它的空间效率高,但是找到的不一定是最优解,必须记录并完成整个搜索,故一般情况下,深搜需要非常高效的剪枝(优化). 像搜索最短路径这些的很明显要是用广搜,因为广搜的特性就是一层一层往下搜的,保证当前搜到的都是最优解,当然,最短路径只是一方面的应用,像什么最少状态转换也是可以应用的.深搜就是优先搜索一棵子树,