JAVA编程基础——迷宫找路径问题

JAVA编程基础——迷宫找路径问题

问题描述:
给一个迷宫和一个入口,找出所有走出迷宫的路线并输出路线

分析:
1、可以用一个二维数组来表现迷宫地图;1表示墙,0表示路
2、用一个栈来存储走过的路线,走一步存一个
3、每走到一个格子就要遍历除了自己过来的那个方向的其他三个方向,判断,有路就往前走,遇墙就换方向,这样遍历到第四个就说明遇到死胡同,原路返回
4、设置一个边界判断,表示如果出界的话那么就说明走出迷宫,当然起点除外
5、判断走出迷宫后将栈倒出来,输出路线,并把当前出口变成墙,在重回起点重新找另一条路。。。

迷宫类

//迷宫类

public class Maze 
{
	private int map[][]=null;
	//private int way[]=null;
	
	public Maze()        //构造默认地图
	{
		int maps[][]={{1,0,1,1,1},{1,0,1,1,1},{1,0,0,0,0},{0,0,1,0,1},{1,1,1,0,1}};
		this.map=maps;
/*		for(int i=0;i<map.length;i++)
			for(int j=0;j<map[0].length;j++)
				System.out.print("  "+map[i][j]);*/
	}
	
	public void findWay()
	{
		//创建一个栈用来存路线
		Stack<Integer> stack=new Stack<Integer>();
		stack.push(-1);
		stack.push(-1);
		//先找一个入口,我们假设就在第一行
		int row=0,col=0;        //当前位置
		for(int j=0;j<5;j++)
		{
			if(map[0][j]==0)
			{
				col=j;
			}
		}
		
/*		System.out.print(row);
		System.out.print(col);*/
		
		int stop=1;
		//sign  1==上,2==右,3==下 ,4==左
		int sign=2; 
		//记录起始位置
		int rerow=row;          
		int recol=col;
		stack.push(rerow);
		stack.push(recol);
		
		//行动代码
		while(stop==1)
		{
//向上走^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
			while(sign==1) 
			{
				if(row-1==-1 && row==rerow && col==recol)
				{
					stop=0;
					break;
				}

				//胜利条件,前方路径已超出迷宫范围
				if(row-1==-1)
				{
					System.out.println("111  "+stop);
					Stack<Integer> ostack=new Stack<Integer>();    //new一个栈把路径倒出来
					while(stack.getTop()>1) ostack.push(stack.pop());
					while(!ostack.isEmpty())
					{
						System.out.print("("+ostack.pop()+","+ostack.pop()+")"+"  ");
					}
					System.out.println();
					map[row][col]=1;   //找到一条路径后,封路,重找
					//恢复起始位置
					row=rerow;
					col=recol;
					stack.push(rerow);
					stack.push(recol);
					sign=2;
					break;
				}
				//向上走,走不通就往右搜索;
				if(map[row-1][col]==1) 
				{
					sign=2;
					break;
				}
				//走得通
				if(map[row-1][col]==0)
				{
					int forcol=stack.pop();
					int forrow=stack.pop();
					
					int pacol=stack.pop();
					int parow=stack.pop();
					
					stack.push(parow);
					stack.push(pacol);
					
					if(row-1!=parow || col!=pacol)
					{
						stack.push(forrow);
						stack.push(forcol);
						
						row=row-1;
						stack.push(row);
						stack.push(col);
					}
					else row=row-1;
					
					sign=4;
					break;
				}
			}
			
//向右走^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
			while(sign==2)
			{
				if(col+1==map[0].length && row==rerow && col==recol)
				{
					stop=0;
					break;
				}
				if(col+1==map[0].length)
				{
					Stack<Integer> ostack=new Stack<Integer>();
					while(stack.getTop()>1) ostack.push(stack.pop());
					while(!ostack.isEmpty())
					{
						System.out.print("("+ostack.pop()+","+ostack.pop()+")"+"  ");
					}
					System.out.println();
					map[row][col]=1;
					row=rerow;
					col=recol;
					stack.push(rerow);
					stack.push(recol);
					sign=2;
					break;
				}
				
				if(map[row][col+1]==1)
				{
					sign=3;
					break;
				}
				
				if(map[row][col+1]==0)
				{
					int forcol=stack.pop();
					int forrow=stack.pop();
					
					int pacol=stack.pop();
					int parow=stack.pop();
					
					stack.push(parow);
					stack.push(pacol);
					
					if(row!=parow || col+1!=pacol)
					{
						stack.push(forrow);
						stack.push(forcol);
						
						col=col+1;
						stack.push(row);
						stack.push(col);
					}
					else col=col+1;
					
					sign=1;
					break;
				}
			}

//向下走^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
			while(sign==3)
			{
				if(row+1==map.length && row==rerow && col==recol)
				{
					stop=0;
					break;
				}
				
				if(row+1==map.length)
				{
					Stack<Integer> ostack=new Stack<Integer>();
					while(stack.getTop()>1) ostack.push(stack.pop());
					while(!ostack.isEmpty())
					{
						System.out.print("("+ostack.pop()+","+ostack.pop()+")"+"  ");
					}
					System.out.println();
					map[row][col]=1;
					row=rerow;
					col=recol;
					stack.push(rerow);
					stack.push(recol);
					sign=2;
					break;
				}
				
				if(map[row+1][col]==1)
				{
					sign=4;
					break;
				}

				if(map[row+1][col]==0)
				{
					int forcol=stack.pop();
					int forrow=stack.pop();
					
					int pacol=stack.pop();
					int parow=stack.pop();
					
					stack.push(parow);
					stack.push(pacol);
					
					if(row+1!=parow || col!=pacol)
					{
						stack.push(forrow);
						stack.push(forcol);
						
						row=row+1;
						stack.push(row);
						stack.push(col);
					}
					
					else row=row+1;

					
					sign=2;
					break;
				}
			}
			
//向左走^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
			while(sign==4)
			{
				if(col-1==-1 && row==rerow && col==recol)
				{
					stop=0;
					break;
				}
				if(col-1==-1)
				{
					Stack<Integer> ostack=new Stack<Integer>();
					while(stack.getTop()>1) ostack.push(stack.pop());
					while(!ostack.isEmpty())
					{
						System.out.print("("+ostack.pop()+","+ostack.pop()+")"+"  ");
					}
					System.out.println();
					map[row][col]=1;
					row=rerow;
					col=recol;
					stack.push(rerow);
					stack.push(recol);
					sign=2;
					break;
				}
				if(map[row][col-1]==1)
				{
					sign=1;
					break;
				}
				if(map[row][col-1]==0)
				{
					int forcol=stack.pop();
					int forrow=stack.pop();
					
					int pacol=stack.pop();
					int parow=stack.pop();
					
					stack.push(parow);
					stack.push(pacol);
					
					if(row!=parow || col-1!=pacol)
					{
						stack.push(forrow);
						stack.push(forcol);
						
						col=col-1;
						stack.push(row);
						stack.push(col);
					}
					else col=col-1;
					
					sign=3;
					break;
				}
			}
		}

	}
	
}


主函数


public class APPMaze {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//初始化迷宫
		Maze maze=new Maze();
		maze.findWay();

	}
}


@SuppressWarnings("unused")
public class Stack<T>
{
	private T data[];
	private int top;
	
	@SuppressWarnings("unchecked")
	public Stack()
	{
		this.data=(T[]) new Object[10];
		this.top=-1;
	}
	
	@SuppressWarnings("unchecked")
	public Stack(int size)
	{
		this.data=(T[]) new Object[size];
		this.top=-1;
	}
	
	@SuppressWarnings("unchecked")
	public boolean push(T root)
	{
		if(this.top==this.data.length-1) 
		{
			Object[] t=new Object[this.data.length*2];
			for(int i=0;i<this.data.length;i++) t[i]=(int) this.data[i];
			this.data=(T[]) t;
		}
		this.top++;
		this.data[top]=(T) root;
		return true;
	}
	
	public T pop()
	{
		if(this.top==-1)   return null;
		T num=this.data[top];
		this.top--;
		return  num;
	}
	
	public boolean isEmpty()
	{
		return this.top==-1;
	}
	
	public boolean isFull()
	{
		return true;
	}
	
	public int getTop()
	{
		return top;
	}
	
}

运行结果:
(0,1) (1,1) (2,1) (2,2) (2,3) (2,4)
(0,1) (1,1) (2,1) (2,2) (2,3) (3,3) (4,3)
(0,1) (1,1) (2,1) (3,1) (3,0)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值