迷宮問題(java實現)

來源:互聯網
上載者:User

標籤:java   程式   gety   位置   out   direction   實現   main   ack   

1、

public class Direction {    private int x;    private int y;    public Direction(int x, int y) {        this.x = x;        this.y = y;    }    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;    }}

2、

public class Position {    private int x;    private int y;    private int d;    public int getX() {        return this.x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return this.y;    }    public void setY(int y) {        this.y = y;    }    public Position(int x, int y, int d) {        this.x = x;        this.y = y;        this.d = d;    }    public int getD() {        return this.d;    }    public void setD(int d) {        this.d = d;    }}

3、

import java.util.Iterator;import java.util.Stack;public class MazeProblem {    public static Stack<Position> path(int[][] maze, Direction[] move) {        Stack<Position> s = new Stack<Position>();        // 起點位置 還未開始探索所以無方向        Position start_p = new Position(1, 1, -1);        s.push(start_p);        maze[1][1] = -1; // 起點位置表示已經走過,不可以往回探索,pop的時候 可以退回        while (!s.empty()) {            Position temp = s.pop(); // 取出當前的位置 準備進行向下探索            // 確定探索的位置方向            int x = temp.getX(); // 當前處在正在探索的位置和方向            int y = temp.getY();            int d = temp.getD() + 1;// 探索的方向            while (d < 8) { // 開始探索 一共八個方向                int i = x + move[d].getX(); // 根據某個方向探的下一個位置                int j = y + move[d].getY();                if (maze[i][j] == 0) { // 如果下一個位置是可以進去的,則放入當前位置                    s.push(new Position(x, y, d));                    x = i; // 把當前探索位置 調整為放入的位置                    y = j;                    d = 0; // 調整方向為0,為下次探索做準備                    maze[x][y] = -1; // 然後設定為已走過                    if (x == Destination.m && y == Destination.n) { // 在判斷是不是已經是終點位置 如果是 則程式退出                        s.push(new Position(x, y, d));                        return s;                    }                } else {                    d++;  //// 如果下一個位置是不可以進去的,則放入調整方向                }            }        }        return new Stack<Position>();    }    //終點位置    static class Destination {        static int m = 6;        static int n = 8;    }    public static void main(String[] arg) {        // 0表示可進入 1 表示 不可以進去 -1 表示走過的路勁也不可進入        // 每個位置 都有八個方向        int[][] maze = {            //    0  1  2  3  4  5  6  7  8  9                { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 0                { 1, 0, 1, 1, 1, 0, 1, 1, 1, 1 }, // 1                { 1, 1, 0, 1, 0, 1, 1, 1, 1, 1 }, // 2                { 1, 0, 1, 0, 0, 0, 0, 0, 0, 1 }, // 3                { 1, 0, 1, 1, 1, 1, 1, 0, 1, 1 }, // 4                { 1, 1, 0, 0, 1, 1, 1, 1, 0, 1 }, // 5                { 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, // 6                { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }  // 7        };        Direction[] move =         {            new Direction(0,  1),            new Direction(1,  1),             new Direction(1,  0),            new Direction(1, -1),            new Direction(0, -1),            new Direction(-1,-1),             new Direction(-1, 0),            new Direction(-1, 1)        };        Stack<Position> s = MazeProblem.path(maze, move);        Iterator<Position> it = s.iterator();        while (it.hasNext()) {            Position e = it.next();            System.out.println(e.getX() + ",-->" + e.getY());        }    }}

 

迷宮問題(java實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.