杭电 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): 15516    Accepted Submission(s): 4911
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
 


这个题目就是一道简单的优先队列+BFS能过的题目,但是输出明显墨迹的要死。。。。我们这里先说明为什么要加入优先队列呢?因为我们要干掉卫兵的存在。

我们都知道,BFS有贪心的特性,也可以说BFS有路径优先的特性,但是我们这里要的是时间优先,并不是路径优先,所以呢,我们这里就要应用优先队列改变优先条件,让我们控制的不再是路径优先,变成了时间优先就行了。

那么我们怎么处理这个输出结果呢?

我们这里来谈一下初始化,相信大家就会有相应的思路了:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;//头文件
struct zuobiao
{
    int x,y,output;
    friend bool operator <(zuobiao a,zuobiao b)
    {
        return a.output>b.output;
    }//优先队列output
    int zoufa[1000][2];//走法的记录。
}now,nex;
int vis[1000][1000];//访问数组
char a[1000][1000];//图
int n,m;
int fx[4]={0,0,1,-1};//走法
int fy[4]={1,-1,0,0};

我们这里用结构体里边的zoufa【】【】来完成走的步的记录。对于这个记录的时候 ,有一些细节的处理是经验的问题,这里上代码的核心部分:

void bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    priority_queue<zuobiao>s;
    now.x=x;
    now.y=y;
    now.output=0;
    now.zoufa[0][0]=x;
    now.zoufa[0][1]=y;
    vis[x][y]=1;
    s.push(now);
    while(!s.empty())
    {
        now=s.top();
        if(now.x==n-1&&now.y==m-1)//如果走到了终点,按需求输出,这里边的东西比较好理解,就不啰嗦了。
        {
            int bijiaox=0,bijiaoy=0;
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.output);
            for(int i=0;i<now.output;i++)
            {
                if(bijiaox==now.zoufa[i+1][0]&&bijiaoy==now.zoufa[i+1][1])
                {
                    printf("%ds:FIGHT AT (%d,%d)\n",i+1,now.zoufa[i+1][0],now.zoufa[i][1]);
                    bijiaox=now.zoufa[i+1][0];
                    bijiaoy=now.zoufa[i+1][1];
                    continue;
                }
                printf("%ds:(%d,%d)->(%d,%d)\n",i+1,now.zoufa[i][0],now.zoufa[i][1],now.zoufa[i+1][0],now.zoufa[i+1][1]);
                bijiaox=now.zoufa[i+1][0];
                bijiaoy=now.zoufa[i+1][1];
            }
            printf("FINISH\n");
            return ;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex=now;//关键的代码处理点在这里,如果不在这里copy上一步的内容,我们这里边最终输出的zoufa【】【】是会丢东西的
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]!='X')
            {
                if(a[nex.x][nex.y]=='.')//如果走的是路
                {
                    nex.output=now.output+1;//直接都+1就行
                    int num=nex.output;
                    nex.zoufa[num][0]=nex.x;
                    nex.zoufa[num][1]=nex.y;
                    vis[nex.x][nex.y]=1;
                    s.push(nex);
                }
                if(a[nex.x][nex.y]>='1'&&a[nex.x][nex.y]<='9')//如果遇到的是卫兵
                {
                    int xunhuan=a[nex.x][nex.y]-'0';//我们这里要先往上走一步
                    nex.output=now.output+1;
                    int num=nex.output;
                    nex.zoufa[num][0]=nex.x;
                    nex.zoufa[num][1]=nex.y;
                    for(int k=0;k<xunhuan;k++)//再和卫兵大战五百合
                    {
                        nex.output+=1;
                        int num=nex.output;
                        nex.zoufa[num][0]=nex.x;
                        nex.zoufa[num][1]=nex.y;
                        vis[nex.x][nex.y]=1;
                    }
                    s.push(nex);
                }
            }
        }
    }
    printf("God please help our poor hero.\nFINISH\n");//如果没走到终点要对应输出语句。
}
然后是完整的AC代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct zuobiao
{
    int x,y,output;
    friend bool operator <(zuobiao a,zuobiao b)
    {
        return a.output>b.output;
    }
    int zoufa[1000][2];
}now,nex;
int vis[1000][1000];
char a[1000][1000];
int n,m;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
void bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    priority_queue<zuobiao>s;
    now.x=x;
    now.y=y;
    now.output=0;
    now.zoufa[0][0]=x;
    now.zoufa[0][1]=y;
    vis[x][y]=1;
    s.push(now);
    while(!s.empty())
    {
        now=s.top();
        if(now.x==n-1&&now.y==m-1)
        {
            int bijiaox=0,bijiaoy=0;
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",now.output);
            for(int i=0;i<now.output;i++)
            {
                if(bijiaox==now.zoufa[i+1][0]&&bijiaoy==now.zoufa[i+1][1])
                {
                    printf("%ds:FIGHT AT (%d,%d)\n",i+1,now.zoufa[i+1][0],now.zoufa[i][1]);
                    bijiaox=now.zoufa[i+1][0];
                    bijiaoy=now.zoufa[i+1][1];
                    continue;
                }
                printf("%ds:(%d,%d)->(%d,%d)\n",i+1,now.zoufa[i][0],now.zoufa[i][1],now.zoufa[i+1][0],now.zoufa[i+1][1]);
                bijiaox=now.zoufa[i+1][0];
                bijiaoy=now.zoufa[i+1][1];
            }
            printf("FINISH\n");
            return ;
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex=now;
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]!='X')
            {
                if(a[nex.x][nex.y]=='.')
                {
                    nex.output=now.output+1;
                    int num=nex.output;
                    nex.zoufa[num][0]=nex.x;
                    nex.zoufa[num][1]=nex.y;
                    vis[nex.x][nex.y]=1;
                    s.push(nex);
                }
                if(a[nex.x][nex.y]>='1'&&a[nex.x][nex.y]<='9')
                {
                    int xunhuan=a[nex.x][nex.y]-'0';
                    nex.output=now.output+1;
                    int num=nex.output;
                    nex.zoufa[num][0]=nex.x;
                    nex.zoufa[num][1]=nex.y;
                    for(int k=0;k<xunhuan;k++)
                    {
                        nex.output+=1;
                        int num=nex.output;
                        nex.zoufa[num][0]=nex.x;
                        nex.zoufa[num][1]=nex.y;
                        vis[nex.x][nex.y]=1;
                    }
                    s.push(nex);
                }
            }
        }
    }
    printf("God please help our poor hero.\nFINISH\n");
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
        }
        bfs(0,0);
    }
}








你可能感兴趣的:(HDU,杭电,bfs,1026,1026)