java使用链栈实现迷宫求解

java实现链栈在前面有所介绍:http://www.cnblogs.com/lixiaolun/p/4644141.html

 

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

  

上一篇:JavaWeb学习之路(21)–HTML之表格


下一篇:18.1 集群介绍;18.2 keepalived介绍;18.3,18.4,18.5 用keepalived配置高可用集群(上,中,下);