BFS解迷宫java例子

public class MiGong {
	public static void main(String[] args) {
		int[][] array = {
				{0, 0, 0, 0,1,1,0,0},
				{0, 1, 0, 0,0,0,0,0},
				{0, 1, 0, 0,1,1,0,0},
				{0, 1, 0, 0,1,1,0,0}
		};
		new MiGong().findMinRold(array);
	}

	//定义图的节点信息
	class Node {
		//定义坐标和距离第一个节点的距离
		int x;
		int y;
		int dis;
		//定义节点的前缀,用于绘制整个最短路径的  线路图
		Node pre;

		public Node(int x, int y, int dis, Node pre) {
			this.x = x;
			this.y = y;
			this.dis = dis;
			this.pre = pre;
		}
	}

	public void findMinRold(int[][] arr) {
		//定义  上下左右四个方向
		int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
		//创建队列
		LinkedList<Node> queue = new LinkedList<Node>();
		//创建开始节点
		Node start = new Node(0, 0, 0, null);
		//把开始节点放入队列中,并做下标记,在原来数组中做标记
		queue.offer(start);
		arr[0][0] = 1;//修改位置,标识已经遍历设置为1
		//循环操作队列,进行广度遍历
		Node temp = null;
		ok:
		while (!queue.isEmpty()) {
			temp = queue.poll();//移除头部元素
			//依次遍历这个节点的四个方向,查找还没有遍历的相连节点
			for (int i = 0; i < 4; i++) {
				int newX = temp.x + direction[i][0];//标识x坐标移动 direction[i][0]
				int newY = temp.y + direction[i][1];//标识x坐标移动 direction[i][1]
				//此时表示  arr[temp.x][temp.y ]移动到了arr[newX][newY]
				//是否越界,坐标
				if (newX < 0 || newX >= 4 || newY < 0 || newY >= 8) {
					continue;
				}
				//判断是否该节点可以通过,即为0
				if (arr[newX][newY] == 1) {
					continue;
				}
				//构造节点
				Node next = new Node(newX, newY, temp.dis + 1, temp);
				//该节点可以通过,判断该节点是否是最终节点

				if (newX == 3 && newY == 7) {
					queue.clear();
					//相当于头插法,转置
					queue.offerFirst(next);
					Node preNode = next.pre;
					while (preNode != null) {
						queue.offerFirst(preNode);
						preNode = preNode.pre;
					}
					int len = queue.size();
					System.out.println("最短路径长度为:" + (len - 1));
					for (Node it : queue) {
						System.out.println("(" + it.x + "," + it.y + ") ");
					}
					break ok;
				}
				arr[newX][newY] = 1;
				queue.offer(next);//队尾插入
			}//for

		}//while

	}

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值