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): 16895    Accepted Submission(s): 5402
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


1.一定要注意细节  经常有字母打错  找了好久BUG

2.理清思路

3.第二种方法中不明白为什么用ckeck代替if条件语句就错误,求广大网友指正



以下给出两个路径输出的方法 

第一种是DFS递归调用

第二种方法是stack的使用


/*
优先队列
BUG
1.
在BFS里面可以输出step
return 返回到主函数后  输出乱码
2.
终点如果还有数字 不会加上终点的数字
3.
n.y
#include
#include
using namespace std;
struct node
{
    int x,y,step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
};
char map[105][105];
int hp[105][105];
int vis[105][105];
int n,m;
int stepp[4][2]= {1,0,-1,0,0,1,0,-1}; //下上右左
int minn;
int time;
int flag;
int pre[105][105];
bool check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m)//越界
        return false;
    if(map[x][y]=='X')//墙
        return false;
    if(vis[x][y]==true)//走过了
        return false;
    return true;
}
void BFS()
{
    int bx,by;
    priority_queue q;
    node a,b;
    a.x=0;
    a.y=0;
    a.step=0;
    memset(vis,0,sizeof(vis));
    vis[0][0]=1;
    q.push(a);
    while(!q.empty())
    {
        a=q.top();
        q.pop();
        //printf("%d %d %d\n",a.x,a.y,a.step);
        if(a.x == n-1 && a.y == m-1)
        {
            flag=1;
            minn=a.step;
            return ;
        }
        for(int i=0; i<4; i++)
        {
            b.x=a.x+stepp[i][0];
            b.y=a.y+stepp[i][1];
            //if(check(bx,by))//如果能走
            if(check(b.x,b.y))
            {
                //b.y(%d,%d)\n",time++,nx,ny,x,y);
    while(hp[x][y]--)
    {
        printf("%ds:FIGHT AT (%d,%d)\n",time++,x,y);
    }
}
int main(void)
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(pre,0,sizeof(pre));
        for(int i=0; i


第二种:





#include
#include
#include
#include
#include
using namespace std;
struct node
{
    int x;
    int y;
    int step;
    friend bool operator<(node a,node b)
    {
        return a.step>b.step;
    }
} a,b;
struct node1
{
    int x;
    int y;
    int num;
} pre[105][105];
int hp[105][105];
int step[4][2]= {1,0,-1,0,0,1,0,-1};
int vis[105][105];
int m,n;
int minn;
char map[105][105];
int check(int x,int y)//用check就错误 不知道为什么
{
    if(x=0&&y>=0&&y q;
    int bx,by;
    int vis[105][105];
    memset(vis,0,sizeof(vis));
    vis[0][0]=1;
    a.x=0;
    a.y=0;
    a.step=0;
    minn=0;
    q.push(a);
    while(!q.empty())
    {
        a=q.top();
        // printf("%d %d %d \n",a.x,a.y,a.step);
        q.pop();
        if(a.x==x&&a.y==y)
        {
            minn=a.step;
            return ;
        }
        for(int i=0; i<4; i++)
        {
            bx=a.x+step[i][0];
            by=a.y+step[i][1];
            if(bx>=0 && bx=0 && by Q;
    node1 aa,bb;
    bb.x=n-1;
    bb.y=m-1;
    bb.num=hp[n-1][m-1];//之前输入 bb.num=pre[n-1][m-1].pre;如果终点(n-1,m-1)的HP是1 bb.num是1, 而不是2 应该把 2传给bb.num(走到time+1 杀怪time+1)
    Q.push(bb);

    for(x1=n-1, y1=m-1;;)
    {
        x2=x1;
        y2=y1;
        Q.push(pre[x2][y2]);
        x1=pre[x2][y2].x;
        y1=pre[x2][y2].y;
        if(x1==0&&y1==0)break;
    }
    while(!Q.empty())
    {
        if(Q.size()-1==0&&Q.top().num==1)//最后一个跳出条件
            break;
        aa=Q.top();
        Q.pop();
        if(Q.size()>0)
            bb=Q.top();
        if(aa.num==1)
            printf("%ds:(%d,%d)->(%d,%d)\n",time++,aa.x,aa.y,bb.x,bb.y);
        else
        {
            for(int i=1; i0)
                printf("%ds:(%d,%d)->(%d,%d)\n",time++,aa.x,aa.y,bb.x,bb.y);
        }
    }
}
int main (void)
{
    int x1,y1;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0; i


你可能感兴趣的:(ACM__难点,ACM__BFS,ACM解题报告)