HDU - 1026 - Ignatius and the Princess I(BFS)Java实现

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

说明一下,我发现网上的所有算法题目几乎都是C和C++实现(虽然也能大致读懂,因为我自学了一点C),但Java作为本人的本命语言,我就希望网上能有Java实现的版本。因为本人水平有限,如有不当之处,欢迎指出,大家互相学习。

题目大意为英雄需要从第一个点走到最后一个点,遇到'.'可通过,'X'为陷阱,数字为对应的停留时间,因为要求最短时间,所以可以用广度优先搜索实现,但是要把所有的路遍历完,因为路径最短不代表时间最短(中途有怪物),用step数组记住当前结点所花的时间,不要用visited数组!因为此层结点所花的时间可能比上层结点少(但是步数一定多)!所以说step数组不一定是不变的,如果后面来的结点花的时间少,当前格的step就要被重新赋值。

打印路径是一个难点,我是用一个三维数组path[N][M][2],path[][][0]保存横坐标,path[][][1]保存纵坐标,path[][][2]保存打怪花的时间,每次step被赋值,就用path保存前一步的结点坐标。然后从目标结点一直遍历到起始结点,并保存在另一个数组中,最后打印出来即可。

下面是AC的代码:

import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;

public class Main {
    static int[][][] node;
    static int[][] dn = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while(input.hasNext()) {
            int N = input.nextInt();
            int M = input.nextInt();
            char[][] a = new char[N][M];
            for(int i=0; i<N; i++)
                a[i]=input.next().toCharArray();
            node = new int[N][M][3];
            Queue<Integer> queue = new ArrayBlockingQueue<>(N*M);
            int[][] step = new int[N][M];
            step[0][0]=0;
            queue.add(0);
            queue.add(0);
            int x, y, m, n, s;            
            while(!queue.isEmpty()) {
                x=queue.poll();
                y=queue.poll();
                for(int i=0; i<4; i++) {
                    m=x+dn[i][0];
                    n=y+dn[i][1];
                    if(m>=0&&m<N&&n>=0&&n<M&&a[m][n]!='X') {
                        if(a[m][n]=='.') {
                            if(step[m][n]==0||step[x][y]+1<step[m][n]) {
                                queue.add(m);
                                queue.add(n);
                                step[m][n]=step[x][y]+1;
                                node[m][n][0]=x;
                                node[m][n][1]=y;
                            }                            
                        }
                        else {
                            s = (int)(a[m][n]-47);
                            if(step[m][n]==0||step[x][y]+s<step[m][n]) {
                                queue.add(m);
                                queue.add(n);
                                step[m][n]=step[x][y]+s;
                                node[m][n][0]=x;
                                node[m][n][1]=y;
                                node[m][n][2]=s-1;
                            }
                        }
                        if(m==N-1&&n==M-1) break;                        
                    }
                }                
            }            
            int length = step[N-1][M-1];
            if(length!=0) {
                System.out.println("It takes "+length+" seconds to reach the target position, let me show you the way.");
                int[][] path = new int[length+1][2];
                int f=N-1, g=M-1, count=length, x1, y1, x2, y2;
                path[count][0]=f;
                path[count--][1]=g;
                while(f!=0||g!=0) {
                        path[count][0]=node[f][g][0];
                        path[count][1]=node[f][g][1];    
                        f=path[count][0];
                        g=path[count][1];
                        count--;
                }    
                count++;
                int cou=0;
                while(count<length) {    
                    x1=path[count][0];
                    y1=path[count][1];
                    x2=path[count+1][0];
                    y2=path[count+1][1];
                    if(node[x1][y1][2]!=0) {                                                                    
                        for(int j=0; j<node[x1][y1][2]; j++)
                            System.out.println(++cou+"s:FIGHT AT ("+x1+","+y1+")");
                    }    
                    System.out.println(++cou+"s:("+x1+","+y1+")->("+x2+","+y2+")");                    
                    count++;
                }
                if(node[N-1][M-1][2]!=0) {    
                    for(int j=0; j<node[N-1][M-1][2]; j++) {
                        System.out.println(++cou+"s:FIGHT AT ("+--N+","+--M+")");
                        N+=1;
                        M+=1;
                    }
                }
            }
            else
                System.out.println("God please help our poor hero.");
            System.out.println("FINISH");
        }
        input.close();    
    }
    
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值