数据结构-怀化学院期末题(59)

题目描述:

迷宫求最短路径。
只能走上、下,左、右四个方向。
输入:
输入只有一个用例。
第一行为迷宫大小,n,m,即n行m列,
第二行为起点位置,
第三行为终点位置,
接下来的为迷宫图,1表示墙壁,0表示通道

输出:
输出从起点到终点的最短路径,即最少走的步数

输入样例:

10 10
2 2
9 9
1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 0 1 0 1
1 0 0 1 0 0 0 1 0 1
1 0 0 0 0 1 1 0 0 1
1 0 1 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 1 1 0 1
1 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1

输出样例:

14 

代码:

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

public class no59 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int sx = in.nextInt();
        int sy = in.nextInt();
        int ex = in.nextInt();
        int ey = in.nextInt();
        int[][] map = new int[110][110];
        for(int i = 1;i <= n;i ++)
            for(int j = 1;j <= m;j ++)
                map[i][j] = in.nextInt();

        System.out.printf("%d",bfs(n,m,sx,sy,map,ex,ey));
    }
    public static int bfs(int n,int m,int sx,int sy,int[][] map,int ex,int ey){
        int[][] book = new int[110][110];
        int[] dx = {1,-1,0,0};
        int[] dy = {0,0,1,-1};
        int[][] lu = new int[110][110];
        Queue<Node> q = new LinkedList<>();
        q.offer(new Node(sx,sy));
        book[sx][sy]=1;
        while(!q.isEmpty()){
             Node t = q.peek();
             q.poll();
             if(t.x == ex && t.y == ey){
                return lu[ex][ey];
             }
             for(int i = 0;i < 4;i ++){
                 int qx = t.x + dx[i];
                 int qy = t.y + dy[i];
                 if(qx>0&&qy>0&&qx<=n&&qy<=m&&book[qx][qy]==0&&map[qx][qy]==0){
                     book[qx][qy] = 1;
                     lu[qx][qy] = lu[t.x][t.y] + 1;
                     q.offer(new Node(qx,qy));
                 }
             }
        }
        return lu[ex][ey];
    }
    static class Node{
        int x;int y;

        public Node(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}

相关推荐

  1. 数据结构-怀化学院期末59

    2023-12-27 17:34:01       55 阅读
  2. 数据结构-怀化学院期末58

    2023-12-27 17:34:01       62 阅读
  3. 数据结构-怀化学院期末56

    2023-12-27 17:34:01       52 阅读
  4. 数据结构-怀化学院期末(34)

    2023-12-27 17:34:01       50 阅读
  5. 数据结构-怀化学院期末(489)

    2023-12-27 17:34:01       55 阅读
  6. 数据结构-怀化学院期末

    2023-12-27 17:34:01       50 阅读
  7. 数据结构-怀化学院期末(1321)

    2023-12-27 17:34:01       62 阅读
  8. 数据结构-怀化学院期末

    2023-12-27 17:34:01       45 阅读
  9. 数据结构-怀化学院期末(490)

    2023-12-27 17:34:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 17:34:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 17:34:01       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 17:34:01       87 阅读
  4. Python语言-面向对象

    2023-12-27 17:34:01       96 阅读

热门阅读

  1. $bus的用法 vue

    2023-12-27 17:34:01       47 阅读
  2. Spring 表达式expression

    2023-12-27 17:34:01       49 阅读
  3. 相机FOV是什么英文单词的缩写,是什么意思。

    2023-12-27 17:34:01       56 阅读
  4. 前端axios与python库requests的区别

    2023-12-27 17:34:01       59 阅读
  5. effective c++ 笔记 导读/条款2-4

    2023-12-27 17:34:01       51 阅读
  6. NGINX高级技巧

    2023-12-27 17:34:01       61 阅读
  7. ElementuiPlus文件上传失败原因,一个小坑记录!

    2023-12-27 17:34:01       57 阅读
  8. 架构艺术:系统演进的精髓与实践

    2023-12-27 17:34:01       50 阅读
  9. Python实现音乐推荐系统

    2023-12-27 17:34:01       45 阅读
  10. python异常之assert语句

    2023-12-27 17:34:01       74 阅读