Java項(xiàng)目實(shí)現(xiàn)尋找迷宮出路
本文實(shí)例為大家分享了Java實(shí)現(xiàn)尋找迷宮出路的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目名稱
尋找迷宮出路
項(xiàng)目描述
給定一個(gè)自定義迷宮,0表示能通過(guò),1表示不能通過(guò)。通過(guò)程序找出正確的迷宮出路,并將正確的路線改為2輸出。

代碼實(shí)現(xiàn)
測(cè)試類
public class Test {
public static void main(String[] args) {
Maze maze = new Maze();
maze.begin();
}
}
主類:實(shí)現(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("請(qǐng)輸入行列數(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ù)當(dāng)前節(jié)點(diǎn)的四個(gè)方向上面的value值
// 初始化當(dāng)前節(jié)點(diǎn)的四個(gè)方向行走狀態(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é)點(diǎn)的值是0
if(j+1<col && mazeNodes[i][j+1].getValue() == 0){
// 將當(dāng)前節(jié)點(diǎn)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("沒(méi)有迷宮路徑");
return;
}
stack.push(mazeNodes[0][0]);
while (!stack.isEmpty()) {//TODO:??????
MazeNode top = stack.peek();
//獲取當(dāng)前棧頂元素在二維數(shù)組中的行列坐標(biāo)
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é)點(diǎn)進(jìn)行入棧操作
stack.push(mazeNodes[i + 1][j]);
//封路1:將南邊節(jié)點(diǎn)的回路(北邊)方向封掉
mazeNodes[i+1][j].setWayState(Constant.WAY_NORTH,Constant.WAY_DISABLE);
//封路2:將當(dāng)前節(jié)點(diǎn)i,j 走過(guò)的路封掉 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é)點(diǎn)類,用于迷宮結(jié)點(diǎn)
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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java.sql.SQLException問(wèn)題解決以及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于java.sql.SQLException問(wèn)題解決以及注意事項(xiàng)的相關(guān)資料,這個(gè)問(wèn)題其實(shí)很好解決,文中通過(guò)圖文將解決的辦法介紹的很詳細(xì),需要的朋友可以參考下2023-07-07
SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Gateway
這篇文章主要介紹了SpringCloud Gateway微服務(wù)網(wǎng)關(guān),負(fù)載均衡,熔斷和限流,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)url攔截(基于rbac模型)
本文主要介紹了SpringSecurity動(dòng)態(tài)url攔截,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
LeetCode -- Path Sum III分析及實(shí)現(xiàn)方法
這篇文章主要介紹了LeetCode -- Path Sum III分析及實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10
Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問(wèn)題
這篇文章主要介紹了Druid連接池未關(guān)閉導(dǎo)致內(nèi)存泄漏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Kafka消費(fèi)客戶端協(xié)調(diào)器GroupCoordinator詳解
這篇文章主要為大家介紹了Kafka消費(fèi)客戶端協(xié)調(diào)器GroupCoordinator使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁(yè)的示例代碼
這篇文章主要介紹了springboot中使用mybatisplus自帶插件實(shí)現(xiàn)分頁(yè),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
詳解java接口(interface)在不同JDK版本中的變化
這篇文章主要介紹了詳解java接口(interface)在不同JDK版本中的變化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

