奇特的迷宫

奇特的迷宫

Time limit:1 Seconds Memory limit:32768K

问题描述:

        如图(a)所示的15行、15列的迷宫(相当于n=8),迷宫中每一个位置可能为S(表示起始位置)、D(表示目标位置)、1-9的数字,且S和D各只有一个。对于1-9的数字n,表示能从当前位置出发,可以沿上、下、左、右方向走n步(多一步,少一步都不行);图(b)表示的是,数字2表示可以沿上、下、左、右方向走两步到达的位置。从S出发,可以沿着上、下、左、右走一步。现在要求从S到D的最少步数。

        

输入描述:

        输入文件中包含多个测试数据。每个测试数据的第一行为一个整数n,2<=n且n<=10,表示迷宫的大小为2n-1行、2n-1列。接下来有2n-1行,为每行个位置上的数字(或为S、D),第1行有一个字符,第2行有2个字符,...,第n行有n个字符,第n+1行有n-1个字符,...,第2n-1行有1个字符。输入文件中最后一行为0,表示测试数据结束。


输出描述:

        对输入文件中的每个测试数据,如果能从S走到D,则输出最少步数;否则(即从S走不到D),则输出0。

样例输入:

        4

        1

        23

        1S4

        1323

        21D

        23

        2

        0

样例输出:

        2


 代码实现:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


class Pos{
    private int x;
    private int y;
    private int value;
    private boolean visited;
    private int step;
    public int getX() {
        return x;
    }


    public void setX(int x) {
        this.x = x;
    }


    public int getY() {
        return y;
    }


    public void setY(int y) {
        this.y = y;
    }


    public int getValue() {
        return value;
    }


    public void setValue(int value) {
        this.value = value;
    }


    public boolean isVisited() {
        return visited;
    }


    public int getStep() {
        return step;
    }


    public void setStep(int step) {
        this.step = step;
    }


    public void setVisited(boolean visited) {
        this.visited = visited;
    }


    public Pos(int x,int y,int value,int step){
        this.x = x;
        this.y = y;
        this.value = value;
        this.visited = false;
        this.step = step;
    }
}
public class SpecialLabyrinth {
    private static Queue<Pos> queue = new LinkedList<Pos>();
    private static int n;
    private static char[][] characters;
    private static Scanner in = new Scanner(System.in);
    private static ArrayList<Pos> posList = new ArrayList<>();
    private static boolean flag = false;
    public static Pos getPosition(int i,int j){
        for(Pos p : posList){
            if(p.getX() == i && p.getY()==j){
                return p;
            }
        }
        return null;
    }


    public static void init(){
        String[] matrix = new String[(2*n)-1];
        characters = new char[(2*n)-1][];
        for(int i = 0;i<matrix.length;i++){
            matrix[i] = in.nextLine();
            characters[i] = new char[matrix[i].length()];
            characters[i] = matrix[i].toCharArray();
        }
        startFind();
    }


    public static void startFind(){
        int posSx = 0;
        int posSy = 0;
        int posDx = 0;
        int posDy = 0;
        for(int i = 0;i<characters.length;i++){
            for(int j = 0;j<characters[i].length;j++){
                if(characters[i][j] == 'S'){
                    Pos p = new Pos(i,j,1,0);
                    posSx = i;
                    posSy = j;
                    posList.add(p);
                }else if(characters[i][j] == 'D'){
                    Pos p = new Pos(i,j,-1,0);
                    posDx = i;
                    posDy = j;
                    posList.add(p);
                }else{
                    int value = Integer.parseInt(String.valueOf(characters[i][j]));
                    Pos p = new Pos(i,j,value,0);
                    posList.add(p);
                }
            }
        }


        Pos S = getPosition(posSx,posSy);
        queue.add(S);
        S.setVisited(true);
        while(!queue.isEmpty()){
            Pos first = queue.poll();
            //向左
            if(first.getY() - first.getValue() >= 0){
                int new_x = first.getX() ;
                int new_y = first.getY() - first.getValue();
                if(new_x == posDx && new_y == posDy){
                    flag = true;
                    System.out.println(first.getStep() + 1);
                    queue.clear();
                    break;
                }else {
                    if (!getPosition(new_x, new_y).isVisited()){
                        Pos p = getPosition(new_x,new_y);
                        p.setStep(first.getStep() + 1);
                        p.setVisited(true);
                        queue.add(p);
                    }
                }
            }


            //向右
            if(first.getY() + first.getValue() < characters[first.getX()].length){
                int new_x = first.getX();
                int new_y = first.getY() + first.getValue();
                if(new_x == posDx && new_y == posDy){
                    flag = true;
                    System.out.println(first.getStep() + 1);
                    queue.clear();
                    break;
                }else {
                    if (!getPosition(new_x, new_y).isVisited()){
                        Pos p = getPosition(new_x,new_y);
                        p.setStep(first.getStep() + 1);
                        p.setVisited(true);
                        queue.add(p);
                    }
                }
            }




            //向上
            if(first.getX() - 2*first.getValue() >= 0){
                int current_x = first.getX();
                int current_y = first.getY();
                for(int i = 0;i<first.getValue();i++){
                    if(current_x <= (n-1)){
                        current_x = current_x - 2;
                        current_y = current_y - 1;
                    }else if(current_x > n){
                        current_x = current_x - 2;
                        current_y = current_y + 1;
                    }else if(current_x == n){
                        current_x = current_x - 2;
                    }
                }


                if(current_y < characters[current_x].length && current_y >= 0){
                    if(current_x == posDx && current_y == posDy){
                        System.out.println(first.getStep() + 1);
                        flag = true;
                        queue.clear();
                        break;
                    }else {
                        if (!getPosition(current_x,current_y).isVisited()){
                            Pos p = getPosition(current_x,current_y);
                            p.setStep(first.getStep() + 1);
                            p.setVisited(true);
                            queue.add(p);
                        }
                    }
                }
            }


            //向下
            if(first.getX() + 2*first.getValue() < (2*n-1)){
                int current_x = first.getX();
                int current_y = first.getY();
                for(int i = 0;i<first.getValue();i++){
                    if(current_x < (n-2)){
                        current_x = current_x + 2;
                        current_y = current_y + 1;
                    }else if(current_x >= (n-1)){
                        current_x = current_x + 2;
                        current_y = current_y - 1;
                    }else if(current_x == (n-2)){
                        current_x = current_x + 2;
                    }
                }
                if(current_y < characters[current_x].length && current_y >= 0) {
                    if (current_x == posDx && current_y == posDy) {
                        System.out.println(first.getStep() + 1);
                        queue.clear();
                        flag = true;
                        break;
                    } else {
                        if (!getPosition(current_x, current_y).isVisited()) {
                            Pos p = getPosition(current_x, current_y);
                            p.setStep(first.getStep() + 1);
                            p.setVisited(true);
                            queue.add(p);
                        }
                    }
                }
            }
        }
        if(flag != true){
            System.out.println(0);
        }
    }
    public static void main(String[] args) {
        while(true){
            queue.clear();
            posList.clear();
            flag = false;
            n = Integer.parseInt(in.nextLine());
            if(n==0){
                break;
            }
            init();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值