HDU1026 Ignatius and the Princess I【优先搜索】

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20304    Accepted Submission(s): 6619
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
 


问题链接HDU1026 Ignatius and the Princess I

题意简述

  从矩阵的左上角走到右下角所需的最短时间,并且要求输出走的过程。矩阵中"."是可以走的,"X"是墙,n(数字1-9)是怪兽,需要战斗数字所示的时间。对于每个测试实例,先输入n和m,分别表示行数和列数,然后输入矩阵。

问题分析

  显然求最短路径问题用BFS,另外由于有怪兽,所以搜索过程需要使用优先队列。一个典型的用分支限界法解决的问题。最小路径上每个点到出发点距离应该是最小的。

程序说明

  用节点矩阵grid[][]存储输入的矩阵,同时存储输入的矩阵和到起始点需要行走的最少步骤,以及最小路径上前一个节点坐标。这个程序运行时间上算是比较短的。


AC的C++语言程序如下:

/* HDU1026 Ignatius and the Princess I */

#include 
#include 
#include 
#include 
#include 

using namespace std;

#define DIRECTSIZE 4

struct direct {
    int drow;
    int dcol;
} direct[DIRECTSIZE] =
    {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

const int MAXN = 100;
const unsigned int MAXTIME = MAXN*MAXN*9+1;

struct gridnode {
    char v;
    int prevrow, prevcol;
    unsigned int mintime;
};

gridnode grid[MAXN+1][MAXN+1];

struct node {
    int row, col;
    unsigned int sumtime;

    bool operator<(const node n) const {
        return sumtime > n.sumtime;
    }
};

int n, m;
unsigned int mintime;
node start, end2, f, v;

int bfs()
{
    int ans = 0;

    grid[0][0].prevrow = -1;
    grid[0][0].prevcol = -1;
    grid[0][0].mintime = 0;

    start.row = 0;
    start.col = 0;
    start.sumtime = 0;

    end2.row = n - 1;
    end2.col = m - 1;

    mintime = MAXTIME;

    priority_queue q;
    q.push(start);

    while(!q.empty()) {
        f = q.top();
        q.pop();

        if(f.row == end2.row && f.col == end2.col) {
            if(f.sumtime < mintime) {
                mintime = f.sumtime;
                ans = 1;
                continue;
            }
        }

        if(f.sumtime >= mintime)
            continue;

        for(int i=0; i s;
    node v1, v2;

    v.row = end2.row;
    v.col = end2.col;
    s.push(v);
    while(grid[v.row][v.col].prevrow != -1 || grid[v.row][v.col].prevcol != -1) {
        v2 = v;
        v.row = grid[v2.row][v2.col].prevrow;
        v.col = grid[v2.row][v2.col].prevcol;
        s.push(v);
    }


    printf("It takes %d seconds to reach the target position, let me show you the way.\n", mintime);

    int count = 0;
    v1 = s.top();
    s.pop();
    while(!s.empty()) {
        v2 = s.top();
        s.pop();
        printf("%ds:(%d,%d)->(%d,%d)\n", ++count, v1.row, v1.col, v2.row, v2.col);
        if(grid[v2.row][v2.col].v != '.') {
            for(int j=1; j<=grid[v2.row][v2.col].v-'0'; j++)
                printf("%ds:FIGHT AT (%d,%d)\n", ++count, v2.row, v2.col);
        }

        v1 = v2;
    }
}

char mygetchar()
{
    char c;

    while((c = getchar()) && (c == ' ' || c == '\t' || c == '\n'));

    return c;
}

int main()
{
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i=0; i


你可能感兴趣的:(#,ICPC-备用二,#,ICPC-HDU,#,ICPC-优先搜索)