java迷宫算法

9 篇文章 0 订阅

本章使用栈来实现迷宫算法

基本思路为:

   对入口节点进行标记

   入口节点入栈

  while(栈不空){

  查看栈顶元素;

   if(栈顶元素为出口节点){

       循环输出栈中元素

   }

  else{

        if(查看该节点的下方节点是否符合条件){

               对下方节点标记;

               下方节点入栈;

                continue;

        } 

         if(查看该节点的右方节点是否符合条件){

               对右方节点进行标记;

               右方节点入栈;

            continue;

           } 

     

  if(查看该节点的上方节点是否符合条件){

               对上方节点进行标记;

               上方节点入栈;

            continue;

           } 

  if(查看该节点的左方节点是否符合条件){

               对左方节点进行标记;

               左方节点入栈;

            continue;

           } 

   都不符合条件,当前节点出栈;


   }

}

定义节点类:

class Point{
	int x;
	int y;
	int color=0;
	public Point(int x,int y){
		this.x = x;
		this.y = y;	
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Point other = (Point) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "Point [x=" + x + ", y=" + y + "]";
	}
	public int getColor() {
		return color;
	}
	public Point setColor(int color) {
		this.color = color;
		return this;
	}
	
	
}
主程序:


public static void main(String[] args) {
		Stack<Point> s = new Stack<Point>();
		
		Point[][] roat=new Point[][]{
				{new Point(0,0),new Point(0,1).setColor(1),new Point(0,2),new Point(0,3),new Point(0,4)},
				{new Point(1,0),new Point(1,1).setColor(1),new Point(1,2),new Point(1,3).setColor(1),new Point(1,4)},
				{new Point(2,0),new Point(2,1),new Point(2,2),new Point(2,3).setColor(1),new Point(2,4)},
				{new Point(3,0).setColor(1),new Point(3,1).setColor(1),new Point(3,2).setColor(1),new Point(3,3).setColor(1),new Point(3,4)},
				{new Point(4,0).setColor(1),new Point(4,1).setColor(1),new Point(4,2).setColor(1),new Point(4,3).setColor(1),new Point(4,4)}
				
		};
		
		Point p = new  Point(0,0);
		p.color =2;
		Point p1 = new Point(4,4);
		findRoat(s,p,p1,roat);

	}
	
	public static void  findRoat(Stack<Point> s,Point rukou,Point chukou,Point[][] roat){
		Stack<Point> out = new Stack<Point>();
		int xx=roat[0].length;
		int yy=roat.length;
		rukou.color = 2;
		s.push(rukou);
		while(!s.isEmpty()){
			System.out.println("s.isEmpty()");
			Point p = s.peek();
			if(p.equals(chukou)){
				while(!s.isEmpty()){
					//System.out.println(s.pop());
					out.push(s.pop());
				}
				while(!out.isEmpty()){
					System.out.println(out.pop());
					
				}
			}
			else{
				if(( p.x+1<xx )&&( roat[p.x+1][p.y].color==0)){
					roat[p.x+1][p.y].color = 2;//标记走过
					s.push(roat[p.x+1][p.y]);
					System.out.println("1:"+(p.x+1)+","+(p.y));
					continue;
				}
				if(p.y+1<yy  && (roat[p.x][p.y+1].color == 0)){
					roat[p.x][p.y+1].color = 2;
					s.push(roat[p.x][p.y+1]);
					System.out.println("2:"+(p.x)+","+(p.y+1));
					continue;
				}
				if(p.x -1 >=0 && roat[p.x-1][p.y].color==0){
					roat[p.x-1][p.y].color = 2;
					s.push(roat[p.x-1][p.y]);
					System.out.println("3:"+(p.x-1)+","+(p.y));
					continue;
				}
				if(p.y-1 >= 0 && roat[p.x][p.y-1].color == 0){
					roat[p.x][p.y-1].color =2;
					s.push(roat[p.x][p.y-1]);
					System.out.println("4:"+(p.x)+","+(p.y-1));
					continue;
				}
				s.pop();
				System.out.println("s.pop()");
			}
			
		}
	}
	
	public static void print(Stack<Point> s){
		if(!s.isEmpty()){
			print(s);
			System.out.println(s.pop());
		}
		
	}

     

           y轴

        ————————

       |  0   1   0   0   0    |

x轴  |  0   1   0   1   0    |

       |  0   0   0   1   0    |

       |  1   1   1   1   0    |

       |  1   1   1   1   0    |

       ————————— 

输出结果

Point [x=0, y=0]
Point [x=1, y=0]
Point [x=2, y=0]
Point [x=2, y=1]
Point [x=2, y=2]
Point [x=1, y=2]
Point [x=0, y=2]
Point [x=0, y=3]
Point [x=0, y=4]
Point [x=1, y=4]
Point [x=2, y=4]
Point [x=3, y=4]
Point [x=4, y=4]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成迷宫算法有很多种,其中比较常见的是深度优先搜索(DFS)和广度优先搜索(BFS)。 下面是使用DFS算法生成迷宫Java代码示例: ```java import java.util.*; public class MazeGenerator { private int width; private int height; private int[][] maze; private boolean[][] visited; public MazeGenerator(int width, int height) { this.width = width; this.height = height; this.maze = new int[2 * width + 1][2 * height + 1]; this.visited = new boolean[width][height]; } public void generate() { // 初始化迷宫,所有格子都是墙壁 for (int i = 0; i < 2 * width + 1; i++) { for (int j = 0; j < 2 * height + 1; j++) { maze[i][j] = 1; } } // 从起点开始生成迷宫 dfs(0, 0); // 将迷宫的外围设置为墙壁 for (int i = 0; i < 2 * width + 1; i++) { maze[i][0] = maze[i][2 * height] = 1; } for (int i = 0; i < 2 * height + 1; i++) { maze[0][i] = maze[2 * width][i] = 1; } } public int[][] getMaze() { return maze; } private void dfs(int x, int y) { visited[x][y] = true; // 随机打乱四个方向的顺序 List<Integer> directions = Arrays.asList(0, 1, 2, 3); Collections.shuffle(directions); for (int direction : directions) { int dx = 0, dy = 0; switch (direction) { case 0: // 上 if (y == 0 || visited[x][y - 1]) { continue; } dy = -1; break; case 1: // 右 if (x == width - 1 || visited[x + 1][y]) { continue; } dx = 1; break; case 2: // 下 if (y == height - 1 || visited[x][y + 1]) { continue; } dy = 1; break; case 3: // 左 if (x == 0 || visited[x - 1][y]) { continue; } dx = -1; break; } // 将当前格子和相邻格子之间的墙壁打通 maze[2 * x + 1 + dx][2 * y + 1 + dy] = 0; dfs(x + dx, y + dy); } } } ``` 使用方法: ```java MazeGenerator generator = new MazeGenerator(10, 10); generator.generate(); int[][] maze = generator.getMaze(); ``` 其中,`width`和`height`表示迷宫的宽度和高度,`maze`是一个二维数组,表示生成的迷宫,其中`0`表示通路,`1`表示墙壁。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值