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): 14569    Accepted Submission(s): 4608
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

题意:给定m行n列的地图,“.”表示道路,“1”~“9”表示有怪兽,数值为打败怪兽所需时间,“X”表示陷阱,不可走。每走一步需要1s,左上角(0,0)是起点,右下角(n-1,m-1)是终点。要求从起点走到终点的最短时间,并且输出每一秒的动作(两点间的移动或是在某一点打怪)。

这道题的代码我从昨天改到今天,中间发现了一堆奇奇怪怪的错误。

一开始是被题目上面的Special Judge”所迷惑,也没有好好看题,就以为只要在多种路线中输出一种就行,所以就用了DFS,然后华丽丽的TLE了,改成BFS之后再交,WA。。重新看题才发现是要求最短时间。

为了求最少时间,给map[i][j]多定义了一个参数ans来记录从起点走到点(i,j)所用的最少时间,初始化为最大值1e9-1。从now的四个不同方向走到next,每走一步都比较一下是否能够减小next的ans。还把visit数组删掉了,因为每个方向都要走一遍,肯定不是每个点只走一次,所以visit数组就没有意义了。

改完之后样例过了(样例一直都能过。。。),就特别开心地交了,还是WA。。。就觉得自己的代码没有问题了,就去网上搜各种其他样例,当然也看了别人的代码,发现好多都是用的优先队列,原谅本渣渣还没有接触过优先队列,又固执地认为自己的算法肯定没有错,就只是试各种数据,终于发现一组数据过不了。错误是,在BFS里,当走到终点时,我就直接return了,所以这个代码实质上还是只记录了一种路线,只是在过程中注意了最少时间,结果依旧不是最少的。因此,改代码,走到终点时不退出BFS,比较记录的走到终点所用时间跟新走法的时间,然后再继续走其他方向,直到把所有可能情况走完。

改完交,又WA,我都要绝望了,从头开始看代码,发现记录路径的数组a[]、b[]开的太小,只开了100,n和m的范围是100,路径长度肯定不止100呀,于是索性改成10000。终于AC了。

我的路径记录是用两个数组记录每一步的横、纵座标,给map[i][j]定义了一个参数fa来在BFS中记录是从哪个点走到点(i,j),在输出前倒序记录到数组a[]、b[]中。看到网上的其他好多代码都是用的递归输出,我吧,以前爱用递归,觉得代码简洁,写起来也简单,但是经历了几次爆栈之后,就不怎么敢用了。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

struct pos
{
    int x;
    int y;
    int step;
};

struct maps
{
    char data;
    int ans;
    pos fa;
};

int n,m,cnt;
maps map[105][105];
int a[10005],b[10005];
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
pos q[1000005];
//bool vis[105][105];

void bfs(int x, int y)
{
    int f = 0,r = 0;
    pos now,next;
    q[r].x = x;
    q[r].y = y;
    q[r++].step = 0;
    map[x][y].ans = 0;
    //vis[x][y] = true;
    while(f != r)
    {
        now = q[f++];
        if(now.x == n - 1 && now.y == m - 1 && map[n - 1][m - 1].ans > now.step)
        {
            map[n - 1][m - 1].ans = now.step;
        }
        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m )
            {
                if(map[next.x][next.y].data == '.' && map[next.x][next.y].ans > now.step + 1)
                {
                    //vis[next.x][next.y] = true;
                    next.step = now.step + 1;
                    map[next.x][next.y].ans = next.step;
                    map[next.x][next.y].fa = now;
                    q[r++] = next;
                }
                else if(map[next.x][next.y].data >= '1' && map[next.x][next.y].data <= '9' && map[next.x][next.y].ans > now.step + map[next.x][next.y].data - '0' + 1)
                {
                    //vis[next.x][next.y] = true;
                    next.step = now.step + map[next.x][next.y].data - '0' + 1;
                    map[next.x][next.y].ans = next.step;
                    map[next.x][next.y].fa = now;
                    q[r++] = next;
                }
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m) != EOF)
    {
        cnt = 1;
        //memset(vis,false,sizeof(vis));
        for(int i = 0; i < n; i++)
        {
            getchar();
            for(int j = 0; j < m; j++)
            {
                scanf("%c",&map[i][j].data);
                map[i][j].ans = 999999999;
            }
        }
        map[0][0].ans = 0;
        bfs(0,0);
        if(map[n-1][m-1].ans == 999999999)
            printf("God please help our poor hero.\n");
        else
        {
            a[map[n-1][m-1].ans] = n-1;
            b[map[n-1][m-1].ans] = m-1;
            for(int i = map[n-1][m-1].ans; i>0; i--)
            {
                a[i-1] = map[a[i]][b[i]].fa.x;
                b[i-1] = map[a[i]][b[i]].fa.y;
            }
            printf("It takes %d seconds to reach the target position, let me show you the way.\n",map[n-1][m-1].ans);
            for(int i = 1; i <= map[n-1][m-1].ans; i++)
            {
                if(a[i] != 0 || b[i] != 0)
                {
                    printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,a[i-1],b[i-1],a[i],b[i]);
                    if(map[a[i]][b[i]].data >= '1' && map[a[i]][b[i]].data <= '9')
                    {
                        for(int j = 0; j < map[a[i]][b[i]].data - '0'; j++)
                        {
                            printf("%ds:FIGHT AT (%d,%d)\n",cnt++,a[i],b[i]);
                        }
                    }
                }
            }
        }
        printf("FINISH\n");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章