• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
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): 28700 Accepted Submission(s): 9240
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

思路 优先队列BFS + 回溯

​ 我们用优先队列存储点的信息,每次选择的时候优先选择代价较小的结点入队,这样能够保证代价高的结点不会被更新

(其实优先队列BFS 就是 堆优化的Dijkstra)

​ 普通的BFS,假设代价为5的先入队,那么其下方的结点到源点的最短路就会被更新为8,显然这样的方法是错误的,我们用优先队列可以保证代价较小的点先入队,这样我们用BFS的紧迫性(即,层序遍历结点,如果先搜到那么必然是最短路),来解释先到达的路一定是最短的。

image-20230113155009140

Code

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#define rep(i, j, k) for (int i = j; i <= k; i ++)

using namespace std;
typedef pair<int, int> PII;

int n, m, d[110][110];
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};

char mp[110][110];

bool judge = false, vis[110][110];

struct node {
        int x, y, dis;
        bool operator < (struct node b) const& {
                return this->dis > b.dis;
        }
};

struct road {
        int x, y, sec;
};

vector<road> res;

road path[110][110];

void print(int x, int y) {

        if (x == 1 && y == 1) {
                res.push_back({x, y, 0});
                return;
        }
        else {
                print(path[x][y].x, path[x][y].y);
        }
        res.push_back({x, y, 0});
        //cout << x - 1 << " " << y - 1 <<  " " << path[x][y].sec << endl;
}

bool check(int x, int y) {
        return x >= 1 && x <= n && y >= 1 && y <= m && d[x][y] == -1 && mp[x][y] != 'X' && !vis[x][y];
}

void bfs() {
        memset(d, -1, sizeof d);
        memset(vis, 0, sizeof vis);
        priority_queue<node> heap;
        res.clear();
        heap.push({1, 1, 0});
        d[1][1] = 0;
        while (heap.size()) {
                auto t = heap.top();
                heap.pop();
                //cout << t.x << " " << t.y << endl;
                if (t.x == n && t.y == m) {
                        judge = true;
                        printf("It takes %d seconds to reach the target position, let me show you the way.\n", t.dis);
                        print(n, m);
                        for (int i = 1; i < res.size(); i ++) {
                                int x = res[i].x, y = res[i].y, lx = res[i - 1].x, ly = res[i - 1].y;
                                int sec = d[x][y] + 1;
                                printf("%ds:(%d,%d)->(%d,%d)\n",sec,lx - 1,ly - 1,x - 1,y - 1);
                                if (mp[x][y] != '.') {
                                        for (int i = 1; i <= mp[x][y] - '0'; i ++) {
                                                printf("%ds:FIGHT AT (%d,%d)\n",sec + i, x - 1, y - 1);
                                        }
                                }
                        }
                        return;
                }
                rep (i, 0, 3) {
                        int tx = t.x + dx[i], ty = t.y + dy[i];
                        if (check(tx, ty)) {
                                node a = {tx, ty, 0};
                                if (mp[tx][ty] != '.')
                                        a.dis = t.dis + mp[tx][ty] - '0' + 1;
                                else
                                        a.dis = t.dis + 1;
                                d[tx][ty] = t.dis;
                                heap.push(a);
                                vis[tx][ty] = true;
                                path[tx][ty].x = t.x, path[tx][ty].y = t.y, path[tx][ty].sec = t.dis;
                        }
                }
        }
}


int main() {
        while (cin >> n >> m) {
                judge = false;
                rep (i, 1, n)
                        rep (j, 1, m)
                                cin >> mp[i][j];
                bfs();
                if (!judge)
                        cout << "God please help our poor hero." << endl;
                cout << "FINISH" << endl;
        }
}
posted on 2023-01-13 16:07  Jack404  阅读(17)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3