第十届蓝桥杯 JavaA 迷宫

第十届蓝桥杯 JavaA 迷宫


在这里插入图片描述
在这里插入图片描述


法一:
思路:
bfs,path记录路径。
/**
 * 1.编程https://www.cnblogs.com/woxiaosade/p/10592061.html
 * 2.观察https://www.cnblogs.com/yzm10/p/10595165.html
 * 
 * @description TODO
 * @author frontier
 * @time 2019年4月9日 上午11:19:05
 *
 */
class Node {
	int step;
	String path;
	int x, y;

	public Node(int step, String path, int x, int y) {
		super();
		this.step = step;
		this.path = path;
		this.x = x;
		this.y = y;
	}
}

public class D迷宫 {
	static Scanner in = new Scanner(System.in);
	static int n, m;
	static char[][] maze = new char[60][60];
	static boolean[][] vis = new boolean[60][60];
	static int[] X = { 1, 0, 0, -1 };// 增量数组,方向向量,直接按字典序排列
	static int[] Y = { 0, -1, 1, 0 };
	static String[] d = { "D", "L", "R", "U" };

	public static void main(String[] args) throws FileNotFoundException {
		Scanner in = new Scanner(new File("src/JavaA/s10/5.txt"));
		n = in.nextInt();
		m = in.nextInt();
		for (int i = 1; i <= n; ++i) {
			String temp = in.next();
			for (int j = 1; j <= m; ++j) {
				maze[i][j] = temp.charAt(j - 1);
			}
		}
		bfs();
	}

	static boolean check(int x, int y) {
		if (x < 1 || x > n || y < 1 || y > m)
			return false;
		if (vis[x][y] || maze[x][y] == '1')
			return false;
		return true;
	}

	static void bfs() {
		Queue<Node> q = new LinkedList<Node>();
		q.offer(new Node(0, "", 1, 1));
		while (!q.isEmpty()) {
			Node top = q.poll();
			vis[top.x][top.y] = true;
			if (top.x == n && top.y == m) {
				System.out.println(top.step + " " + top.path);
				return;
			}
			for (int i = 0; i < 4; ++i) {
				int newX = top.x + X[i];
				int newY = top.y + Y[i];
				if (check(newX, newY)) {
					Node temp = new Node(top.step + 1, top.path + d[i], newX, newY);
					q.offer(temp);
				}
			}
		}
	}
}

法二:
思路:
肉眼走迷宫。
参考:

肉眼走迷宫

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值