A M × N square maze,0 means road while 1 means wall, find a way from entrance to exit,
this maze which base on two-dimensional array has 4 directions(right,left,up,down)
by the way, the maze start with 1 ,not 0,
for example
the point(1,1) is the entrance and (8,9) is the exit;
the code is
package com.xuf.d;
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;
}
public class Maze{
int maze[][];
private int row = 9;
private int col = 8;
Stack<Position> stack;
boolean p[][] = null;
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("please put in the rows of this maze ");
row = scanner.nextInt();
System.out.println("please put in the columns of this maze ");
col = scanner.nextInt();
System.out.println("please put in" + row + "rows " + col + "columns maze");
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("no way");
} else {
System.out.println("there exists a way");
System.out.println("the way is :");
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();
}
}
public static void main(String[] args){
Maze demo = new Maze();
demo.init();
demo.findPath();
}
}
when it runs
we should put in rows ,colunms and the array
then we can get the resutl
for example (no way)
for example (this exists a way)