Acwing 844 走迷宫

题目

给定一个 n×m 的二维整数数组,用来表示一个迷宫,数组中只包含 00 或 11,其中 00 表示可以走的路,11 表示不可通过的墙壁。

最初,有一个人位于左上角 (1,1)(1,1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。

请问,该人从左上角移动至右下角 (n,m) 处,至少需要移动多少次。

数据保证 (1,1)(1,1) 处和 (n,m) 处的数字为 0,0,且一定至少存在一条通路。

输入格式

第一行包含两个整数 n 和 m。

接下来 n 行,每行包含 m 个整数(00 或 11),表示完整的二维数组迷宫。

输出格式

输出一个整数,表示从左上角移动至右下角的最少移动次数。

数据范围

1≤n,m≤1001≤100

输入样例:
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
输出样例:
8

代码与解析

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    private static final int N = 110;
    private static int[][] g = new int[N][N], d = new int[N][N];
    private static PII[] q = new PII[N * N];
    private static int n;
    private static int m;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取输入的 n 和 m
        n = scanner.nextInt();
        m = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        // 读取迷宫数据并存入 g 数组
        for (int i = 0; i < n; i++) {
            String[] str = scanner.nextLine().split(" ");
            for (int j = 0; j < m; j++) {
                g[i][j] = Integer.parseInt(str[j]);
            }
        }

        System.out.println(bfs()); // 调用 bfs 方法计算最短路径
        scanner.close();
    }

    private static int bfs() {
        int hh = 0, tt = 0;
        q[0] = new PII(0, 0); // 初始化队列,起点为 (0, 0)
        for (int i = 0; i < d.length; i++) {
            for (int j = 0; j < d[i].length; j++) {
                d[j][i] = -1; // 初始化距离数组为 -1,表示未访问过
            }
        }
        d[0][0] = 0; // 起点到起点的距离为 0

        int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1}; // 上右下左四个方向
        while (hh <= tt) {
            PII t = q[hh++];
            for (int i = 0; i < 4; i++) {
                int x = t.getFirst() + dx[i], y = t.getSecond() + dy[i]; // 计算当前位置移动后的位置
                if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1) {
                    d[x][y] = d[t.getFirst()][t.getSecond()] + 1; // 更新到达新位置的距离
                    q[++tt] = new PII(x, y); // 将可到达的新位置加入队列
                }
            }
        }
        return d[n - 1][m - 1]; // 返回右下角的最短路径长度
    }

}

class PII {
    private int first;
    private int second;

    public PII(int first, int second) {
        this.first = first;
        this.second = second;
    }

    public PII() {

    }

    public int getFirst() {
        return first;
    }

    public int getSecond() {
        return second;
    }
}

不用数组模拟队列,直接用Linkedlist


import java.util.*;

public class Main {
	static int n,m;
	static int[][] g, dis;
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		m = in.nextInt();
		in.nextLine();
		g = new int[n + 10][m + 10];
		dis = new int[n + 10][m + 10];
		for(int i = 0;i < n;i ++) {
			String[] ss = in.nextLine().split(" ");
			for(int j = 0;j < m;j ++) {
				g[i][j] = Integer.parseInt(ss[j]); 
			}
		}
		System.out.println(bfs() + 1);
		
	}
	public static int bfs() {
		Queue<PII> q = new LinkedList<>();
		q.offer(new PII(0, 0));
		for(int i =0 ;i <n ;i ++) {
			for(int j =0 ;j < m;j ++) {
				dis[i][j] = -1;
			}
		}
		int[] dx = {0, 1, 0, -1}, dy = {1, 0 , -1, 0};
		while(!q.isEmpty()) {
			PII pi = q.poll();
			for(int i =0 ;i < 4;i ++) {
				int a = pi.getFirst() + dx[i];
				int b = pi.getSecond() + dy[i];
				if(a >= 0 && a < n && b >= 0 && b < m && dis[a][b] == -1 && g[a][b] == 0) {
					dis[a][b]= dis[pi.getFirst()][pi.getSecond()] + 1;
					q.offer(new PII(a, b));
				}
			}
		}
		
		return dis[n - 1][m - 1];
	}
	public static class PII{
		int first, second;
		public PII(int first, int second) {
			this.first = first;
			this.second = second;
		}
		public PII() {};
		public int getFirst(){
			return first;
		}
		public int getSecond(){
			return second;
		}
	}

}

三刷代码:


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    static int n, m,M = (int)1e9 ;
    static int[][] g, dis;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        g = new int[n + 1][m + 1];
        dis = new int[n +1][m + 1];

        for (int i = 0;i < n;i ++) {
            for(int j = 0;j < m;j ++) {
                g[i][j] = in.nextInt();
            }
        }
        bfs(0,0);
        System.out.println(dis[n - 1][m - 1]);
    }
    public static void bfs(int x, int y) {
        Queue<PII> q = new LinkedList<>();
        q.offer(new PII(x, y));
        int[] dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
        while (!q.isEmpty()) {
            PII p = q.poll();
            for (int i = 0;i < 4;i ++) {
                int now_x = p.x + dx[i], now_y = p.y + dy[i];
                if (!isArea(now_x, now_y) || g[now_x][now_y] == 1) continue;
                if (g[now_x][now_y] == 0) {
                    dis[now_x][now_y] = dis[p.x][p.y] + 1;
                    g[now_x][now_y] = 2;
                    q.offer(new PII(now_x, now_y));
                }
            }
        }
    }
    public static boolean isArea(int x, int y) {
        if (x < 0 || y < 0 || x >= n || y >= m) return false;
        return true;
    }
    static class PII {
        int x, y;
        public PII(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值