迷宫 DFS 连通性模型 java

该篇文章介绍了一种使用DFS(深度优先搜索)解决连通性模型的问题,具体场景是判断在给定的二维迷宫地图中,两个点是否联通。程序接收迷宫地图和起始、结束点坐标,通过DFS遍历并标记已访问格子,判断从起点到终点是否存在路径。
摘要由CSDN通过智能技术生成

🍑 算法题解专栏


🍑 saber --> 搜索 --> DFS之连通性模型 --> 迷宫

在这里插入图片描述

输入

2
3
.##
..#
#..
0 0 2 2
5
.....
###.#
..#..
###..
...#.
0 0 4 0

输出

YES
NO

import java.util.*;

class Main
{
	static int N = 110;
	static int n, ha, la, hb, lb;
	static boolean[][] st = new boolean[N][N];
	static char[][] g = new char[N][N];
	static int[] dx = { -1, 1, 0, 0 };
	static int[] dy = { 0, 0, -1, 1 };

	static boolean dfs(int x, int y)
	{
		if (x == hb && y == lb)// 递归出口
			return true;
		if (g[x][y] == '#')// 一进来就得判断是否为墙,因为起点可能就是墙
			return false;
		st[x][y] = true;// 标记已遍历
		for (int i = 0; i < 4; i++)// 枚举四个方向
		{
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (xx < 0 || xx >= n || yy < 0 || yy >= n)// 非法越界,跳过
				continue;
			if (st[xx][yy])// 已遍历,跳过
				continue;
			if (g[xx][yy] == '#')// 走不通,跳过
				continue;
			if (dfs(xx, yy))
				return true;
		}
		return false;
	}

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while (T-- > 0)
		{
			for (int i = 0; i < N; i++)
				Arrays.fill(st[i], false);
			n = sc.nextInt();
			for (int i = 0; i < n; i++)
			{
				String s = sc.next();
				for (int j = 0; j < n; j++)
				{
					g[i][j] = s.charAt(j);
				}
			}
			ha = sc.nextInt();
			la = sc.nextInt();
			hb = sc.nextInt();
			lb = sc.nextInt();
			if (dfs(ha, la))
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
}
好的,下面是一个使用深度优先搜索(DFS)解决迷宫问题的Java代码示例: ```java import java.util.Stack; public class MazeSolver { private int[][] maze; private boolean[][] visited; private int startRow, startCol; private int endRow, endCol; public MazeSolver(int[][] maze, int startRow, int startCol, int endRow, int endCol) { this.maze = maze; this.visited = new boolean[maze.length][maze[0].length]; this.startRow = startRow; this.startCol = startCol; this.endRow = endRow; this.endCol = endCol; } public boolean solve() { Stack<Integer> stack = new Stack<>(); stack.push(startRow); stack.push(startCol); while (!stack.isEmpty()) { int col = stack.pop(); int row = stack.pop(); if (row < 0 || col < 0 || row >= maze.length || col >= maze[0].length) { continue; } if (visited[row][col] || maze[row][col] == 1) { continue; } visited[row][col] = true; if (row == endRow && col == endCol) { return true; } stack.push(row + 1); stack.push(col); stack.push(row - 1); stack.push(col); stack.push(row); stack.push(col + 1); stack.push(row); stack.push(col - 1); } return false; } public static void main(String[] args) { int[][] maze = { {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0} }; MazeSolver solver = new MazeSolver(maze, 0, 0, 3, 3); if (solver.solve()) { System.out.println("Maze solved!"); } else { System.out.println("Maze cannot be solved!"); } } } ``` 这个示例中,我们使用了一个栈来实现DFS算法,每次从栈中弹出一个坐标,然后检查是否越界、已经访问过或者是墙壁,如果是,则忽略这个坐标,否则标记为已访问并将其四周的坐标入栈。如果最终栈为空,则说明无法从起点到达终点,否则,说明找到了一条能够从起点到达终点的路径。 这只是一个简单的示例,实际上,如果要解决更复杂的迷宫问题,需要使用更高级的算法来优化搜索过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值