迷宫 java实验


public class TestThree {

	static int mazeSize = 12;
	static char maze[][] = new char [mazeSize][mazeSize];  //保存迷宫地图信息
	
	public static void main(String[] args) {
		
		maze();
	}
	
	static void maze()
	{
		//先随机生成迷宫矩阵(mazeSize X mazeSize),{0,1}矩阵,其中1代表通路,0代表墙,并显示出来。
		//设置起点和终点,为了简单化,将第一个生成的'1'点设为起点,最后生成的'0'点设为终点
		//记录路程,1234分别代表上下左右。
		boolean startingOk = false;
		int startingX = 0, startingY = 0, endingX = 0, endingY = 0; //起点和终点的坐标
		
		//生成以及显示迷宫
		System.out.println("随机生成的迷宫地图为:");
		System.out.print("   ");
		for(int i = 0; i<12; i++)
		{
			System.out.print(i+" ");
		}
		System.out.println();
		System.out.print("  ╔");
		for(int i = 0; i<24; i++)
		{
			System.out.print("═");
		}
		System.out.println("╗");
		for(int i = 0; i<12; i++)
		{
			System.out.printf("%2d",i);
			System.out.print("║");
			for(int j = 0; j<12; j++)
			{
				if( ((float) Math.random()) >= 0.4)
				{
					maze[i][j] = '1';
					if(startingOk == false)  //设置起点
					{
						startingX = i;
						startingY = j;
						startingOk = true;
						System.out.print("♀ ");
						continue;
					}
					endingX = i;  //设置终点
					endingY = j;
					System.out.print("  ");
				}
				else
				{
					maze[i][j] = '0';
					System.out.print("██");
				}
			}
			System.out.print("║");
			System.out.println();
			
		}
		System.out.print("  ╚");
		for(int i = 0; i<24; i++)
		{
			System.out.print("═");
		}
		System.out.println("╝");
		
		System.out.println("随机生成的起点坐标是:"+startingX+" "+startingY);
		System.out.println("随机生成的终点坐标是:"+endingX+" "+endingY);
		
		//回溯法找路,看是不是有解。
		if( findWay(startingX,startingY,endingX,endingY,startingX,startingY) == false)
		{
			System.out.println("此迷宫没有出路!!");
		}
	}
	
	static boolean findWay(int x,int y,int endingX,int endingY,int startingX,int startingY )
	{
		boolean doFindWay = false;
		if( x == endingX && y == endingY)
		{
			showWay(startingX,startingY,endingX,endingY);
			return true;
		}
		if( x+1 < mazeSize && maze[x+1][y] == '1') //向下走
		{
			maze[x][y] = '↓';
			doFindWay = findWay(x+1,y,endingX,endingY,startingX,startingY);
			maze[x][y] = '1';
			if(doFindWay == true)
			{
				return true;
			}
		}
		if( y+1 < mazeSize && maze[x][y+1] == '1')  //向右走
		{
			maze[x][y] = '→';
			doFindWay = findWay(x,y+1,endingX,endingY,startingX,startingY);
			maze[x][y] = '1';
			if(doFindWay == true)
			{
				return true;
			}
		}
		if(x-1 >=0 && maze[x-1][y] == '1')  //向右走
		{
			maze[x][y] = '↑';
			doFindWay = findWay(x-1,y,endingX,endingY,startingX,startingY);
			maze[x][y] = '1';
			if(doFindWay == true)
			{
				return true;
			}
		}
		if( y-1 >=0  && maze[x][y-1] == '1')  //向右走
		{
			maze[x][y] = '←';
			doFindWay = findWay(x,y-1,endingX,endingY,startingX,startingY);
			maze[x][y] = '1';
			if(doFindWay == true)
			{
				return true;
			}
		}
		return false;
	}
	
	static void showWay(int startingX, int startingY,int endingX,int endingY)
	{
		System.out.print("   ");
		for(int i = 0; i<12; i++)
		{
			System.out.print(i+" ");
		}
		System.out.println();
		System.out.print("  ╔");
		for(int i = 0; i<24; i++)
		{
			System.out.print("═");
		}
		System.out.println("╗");
		for(int i = 0; i<12; i++)
		{
			System.out.printf("%2d",i);
			System.out.print("║");
			for(int j = 0; j<12; j++)
			{
				if(i == startingX && j == startingY)
				{
					System.out.print("♀ ");
					continue;
				}
				
				if(i == endingX && j == endingY)
				{
					System.out.print("OK");
					continue;
				}
				
				if(maze[i][j] == '1')
				{
					System.out.print("  ");
				}
				else if(maze[i][j] == '0')
				{
					System.out.print("██");
				}
				else
				{
					System.out.print(maze[i][j]);
					System.out.print(maze[i][j]);
					//System.out.print("☆");
				}
			}
			System.out.print("║");
			System.out.println();
			
		}
		System.out.print("  ╚");
		for(int i = 0; i<24; i++)
		{
			System.out.print("═");
		}
		System.out.println("╝");
	}
}


你可能感兴趣的:(愚蠢的本科:))