do{
if(当前位置可以通过){
入栈当前位置;
判断当前是否到终点;
未到终点,选择一个方向上的下个位置成为当前位置;
}
else{
出栈;判断是否四个方向都遍历完;
是则当前位置不可行,标记不通;再出栈栈顶元素新一轮判断;
没有选择另一方向继续;
}
}while(栈不为空)
主要代码如下:
//type内保存当前位置的坐标和下一位置的方向
class type{
int row;
int col;
int di=0;
}
public boolean searchpath(){
type current = new type();
current.row = start_r;
current.col = start_c;
do{
if(ispass(current))//可以通过(没有到达过的)
{
stack.push(current);
if(check(current)){return true;}//是否到达终点
markcanpass(current);//标记已到达过
current = nexttype(current,1);//从左边开始出发
}
else{//不能通过,换个方向
if(!stack.isEmpty()){
current =stack.pop();
//如果四个方向已经遍历完,且栈不空,
//标记此路不通,从栈顶再取出一个元素进行新一轮判断
while(current.di==4&&!stack.isEmpty()){
marknotpass(current);
current = stack.pop();
}
//还有方向为遍历
if(current.di<4){
current.di++;
stack.push(current);
current = nexttype(current,current.di);
}
}
}
}while(!stack.isEmpty());
return false;
}
结果截图:
代码:
import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;
public class 迷宫栈 {
private int row,col,start_r,start_c,end_r,end_c;
private int[][] maze;
private Stack<type> stack;
class type{
int row;
int col;
int di=0;
}
public 迷宫栈(){
stack = new Stack<type>();
}
public boolean ispass(type cur){
if(maze[cur.row][cur.col]==1){
return true;
}else if(maze[cur.row][cur.col]==3){
return true;
}else{
return false;
}
}
public type nexttype(type cur,int di){
cur.di=di;
type newtype = new type();
newtype.row=cur.row;
newtype.col=cur.col;
switch(di){
case 1 :{
newtype.col--;
break;
}
case 2 :{
newtype.row--;
break;
}
case 3 :{
newtype.col++;
break;
}
case 4 :{
newtype.row++;
break;
}
}
return newtype;
}
public void markcanpass(type cur){
maze[cur.row][cur.col]=2;
}
public void marknotpass(type cur){
maze[cur.row][cur.col]=0;
}
public boolean check(type cur){
return maze[cur.row][cur.col]==3;
}
public void show(){
if(searchpath()){
Iterator<type> s=stack.iterator();
while(s.hasNext()){
type pr = s.next();
System.out.println("("+pr.row+","+pr.col+")-->");
}
for (int i = 0; i <row+2; i++) {
for(int y =0;y<col+2;y++){
System.out.print(maze[i][y]);
}
System.out.println();
}
}else{
System.out.println("没有到终点的通路");
}
}
public boolean searchpath(){
type current = new type();
current.row = start_r;
current.col = start_c;
do{
if(ispass(current))//可以通过(没有到达过的)
{
stack.push(current);
if(check(current)){return true;}//是否到达终点
markcanpass(current);//标记已到达过
current = nexttype(current,1);//从左边开始出发
}
else{//不能通过,换个方向
if(!stack.isEmpty()){
current =stack.pop();
//如果四个方向已经遍历完,且栈不空,
//标记此路不通,从栈顶再取出一个元素进行新一轮判断
while(current.di==4&&!stack.isEmpty()){
marknotpass(current);
current = stack.pop();
}
//还有方向为遍历
if(current.di<4){
current.di++;
stack.push(current);
current = nexttype(current,current.di);
}
}
}
}while(!stack.isEmpty());
return false;
}
public void run(){
Scanner sc = new Scanner(System.in);
System.out.println("输入迷宫行数");
row = sc.nextInt();
System.out.println("输入迷宫列数");
col = sc.nextInt();
System.out.println("输入迷宫路径");
//1为通路2为起点3为终点
maze = new int[row+2][col+2];
for (int i = 1; i <=row; i++) {
for(int y =1;y<=col;y++){
maze[i][y]=sc.nextInt();
}
}
System.out.println("输入起点");
start_r = sc.nextInt();
start_c = sc.nextInt();
System.out.println("输入终点");
end_r = sc.nextInt();
end_c = sc.nextInt();
maze[end_r][end_c] = 3;
for (int i = 0; i <row+2; i++) {
for(int y =0;y<col+2;y++){
System.out.print(maze[i][y]);
}
System.out.println();
}
}
public static void main(String[] args) {
迷宫栈 lala = new 迷宫栈();
lala.run();
lala.show();
}
}