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


    以前写的题了,现在想整理一下,就挂出来了。

    题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1)。

    'X'为墙,'.'为路,数字为怪物。墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间。

    比较麻烦的是需要记录路径。还要记录是在走路还是在打怪。

    因为求最短路,所以可以使用bfs。

    因为进过每一个点花费时间不同,所以可以使用优先队列。

    因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点。当然,记录方法不止一种。

    上代码——

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <queue>
      7 using namespace std;
      8 
      9 struct node
     10 {
     11     int x, y, step;
     12     bool operator < (const node& a) const
     13     {
     14         return a.step < step;
     15     }
     16 };
     17 
     18 int go[4][2] = {{1, 0},{-1, 0}, {0, 1}, {0, -1}};
     19 
     20 int n, m, step;
     21 bool v[110][110];
     22 char mp[110][110];
     23 int last[110][110];
     24 
     25 bool bfs()
     26 {
     27     node p, q;
     28     p.x = n-1;
     29     p.y = m-1;
     30     p.step = 0;
     31     if(mp[n-1][m-1] >= '0' && mp[n-1][m-1] <= '9') p.step += mp[n-1][m-1] -'0';
     32 
     33     priority_queue <node> que;
     34     if(mp[p.x][p.y] != 'X')
     35     que.push(p);
     36     v[p.x][p.y] = 1;
     37 
     38     while(!que.empty())
     39     {
     40         p = que.top();
     41         que.pop();
     42 
     43         for(int i = 0; i < 4; i++)
     44         {
     45             int x = p.x+go[i][0];
     46             int y = p.y+go[i][1];
     47 
     48             if(x >= 0 && x < n && y >= 0 && y < m && !v[x][y])
     49             {
     50                 if(mp[x][y] == 'X') continue;
     51 
     52                 q.x = x; q.y = y; q.step = p.step+1;
     53                 if(mp[x][y] >= '1' && mp[x][y] <= '9') q.step += mp[x][y]-'0';
     54                 que.push(q);
     55                 v[x][y] = 1;
     56                 last[x][y] = p.x*1000+p.y;
     57                 if(x == 0 && y == 0) {step = q.step; return 1;}
     58 
     59             }
     60         }
     61     }
     62     return 0;
     63 }
     64 
     65 void output()
     66 {
     67     printf("It takes %d seconds to reach the target position, let me show you the way.
    ", step);
     68     int x = 0;
     69     int y = 0;
     70     int i = 1;
     71     while(x != n-1 || y != m-1)
     72     {
     73         if(mp[x][y] >= '1' && mp[x][y] <= '9')
     74         {
     75             int stop = mp[x][y] - '0';
     76             while(stop--)
     77             {
     78                 printf("%ds:FIGHT AT (%d,%d)
    ", i++, x, y);
     79             }
     80         }
     81         printf("%ds:(%d,%d)->(%d,%d)
    ", i++, x, y, last[x][y]/1000, last[x][y]%1000);
     82 
     83         int t = last[x][y]/1000;
     84         y = last[x][y]%1000;
     85         x = t;
     86     }
     87     if(mp[x][y] >= '1' && mp[x][y] <= '9')
     88     {
     89         int stop = mp[x][y] - '0';
     90         while(stop--)
     91         {
     92             printf("%ds:FIGHT AT (%d,%d)
    ", i++, x, y);
     93         }
     94     }
     95 }
     96 
     97 int main()
     98 {
     99     //freopen("test.txt", "r", stdin);
    100     while(~scanf("%d%d", &n, &m))
    101     {
    102         memset(mp, 0, sizeof(mp));
    103         memset(v, 0, sizeof(v));
    104         memset(last, 0, sizeof(last));
    105         for(int i = 0; i < n; i++)
    106         {
    107             scanf("%s", mp[i]);
    108         }
    109 
    110         if(bfs()) output();
    111         else printf("God please help our poor hero.
    ");
    112         printf("FINISH
    ");
    113     }
    114     return 0;
    115 }
    View Code
  • 相关阅读:
    js 数组,字符串,json互相转换
    数据库相关概念
    信号量,Event, 定时器
    解决Navicat远程连接mysql很慢的方法
    Ubuntu安装mycli,让mysql命令行可以自动提示
    Requests模块调用接口
    selenium chrome浏览器对应chrome版本
    selenium 元素定位+显示等待 方法封装
    smtplib 发送文本文件和附件文件 的类方法封装
    python 数据库的方法封装
  • 原文地址:https://www.cnblogs.com/mypride/p/4695168.html
Copyright © 2020-2023  润新知