迷宫栈

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();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值