Java項目實現(xiàn)尋找迷宮出路
本文實例為大家分享了Java實現(xiàn)尋找迷宮出路的具體代碼,供大家參考,具體內(nèi)容如下
項目名稱
尋找迷宮出路
項目描述
給定一個自定義迷宮,0表示能通過,1表示不能通過。通過程序找出正確的迷宮出路,并將正確的路線改為2輸出。
代碼實現(xiàn)
測試類
public class Test { public static void main(String[] args) { Maze maze = new Maze(); maze.begin(); } }
主類:實現(xiàn)主方法
public class Maze { private MazeNode[][] mazeNodes; private int row; private int col; private Stack<MazeNode> stack = new Stack<>(); private static Scanner scanner = new Scanner(System.in); public Maze(){ System.out.println("請輸入行列數(shù)"); row = scanner.nextInt(); col = scanner.nextInt(); mazeNodes = new MazeNode[row][col]; } public void initValue(){ System.out.println("輸入迷宮路徑:"); for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ // i j mazeNodes[i][j] = new MazeNode(scanner.nextInt(),i,j); } } } //2. 根據(jù)當前節(jié)點的四個方向上面的value值 // 初始化當前節(jié)點的四個方向行走狀態(tài) public void initWayState(){ for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ //i j if(mazeNodes[i][j].getValue()==0){ //東 :看東邊節(jié)點的值是0 if(j+1<col && mazeNodes[i][j+1].getValue() == 0){ // 將當前節(jié)點i j 的東邊方向設(shè)置成可走狀態(tài) mazeNodes[i][j].setWayState(Constant.WAY_EAST,Constant.WAY_ABLE); } //西 if(j-1>0 && mazeNodes[i][j-1].getValue() == 0){ mazeNodes[i][j].setWayState(Constant.WAY_WEST,Constant.WAY_ABLE); } //南 if(i+1<row && mazeNodes[i+1][j].getValue() == 0){ mazeNodes[i][j].setWayState(Constant.WAY_SOUTH,Constant.WAY_ABLE); } //北 if(i-1>0 && mazeNodes[i-1][j].getValue() == 0){ mazeNodes[i][j].setWayState(Constant.WAY_NORTH,Constant.WAY_ABLE); } } } } } //走迷宮 public void goMaze(){ if(mazeNodes[0][0].getValue()!=0){ System.out.println("沒有迷宮路徑"); return; } stack.push(mazeNodes[0][0]); while (!stack.isEmpty()) {//TODO:?????? MazeNode top = stack.peek(); //獲取當前棧頂元素在二維數(shù)組中的行列坐標 int i = top.getI(); int j = top.getJ(); if(i == row-1 && j==col-1){ System.out.println("找到迷宮路徑"); return; } //TODO: if(mazeNodes[i][j].getWayState(Constant.WAY_SOUTH) == Constant.WAY_DISABLE && mazeNodes[i][j].getWayState(Constant.WAY_EAST) == Constant.WAY_DISABLE && mazeNodes[i][j].getWayState(Constant.WAY_NORTH) == Constant.WAY_DISABLE && mazeNodes[i][j].getWayState(Constant.WAY_WEST) == Constant.WAY_DISABLE){ stack.pop(); } //東 else if (mazeNodes[i][j].getWayState(Constant.WAY_EAST)) { stack.push(mazeNodes[i][j + 1]); mazeNodes[i][j+1].setWayState(Constant.WAY_WEST,Constant.WAY_DISABLE); mazeNodes[i][j].setWayState(Constant.WAY_EAST,Constant.WAY_DISABLE); }//南 else if (mazeNodes[i][j].getWayState(Constant.WAY_SOUTH)) { //如果南邊方向可走,將南邊節(jié)點進行入棧操作 stack.push(mazeNodes[i + 1][j]); //封路1:將南邊節(jié)點的回路(北邊)方向封掉 mazeNodes[i+1][j].setWayState(Constant.WAY_NORTH,Constant.WAY_DISABLE); //封路2:將當前節(jié)點i,j 走過的路封掉 TODO: mazeNodes[i][j].setWayState(Constant.WAY_SOUTH,Constant.WAY_DISABLE); } //西 else if (mazeNodes[i][j].getWayState(Constant.WAY_WEST)) { stack.push(mazeNodes[i][j - 1]); mazeNodes[i][j-1].setWayState(Constant.WAY_EAST,Constant.WAY_DISABLE); mazeNodes[i][j].setWayState(Constant.WAY_WEST,Constant.WAY_DISABLE); } //北 else if (mazeNodes[i][j].getWayState(Constant.WAY_NORTH)) { stack.push(mazeNodes[i - 1][j]); mazeNodes[i-1][j].setWayState(Constant.WAY_SOUTH,Constant.WAY_DISABLE); mazeNodes[i][j].setWayState(Constant.WAY_NORTH,Constant.WAY_DISABLE); } // if(mazeNodes[i][j].getWayState(Constant.WAY_SOUTH) == Constant.WAY_DISABLE && // mazeNodes[i][j].getWayState(Constant.WAY_EAST) == Constant.WAY_DISABLE && // mazeNodes[i][j].getWayState(Constant.WAY_NORTH) == Constant.WAY_DISABLE && // mazeNodes[i][j].getWayState(Constant.WAY_WEST) == Constant.WAY_DISABLE){ // stack.pop(); // } } } public void finish(){ while (!stack.isEmpty()) { MazeNode top = stack.peek(); top.setValue(2); stack.pop(); } System.out.println("迷宮路徑為:"); int i = 0, j = 0; while (i<row){ for (j = 0; j < col; j++) { System.out.print(mazeNodes[i][j].getValue()+" "); } System.out.println(); i++; } } public void begin(){ initValue(); initWayState(); goMaze(); finish(); } // public void show(){ // for(int i=0;i<row;i++){ // for(int j=0;j<colum;j++){ // System.out.print(mazeNodes[i][j].value+" "); // } // System.out.println(); // } // } }
MazeNode:結(jié)點類,用于迷宮結(jié)點
public class MazeNode { private int i; private int j; private int value; private boolean[] wayState; public MazeNode(int value,int i,int j){ wayState = new boolean[Constant.WAYNUM]; this.value = value; this.i = i; this.j = j; } public int getValue() { return value; } public void setWayState(int direction,boolean isAble) { wayState[direction] = isAble; } public boolean getWayState(int direction) { return wayState[direction]; } public void setValue(int value){ this.value = value; } public int getI() { return i; } public int getJ() { return j; } }
Constant:常數(shù)類,方便使用
public class Constant { public static final int WAYNUM = 4; public static final int WAY_EAST = 0; public static final int WAY_WEST = 1; public static final int WAY_SOUTH = 2; public static final int WAY_NORTH = 3; public static final boolean WAY_ABLE = true; public static final boolean WAY_DISABLE = false; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java.sql.SQLException問題解決以及注意事項
這篇文章主要給大家介紹了關(guān)于java.sql.SQLException問題解決以及注意事項的相關(guān)資料,這個問題其實很好解決,文中通過圖文將解決的辦法介紹的很詳細,需要的朋友可以參考下2023-07-07SpringCloud超詳細講解微服務(wù)網(wǎng)關(guān)Gateway
這篇文章主要介紹了SpringCloud Gateway微服務(wù)網(wǎng)關(guān),負載均衡,熔斷和限流,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07SpringSecurity實現(xiàn)動態(tài)url攔截(基于rbac模型)
本文主要介紹了SpringSecurity動態(tài)url攔截,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08LeetCode -- Path Sum III分析及實現(xiàn)方法
這篇文章主要介紹了LeetCode -- Path Sum III分析及實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問題
這篇文章主要介紹了Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Kafka消費客戶端協(xié)調(diào)器GroupCoordinator詳解
這篇文章主要為大家介紹了Kafka消費客戶端協(xié)調(diào)器GroupCoordinator使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10springboot中使用mybatisplus自帶插件實現(xiàn)分頁的示例代碼
這篇文章主要介紹了springboot中使用mybatisplus自帶插件實現(xiàn)分頁,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09詳解java接口(interface)在不同JDK版本中的變化
這篇文章主要介紹了詳解java接口(interface)在不同JDK版本中的變化,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02