• HDU 1026 Ignatius and the Princess I(带路径的BFS)


    http://acm.hdu.edu.cn/showproblem.php?pid=1026

    题意:给出一个迷宫,求出到终点的最短时间路径。

    这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这个时候可以用优先队列,每次让时间最短的出队列。由于最后还需要输出路径,所以需要设置一个数组来保存路径。

     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 const int maxn = 100 + 5;
     7 
     8 struct node
     9 {
    10     int x, y;
    11     int time;
    12     friend bool operator < ( node a, node b)   //重载<号
    13     {
    14         return b.time<a.time;
    15     }
    16 };
    17 
    18 char map[maxn][maxn];
    19 int visited[maxn][maxn];
    20 int path[maxn][maxn];
    21 int  n, m;
    22 int d[][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
    23 
    24 int bfs()
    25 {    
    26     node q,now;
    27     priority_queue<node> p;
    28     q.x = 0;
    29     q.y = 0;
    30     q.time = 0;
    31     p.push(q);
    32     while (!p.empty())
    33     {
    34         q = p.top();
    35         p.pop();
    36         if (q.x == n - 1 && q.y == m - 1)  return q.time;
    37         for (int i = 0; i < 4; i++)
    38         {
    39             int xx = q.x + d[i][0];
    40             int yy = q.y + d[i][1];
    41             now.x = xx;
    42             now.y = yy;
    43             now.time = q.time;
    44             if (xx >= 0 && xx < n && yy >= 0 && yy < m && map[xx][yy]!='X' && !visited[xx][yy])
    45             {
    46                 if (map[xx][yy] == '.')   now.time++;
    47                 else now.time =now.time+ (map[xx][yy] - '0'+1);
    48                 visited[xx][yy] = 1;
    49                 path[xx][yy] = i + 1;     //记录路径
    50                 p.push(now);
    51             }
    52         }
    53     }
    54     return -1;
    55 }
    56 
    57 int temp;
    58 
    59 void print(int x, int y)
    60 {
    61     int xx, yy;
    62     if (path[x][y] == 0)   return;
    63     xx = x - d[path[x][y] - 1][0];   //寻找第一个路径点
    64     yy = y - d[path[x][y] - 1][1];
    65     print(xx, yy);
    66     printf("%ds:(%d,%d)->(%d,%d)
    ", temp++, xx, yy, x, y);
    67     if (map[x][y] <= '9' && map[x][y] >= '0')
    68     {
    69         int m = map[x][y] - '0';
    70         while (m--)    printf("%ds:FIGHT AT (%d,%d)
    ", temp++, x, y);
    71     }
    72 }
    73 
    74 int main()
    75 {
    76     while (cin >> n >> m && (n||m))
    77     {
    78         memset(visited, 0, sizeof(visited));
    79         memset(path, 0, sizeof(path));
    80         for (int i = 0; i < n;i++)
    81         for (int j = 0; j < m; j++)
    82             cin >> map[i][j];
    83         visited[0][0] = 1;
    84         int ans=bfs();
    85         if (ans == -1)  cout << "God please help our poor hero." << endl;
    86         else
    87         {
    88             cout << "It takes " << ans << " seconds to reach the target position, let me show you the way." << endl;
    89             temp = 1;
    90             print(n - 1,m - 1);
    91         }
    92         cout << "FINISH" << endl;
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    pureftp 超时 mlsd
    安卓相机调用适配
    解决多个界面重复共用同一组数据导致数据同步改变(实现数据的完全深拷贝)
    PHP反射API的使用、体会、说明
    c冒泡排序
    iOS开发常用的第三方类库
    修改按钮上图片的大小-iOS
    获取手机通讯录--ios
    根据搜素的字符串改变label包含该字符串的文字
    cell点击按钮崩的一种情况
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/6239152.html
Copyright © 2020-2023  润新知