HDU1026,BFS+SPFA

3 篇文章 0 订阅
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

题目大概意思是从(0,0)到(n-1,m-1)的最短时间,X表示墙,数字表示要停留的时间

一开始直接BFS,然后wa了好几次,后面看了大佬的博客才知道要考虑一个情况:就是我们可以绕过停留点也可以经过停留点

这里我们就可以用优先队列了,把用时时间少的往前排,这样到终点的就是答案了,输出的时候就用DFS就可以了

AC代码

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
    int t;         
    int x,y;
    friend bool operator < (node a,node b)
        {
            return a.t>b.t;
        }
};
int m,n;
int dir[4][2]={0,1,1,0,0,-1,-1,0},step[110][110],fight[110][110];/*step表示前一个点到(i,j)时的方向,fight表示停留的时间*/
char map[110][110];
bool vis[110][110];
int judge(int x,int y)
{
    if (x>=m||x<0||y>=n||y<0||vis[x][y]==true||map[x][y]=='X')
        return 0;
    return 1;
}
int bfs(int x,int y)
{
    int i;
    node now,end;
    now.x=x;now.y=y;
    now.t=0;
    step[x][y]=-1;
    fight[x][y]=map[x][y]>46?map[x][y]-'0':0;    //可能起点就有停留时间,这个要考虑
    vis[x][y]=true;
    priority_queue<node> q;  //优先队列
    while (!q.empty())
        q.pop();
    q.push(now);
    while (q.size())
        {
            now=q.top();
            q.pop();
            for (i=0;i<4;i++)
                {
                    end=now;
                    end.x=now.x+dir[i][0];
                    end.y=now.y+dir[i][1];
                    if (judge(end.x,end.y)==0)
                        continue;
                    step[end.x][end.y]=i;
                    fight[end.x][end.y]=map[end.x][end.y]>46?map[end.x][end.y]-'0':0;
                    end.t=now.t+1+fight[end.x][end.y];    //总时间要加上个这个点的停留时间的
                    q.push(end);
                    vis[end.x][end.y]=true;
                    if (end.x==m-1&&end.y==n-1)
                        return end.t;
                }
            //cout<<end.t<<endl;
        }
    return -1;
}
int sum;
void print(int x,int y)    //这里输出就可以用step记录来输出了,遇到了停留点的一并输出就可以了
{
    int next_x, next_y;
    if (step[x][y]==-1)
        return ;
    next_x=x-dir[step[x][y]][0];
    next_y=y-dir[step[x][y]][1];
    print(next_x,next_y);
    printf("%ds:(%d,%d)->(%d,%d)\n",sum++,next_x,next_y,x,y);
    while (fight[x][y]--)
        printf("%ds:FIGHT AT (%d,%d)\n",sum++,x,y);
}
int main()
{
    int ans=0,i,j;
    while (cin>>m>>n)
        {
            memset(map,0,sizeof(map));
            memset(vis,false,sizeof(vis));
            for (i=0;i<m;i++)
                cin>>map[i];
            ans=bfs(0,0);
            if (ans==-1)
                {
                    cout<<"God please help our poor hero."<<endl;
                    cout<<"FINISH"<<endl;
                }
            else 
                {
                    sum=1;
                    printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
                    print(m-1,n-1);
                    cout<<"FINISH"<<endl;
                }
        }
    return 0;
}
只能说自己太弱了......
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值