走迷宮回溯演算法(Java實現)
阿新 • • 發佈:2019-02-04
以一個M×N的長方陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設計一個程式,對任意設定的迷宮,求出一條從入口到出口的通路,或得出沒有通路的結論。
(1) 根據二維陣列,輸出迷宮的圖形。
(2) 探索迷宮的四個方向:RIGHT為向右,DOWN向下,LEFT向左,UP向上,輸出從入口到出口的行走路徑。
例子:
左上角(1,1)為入口,右下角(8,9)為出口。
可使用回溯方法,即從入口出發,順著某一個方向進行探索,若能走通,則繼續往前進;否則沿著原路退回,換一個方向繼續探索,直至出口位置,求得一條通路。假如所有可能的通路都探索到而未能到達出口,則所設定的迷宮沒有通路。
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];
}
/*
* 構造迷宮
*/
public void init(){
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入迷宮的行數");
row = scanner.nextInt();
System.out.println("請輸入迷宮的列數");
col = scanner.nextInt();
System.out .println("請輸入" + 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;
}
}
// 將原始迷宮複製到新的迷宮中
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j) {
temp[i + 1][j + 1] = maze[i][j];
}
}
// 從左上角開始按照順時針開始查詢
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("沒有路徑");
} 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();
}
}
執行示例:
請輸入迷宮的行數
3
請輸入迷宮的列數
3
請輸入3行3列的迷宮
0 1 1
0 0 1
1 0 0
有路徑
路徑如下:
請輸入迷宮的行數
9
請輸入迷宮的列數
8
請輸入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
有路徑
路徑如下: