java栈 迷宫_java使用链栈实现迷宫求解

java实现链栈的代码:

package stackapplication;

public class LinkStack {

private Element base;

private Element top;

class Element

{

public Step data;

public Element next;

}

/**

* 初始化栈

* */

public void initStack()

{

top = new Element();

base = new Element();

top.data=null;

top.next=base;

base.data=null;

base.next=null;

}

/**

* 入栈

* */

public void push(Step o)

{

Element e = new Element();

e.data = o;

if(top.next==base)//第一次入栈操作

{

e.next=base;

top.next=e;

}else

{

e.next=top.next;

top.next=e;

}

}

/**

* 出栈

* */

public Step pop()

{

Step o = null;

if(top.next==base)

{

System.out.println("栈中没有元素!");

return o;

}else

{

o = top.next.data;

//System.out.println("出栈操作"+o);

top.next=top.next.next;

}

return o;

}

/**

* 判断栈是否为空

* */

public Boolean isEmpty()

{

if(top.next==base)

{

return true;

}

return false;

}

/**

* 打印栈

* */

public void print()

{

System.out.print("打印栈:");

Element temp =top;

while(temp.next!=base)

{

System.out.print(temp.next.data+"\t");

temp =temp.next;

}

System.out.println();

}

}

java实现迷宫求解的类代码:

package stackapplication;

public class Maze {

public static void main(String[] args) {

int [][]map={

{1,1,1,1,1,1,1,1,1,1},

{1,0,0,1,0,0,0,1,0,1},

{1,0,0,1,0,0,0,1,0,1},

{1,0,0,0,0,1,1,0,0,1},

{1,0,1,1,1,0,0,0,0,1},

{1,0,0,0,1,0,0,0,0,1},

{1,0,1,0,0,0,1,0,0,1},

{1,0,1,1,1,0,1,1,0,1},

{1,1,0,0,0,0,0,0,0,1},

{1,1,1,1,1,1,1,1,1,1}

};//入口在map[1][1],出口在map[8][8]

int [][]move={{0,-1},{0,1},{-1,0},{1,0}};//上下左右四个移动方向

LinkStack s = new LinkStack();

s.initStack();

LinkStack s1 = new LinkStack();

s1.initStack();

path(map,move,s,s1);

while(!s1.isEmpty())

{

Step step = s1.pop();

System.out.println("("+step.x+","+step.y+")");

}

}

private static int path(int[][] map, int[][] move,LinkStack s,LinkStack s1) {

Step step = new Step(1, 1, -1);//起始位置

//map[1][1]=-1;//表示已走过该点

s.push(step);

s1.push(step);

while(!s.isEmpty())

{

step=s.pop();

int x=step.x;

int y=step.y;

int d=step.d+1;

while(d<4)

{

int i=x+move[d][0];

int j=y+move[d][1];

if(map[i][j]==0 && i>=0 && i<10 && j>=0 &&j<10)//该位置是通的,且不越界

{

System.out.println(i+","+j);

step = new Step(x, y, d);

s.push(step);//将当前位置压入栈顶

s1.push(step);

step = new Step(i, j, d);

s.push(step);//将当前位置压入栈顶

s1.push(step);

x=i;

y=j;

map[x][y]=-1;//表示已走过该点

if(x==8 && y==8)//到达出口

{

System.out.println("到达出口");

return 1;

}else

{

d=0;//到达一个新的点,所以要从新初始化方向,遍历其4个方向是否是通的

}

}else

{

d++;//下一个方向

}

}

}

return 0;

}

}

class Step

{

int x;//横坐标

int y;//纵坐标

int d;//移动方向,取值为0,1,2,3。分别表示上下左右4个方向。

public Step(int x,int y,int d)

{

this.x=x;

this.y=y;

this.d=d;

}

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值