Java實(shí)現(xiàn)走迷宮回溯算法
以一個(gè)M×N的長(zhǎng)方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設(shè)計(jì)一個(gè)程序,對(duì)任意設(shè)定的迷宮,求出一條從入口到出口的通路,或得出沒(méi)有通路的結(jié)論。
(1) 根據(jù)二維數(shù)組,輸出迷宮的圖形。
(2) 探索迷宮的四個(gè)方向:RIGHT為向右,DOWN向下,LEFT向左,UP向上,輸出從入口到出口的行走路徑。
例子:
左上角(1,1)為入口,右下角(8,9)為出口。

可使用回溯方法,即從入口出發(fā),順著某一個(gè)方向進(jìn)行探索,若能走通,則繼續(xù)往前進(jìn);否則沿著原路退回,換一個(gè)方向繼續(xù)探索,直至出口位置,求得一條通路。假如所有可能的通路都探索到而未能到達(dá)出口,則所設(shè)定的迷宮沒(méi)有通路。
import java.util.*;
class Position{
public Position(){
}
public Position(int row, int col){
this.col = col;
this.row = row;
}
public String toString(){
return "(" + row + " ," + col + ")";
}
int row;
int col;
}
class Maze{
public Maze(){
maze = new int[15][15];
stack = new Stack<Position>();
p = new boolean[15][15];
}
/*
* 構(gòu)造迷宮
*/
public void init(){
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入迷宮的行數(shù)");
row = scanner.nextInt();
System.out.println("請(qǐng)輸入迷宮的列數(shù)");
col = scanner.nextInt();
System.out.println("請(qǐng)輸入" + row + "行" + col + "列的迷宮");
int temp = 0;
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j) {
temp = scanner.nextInt();
maze[i][j] = temp;
p[i][j] = false;
}
}
}
/*
* 回溯迷宮,查看是否有出路
*/
public void findPath(){
// 給原始迷宮的周圍家一圈圍墻
int temp[][] = new int[row + 2][col + 2];
for(int i = 0; i < row + 2; ++i) {
for(int j = 0; j < col + 2; ++j) {
temp[0][j] = 1;
temp[row + 1][j] = 1;
temp[i][0] = temp[i][col + 1] = 1;
}
}
// 將原始迷宮復(fù)制到新的迷宮中
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j) {
temp[i + 1][j + 1] = maze[i][j];
}
}
// 從左上角開始按照順時(shí)針開始查詢
int i = 1;
int j = 1;
p[i][j] = true;
stack.push(new Position(i, j));
while (!stack.empty() && (!(i == (row) && (j == col)))) {
if ((temp[i][j + 1] == 0) && (p[i][j + 1] == false)) {
p[i][j + 1] = true;
stack.push(new Position(i, j + 1));
j++;
} else if ((temp[i + 1][j] == 0) && (p[i + 1][j] == false)) {
p[i + 1][j] = true;
stack.push(new Position(i + 1, j));
i++;
} else if ((temp[i][j - 1] == 0) && (p[i][j - 1] == false)) {
p[i][j - 1] = true;
stack.push(new Position(i, j - 1));
j--;
} else if ((temp[i - 1][j] == 0) && (p[i - 1][j] == false)) {
p[i - 1][j] = true;
stack.push(new Position(i - 1, j));
i--;
} else {
stack.pop();
if(stack.empty()){
break;
}
i = stack.peek().row;
j = stack.peek().col;
}
}
Stack<Position> newPos = new Stack<Position>();
if (stack.empty()) {
System.out.println("沒(méi)有路徑");
} else {
System.out.println("有路徑");
System.out.println("路徑如下:");
while (!stack.empty()) {
Position pos = new Position();
pos = stack.pop();
newPos.push(pos);
}
}
/*
* 圖形化輸出路徑
* */
String resault[][]=new String[row+1][col+1];
for(int k=0;k<row;++k){
for(int t=0;t<col;++t){
resault[k][t]=(maze[k][t])+"";
}
}
while (!newPos.empty()) {
Position p1=newPos.pop();
resault[p1.row-1][p1.col-1]="#";
}
for(int k=0;k<row;++k){
for(int t=0;t<col;++t){
System.out.print(resault[k][t]+"\t");
}
System.out.println();
}
}
int maze[][];
private int row = 9;
private int col = 8;
Stack<Position> stack;
boolean p[][] = null;
}
class hello{
public static void main(String[] args){
Maze demo = new Maze();
demo.init();
demo.findPath();
}
}
運(yùn)行示例:
請(qǐng)輸入迷宮的行數(shù)
3
請(qǐng)輸入迷宮的列數(shù)
3
請(qǐng)輸入3行3列的迷宮
0 1 1
0 0 1
1 0 0
有路徑
路徑如下:

請(qǐng)輸入迷宮的行數(shù)
9
請(qǐng)輸入迷宮的列數(shù)
8
請(qǐng)輸入9行8列的迷宮
0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0
0 0 1 0 1 1 0 1
0 1 1 1 0 0 1 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 1
0 1 1 1 1 0 0 1
1 1 0 0 0 1 0 1
1 1 0 0 0 0 0 0
有路徑
路徑如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springsecurity Oauth2如何設(shè)置token的過(guò)期時(shí)間
如果用戶在指定的時(shí)間內(nèi)有操作就給token延長(zhǎng)有限期,否則到期后自動(dòng)過(guò)期,如何設(shè)置token的過(guò)期時(shí)間,本文就來(lái)詳細(xì)的介紹一下2021-08-08
解決MyEclipse出現(xiàn)the user operation is waiting的問(wèn)題
今天做項(xiàng)目的時(shí)候每次修改代碼保存后都會(huì)跳出一個(gè)框框,然后就有兩個(gè)進(jìn)度條,上面寫the user operation is wating...小編去網(wǎng)上查了查解決了這個(gè)問(wèn)題,下面跟大家分享一下。2018-04-04
Java 如何使用@Autowired注解自動(dòng)注入bean
這篇文章主要介紹了Java 使用@Autowired注解自動(dòng)注入bean的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java并發(fā)編程中實(shí)現(xiàn)可見性的四種可行方案解析
這篇文章主要介紹了java并發(fā)編程中實(shí)現(xiàn)可見性的四種可行方案解析,使用關(guān)鍵字volatile和使用鎖(如synchronized關(guān)鍵字或者java.util.concurrent包中的鎖)來(lái)確保對(duì)共享變量的修改在多線程環(huán)境中能夠正確地被其他線程所觀察到,需要的朋友可以參考下2023-08-08
參數(shù)校驗(yàn)Spring的@Valid注解用法解析
這篇文章主要介紹了參數(shù)校驗(yàn)Spring的@Valid注解用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
java 分轉(zhuǎn)元與元轉(zhuǎn)分實(shí)現(xiàn)操作
這篇文章主要介紹了java 分轉(zhuǎn)元與元轉(zhuǎn)分實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
SpringBoot集成redis與session實(shí)現(xiàn)分布式單點(diǎn)登錄
這篇文章主要介紹了SpringBoot集成redis與session實(shí)現(xiàn)分布式單點(diǎn)登錄,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09

