迷宫问题 Java

迷宫问题

OJ链接:迷宫问题

题目:

Description
定义一个二维数组:

int maze[5][5] = {

	0, 1, 0, 0, 0,

	0, 1, 0, 1, 0,

	0, 0, 0, 0, 0,

	0, 1, 1, 1, 0,

	0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output
左上角到右下角的最短路径,格式如样例所示。

样例输入输出:

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

题目分析:

1.首先因为求最短一般我们采用BFS
2.因为要输出路径,所以我们肯定要有一个队列或者别的数据结构来存储路径
3.因为只需要输出最短路径,我们要记录最短的路径,所以节点必须设置前后节点的连接 或者使用倒推的方式
4.模板套路

题目代码:

package javaBase;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

import java.util.Scanner;
import java.util.Stack;

class Node {
	int row;  //当前的行列
	int column;
	int father_x; //父节点
	int father_y;
	int length;  //长度

	public Node(int row, int column,int father_x, int father_y,  int length) {
		super();
		this.row = row;
		this.column = column;
		this.father_x= father_x;
		this.father_y = father_y;
		this.length = length;
	}
	public Node() {}

}

public class Main{
	private static int data[][] = new int[5][5];
	private static LinkedList<Node> stack = new LinkedList<Node>();
	private static int next[][] = { { 0, 1 }, { 1, 0 } ,{0,-1},{-1,0}};// 4个方向

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		for(int i=0;i<5;i++) {
			String ss = s.nextLine();
			String [] temp = ss.split(" ");
			for(int j=0;j<5;j++) {
				data[i][j]=Integer.parseInt(temp[j]);
			}
		}
		bfs(4, 4);
		Node node = stack.poll();
		int a=node.row;
		int b=node.column;
		System.out.println("("+node.row+","+node.column+")");
		
		while(!stack.isEmpty()) {
			Node node3 = stack.poll();
			if(node3.father_x==a && node3.father_y==b) {
				System.out.println("("+node3.row+","+node3.column+")");
				a=node3.row;
				b=node3.column;
			}
		}
	}
	public static void bfs(int m,int n) {//m n 分别为 目标点 横坐标和纵坐标
		Queue<Node> queue = new LinkedList<Node>();
		Node node1 = new Node(0, 0,-1,-1, 1);
		queue.offer(node1);
		while(!queue.isEmpty()) {
			Node node2 = queue.poll();
			stack.add(node2);
			
			if(node2.row==m && node2.column==n) {
//				System.out.println(node2.length); //记录一共几步
				break;
			}
			
			for(int i=0;i<4;i++) {
				int temp1 = node2.row+next[i][0];
				int temp2 = node2.column+next[i][1];
				
				if(temp1>4 || temp2>4 || temp1<0 || temp2<0 ||data[temp1][temp2]==1) {
					continue;
				}
					
				data[temp1][temp2]=1; //也可以使用visit标记,这里直接修改原数组
				queue.add(new Node(temp1,temp2,node2.row,node2.column,node2.length+1));
			}		
		}	
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用深度优先搜索(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、付费专栏及课程。

余额充值