lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1846 随笔 :: 0 文章 :: 109 评论 :: 288万 阅读

1. 题目

读题

 HJ43 迷宫问题

 

考查点

 

2. 解法

思路

 

代码逻辑

 

具体实现

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        // 迷宫地图
        int[][] maze = {
                {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}
        };
        // 访问数组
        int[][] visited = new int[maze.length][maze[0].length];
        // 路径列表
        List<int[]> path = new ArrayList<>();
        // 调用递归函数
        dfs(maze, visited, path, 0 ,0);
    }

    // 定义一个递归函数,传入当前的坐标和一个列表来存储路径
    public static boolean dfs(int[][] maze, int[][] visited,
                              List<int[]> path,
                              int x ,int y) {
        // 将当前位置加入路径列表,并将访问数组对应位置设为1
        path.add(new int[]{x ,y});
        visited[x][y] = 1;
        // 判断是否到达了右下角,也就是终点
        if (x == maze.length -1 && y == maze[0].length -1) {
            // 打印出路径列表,并返回true
            for (int[] pos : path) {
                System.out.println("(" + pos[0] + "," + pos[1] + ")");
            }
            return true;
        }
        // 定义四个方向的移动
        int[] dx = {-1 ,1 ,0 ,0};
        int[] dy = {0 ,0 ,-1 ,1};
        // 尝试向四个方向移动
        for (int i = 0; i <4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            // 判断是否越界或者遇到墙壁或者已经访问过
            if (nx < 0 || nx >= maze.length || ny < 0 || ny >= maze[0].length ||
                    maze[nx][ny] ==1 || visited[nx][ny] ==1) {
                continue;
            }
            // 如果可以走,就递归调用自己
            if (dfs(maze ,visited ,path ,nx ,ny)) {
                return true;
            }
        }
        // 如果四个方向都不能走或者已经走过,就回溯
        path.remove(path.size() -1);
        visited[x][y] = 0;
        return false;
    }
}

 

自有实现

public class HJ043 {
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[][] maze = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
maze[i][j] = sc.nextInt();
}
}
int[][] visited = new int[m][n];
List<int[]> path = new ArrayList<>();

dfs(maze, path, visited, 0, 0);
}

public static boolean dfs(int[][] maze, List<int[]> path, int[][] visited, int x, int y) {
path.add(new int[]{x, y});
visited[x][y] = 1;

if (x == maze.length - 1 && y == maze[0].length - 1) {
for (int[] pos : path) {
System.out.println("(" + pos[0] + "," + pos[1] + ")");
}
return true;

}

for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= maze.length || ny < 0 || ny >= maze.length || maze[nx][ny] == 1 || visited[nx][ny] == 1) {
continue;
}

if (dfs(maze, path, visited, nx, ny)) {
return true;
}
}
path.remove(path.size() - 1);
visited[x][y] = 0;
return false;

}
}

 

 

3. 总结

posted on   白露~  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2022-07-21 MySQL应用之CROSS JOIN用法
2022-07-21 join连接的五种方式的简单使用案例(Inner join,Left join,Right join,Full join,Cross join)
2022-07-21 MySQL的日期和时间处理函数
2019-07-21 秒杀系统设计与实现
2019-07-21 秒杀系统的设计
2019-07-21 阿里P8架构师谈:阿里双11秒杀系统如何设计?
2019-07-21 秒杀系统设计的知识点
点击右上角即可分享
微信分享提示