HDU1026——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): 17276    Accepted Submission(s): 5530
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
题意:
问从图的左上角到达右下角需要的最短时间,如果格子是数字n(1-9),说明有怪兽,要打死他花费n的时间
解题思路:
历经九九三天三夜之战,终于拿下了这道题,这道题刚开始我没有用优先队列,结果很明显,超时。
后来用了,结果还是WA了,我相信各位思路不是问题了,就差几组数据,看看自己的代码哪有问题,那我在这里就双手奉上了:
5 7 ...X... .X.X.X. .X...X. .XXXXX. ....... 5 7 ...X... .X.X.X. .X...X. .XXXXX. ....9..
4 2 .X 21 2X 2.
5 6
..X.X.
X.X..4
X...X.
.3491.
......
相信够你用了。
本人呕心沥血之作(自己写,最好不要看,很乱生气):
代码实现:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;
const int maxn = 1000000+10;
int n,m,kk,temp,k,lv;
char mp[101][101];
int dis[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
int tt[101][101];

struct node
{
    int x, y, z;
    int prex,prey;
    int pre,ti;
    bool operator < (const node &a) const      // 这玩意不是很熟;
    {
        return a.ti < ti;
    }
}p[20010];

void pp(int a)
{
    //cout << p[a].prex << " " << p[a].prey <<endl;
    if( p[a].x == 0 && p[a].y == 0 )
    {
        //printf("%ds:(0,0)->(%d%,%d)\n",kk++,p[a].x,p[a].y);   //这里注意输出细节;
        return;
    }
    pp(p[a].pre);                                               // 递归注意位置;
    printf("%ds:(%d,%d)->(%d,%d)\n",kk++,p[a].prex,p[a].prey,p[a].x,p[a].y);
    if( mp[p[a].x][p[a].y]!='.' )
    {
        int w = mp[p[a].x][p[a].y] - '0';
        while( w-- )
        {
            printf("%ds:FIGHT AT (%d,%d)\n",kk++,p[a].x,p[a].y);
        }
    }

}

void print()
{
    printf("It takes %d seconds to reach the target position, let me show you the way.\n",tt[n-1][m-1]);
    pp(lv);
}

void bfs(int a, int b)
{
    p[0].x = a;
    p[0].y = b;
    p[0].prex = -1;
    p[0].prey = -1;
    p[0].pre = -1;
    p[0].ti = 0;                                       //到达该点的时间;
    tt[a][b] = 0;
    k=0; kk=1;
    p[0].z = 0;                                        //记录当前是第几个点;
    int flag = 0;
    priority_queue<node>q;
    q.push(p[k++]);
    while( !q.empty() )
    {
        node t ;
        t = q.top();
        q.pop();
        int h = t.z;                                   // 前驱节点号;
        for( int i=0; i<4; i++ )
        {
            int nowx = t.x+dis[i][0], nowy = t.y+dis[i][1];
            if(nowx!=t.prex || nowy!=t.prey)
            {
                if( nowx>=0 && nowx<n && nowy>=0 && nowy<m ){
                if( mp[nowx][nowy]!='X' )
                {
                    if( tt[nowx][nowy]==-1 || (mp[nowx][nowy]=='.' && t.ti+1<tt[nowx][nowy])
                       || (mp[nowx][nowy]!='.' && t.ti+1+(int)(mp[nowx][nowy]-'0')<tt[nowx][nowy]))
                   {
                        if( mp[nowx][nowy]!='.' && tt[nowx][nowy]!=-1 && tt[nowx][nowy] < t.ti+1+(mp[nowx][nowy]-'0') )
                            continue;
                        //cout << k << " ";
                        //cout << nowx << " " << nowy << " ";
                        p[k].prex = t.x;
                        p[k].prey = t.y;
                        p[k].x = nowx;
                        p[k].y = nowy;
                        p[k].pre = h; 
                        p[k].z = k;
                        p[k].ti = t.ti+1;
                        //cout << h <<endl;
                        //tt[nowx][nowy] = t.ti+1;
                        if( mp[nowx][nowy]!='X' && mp[nowx][nowy]!='.')
                            p[k].ti += (int)mp[nowx][nowy]-'0';
                        tt[nowx][nowy] = p[k].ti;
                        if( nowx == n-1 && nowy == m-1 )
                        {
                            //cout << k <<endl;
                            lv = k;
                            flag = 1;
                            break;
                        }
                        q.push(p[k++]);
                   }
                   if( flag == 1 ) break;
                }
                if( flag == 1 ) break;
                }
                if( flag == 1 ) break;
            }if( flag == 1 ) break;
        }
        if( flag == 1 ) break;
    }

    //for( int i=0; i<n; i++ )
    //{
    //    for( int j=0; j<m; j++ )
    //        printf("%02d ",tt[i][j]);
    //    cout <<endl;
    //}

    if( flag==1 )//tt[n-1][m-1]!=-1 )
        print();
    else
        printf("God please help our poor hero.\n");
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(tt,-1,sizeof(tt));
        memset(mp,0,sizeof(mp));
        for( int i=0; i<n; i++ )
            scanf("%s",mp[i]);
        temp = 0;
        bfs(0, 0);
        printf("FINISH\n");
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值