【蓝桥杯】最远滑行距离_DFS&BFS ——java题解

问题描述

   小蓝准备在一个空旷的场地里面滑行,这个场地的高度不一,小蓝用一个 n 行 m 列的矩阵来表示场地,矩阵中的数值表示场地的高度。

   如果小蓝在某个位置,而他上、下、左、右中有一个位置的高度(严格)低于当前的高度,小蓝就可以滑过去,滑动距离为 1 。

   如果小蓝在某个位置,而他上、下、左、右中所有位置的高度都大于等于当前的高度,小蓝的滑行就结束了。

   小蓝不能滑出矩阵所表示的场地。

   小蓝可以任意选择一个位置开始滑行,请问小蓝最多能滑行多远距离。

输入格式

   输入第一行包含两个整数 n, m,用一个空格分隔。

   接下来 n 行,每行包含 m 个整数,相邻整数之间用一个空格分隔,依次表示每个位置的高度。

输出格式

   输出一行包含一个整数,表示答案。

样例输入

4 5
1 4 6 3 1
11 8 7 3 1
9 4 5 2 1
1 3 2 2 1
样例输出

7
样例说明

  滑行的位置一次为 (2, 1), (2, 2), (2, 3), (3, 3), (3, 2), (4, 2), (4, 3)。

评测用例规模与约定

  对于 30% 评测用例,1 <= n <= 20,1 <= m <= 20,0 <= 高度 <= 100。

  对于所有评测用例,1 <= n <= 100,1 <= m <= 100,0

题目代码——BFS

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class 最远滑行距离_bfs {
    static int n, m, res = 0;
    static int map[][] = new int[105][105];
    static int[][] dist = new int[110][110];//存起始位置到每一个坐标的移动次数
    static int dx[] = {0, 0, 1, -1};
    static int dy[] = {1, -1, 0, 0};

    public static void main(String[] args) throws IOException {
        BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));

        String[] s1 = ins.readLine().split(" ");
        n = Integer.parseInt(s1[0]);
        m = Integer.parseInt(s1[1]);

        for (int i = 1; i <= n; i++) {//输入地图
            String[] s2 = ins.readLine().split(" ");
            int tem = 0;
            for (int j = 1; j <= m; j++) {
                map[i][j] = Integer.parseInt(s2[tem++]);
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (dist[i][j] == 0) {
                    dist[i][j] = 1;
                    bfs(i, j);
                }
            }
        }
        System.out.println(res);
    }

    static void bfs(int x, int y) {
        Queue<Pair> q = new LinkedList<>();
        q.offer(new Pair(x, y));

        while (!q.isEmpty()) {
            Pair pair = q.poll();
            for (int i = 0; i < 4; i++) {
                int a = pair.x + dx[i], b = pair.y + dy[i];

                if (a < 1 || a > n || b < 1 || b > m) continue;
                if ((map[pair.x][pair.y] <= map[a][b]) ) continue;
                dist[a][b] = dist[pair.x][pair.y] + 1;
                res = Math.max(res, dist[a][b]);
                q.offer(new Pair(a, b));
            }
        }
    }
}

题目代码——DFS

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class 最远滑行距离_dfs {
    static int n, m, res = 0;
    static int map[][] = new int[105][105];
    static int st[][] = new int[105][105];
    static int[][] dist = new int[110][110];//存起始位置到每一个坐标的移动次数
    static int dx[] = {0, 0, 1, -1};
    static int dy[] = {1, -1, 0, 0};

    public static void main(String[] args) throws IOException {
        BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));

        String[] s1 = ins.readLine().split(" ");
        n = Integer.parseInt(s1[0]);
        m = Integer.parseInt(s1[1]);

        for (int i = 1; i <= n; i++) {//输入地图
            String[] s2 = ins.readLine().split(" ");
            int tem = 0;
            for (int j = 1; j <= m; j++) {
                map[i][j] = Integer.parseInt(s2[tem++]);
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dfs(i, j, 1);
            }
        }
        System.out.println(res);
    }

    static void dfs(int x, int y, int step) {
        if (x > n || y > m) return;

        for (int i = 0; i < 4; i++) {
            int a = x + dx[i], b = y + dy[i];
            if (a < 1 || a > n || b < 1 || b > m) continue;
            if (map[a][b] >= map[x][y]) continue;

            res = Math.max(res, step + 1);
            dfs(a, b, step + 1);


        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_61082895/article/details/129998933