博客
关于我
AcWing 901. 滑雪 (搜索优化为dp)
阅读量:344 次
发布时间:2019-03-04

本文共 2856 字,大约阅读时间需要 9 分钟。

为了解决这个问题,我们需要找到二维矩阵中最长的滑雪轨迹。滑雪轨迹的定义是从某个区域出发,每次只能向上、下、左、右滑动一个单位距离,并且只能滑向高度比当前位置低的相邻区域。

方法思路

我们可以使用动态规划来优化这个问题。动态规划通过预先计算每个区域的最大可能滑动长度,从而避免重复计算。具体步骤如下:

  • 读取输入并构建矩阵:读取矩阵的尺寸R和C,然后读取矩阵中的数据。
  • 初始化动态规划数组:创建一个dp数组,dp[i][j]表示从(i, j)出发可以滑动到的最长路径长度。
  • 按照高度从高到低排序:确保处理较高的区域在前,较低的区域在后。
  • 遍历每个区域:对于每个区域,检查其四个相邻区域(上、下、左、右)。如果相邻区域的高度低于当前区域的高度,更新当前区域的dp值。
  • 找到最大的dp值:遍历dp数组,找出最大的值作为最长滑雪轨迹的长度。
  • 解决代码

    import java.io.*;class Main {    static int N = 310;    static int[][] t = new int[N][N];    static int[][] dp = new int[N][N];    static int[] dx = {-1, 1, 0, 0};    static int[] dy = {0, 0, -1, 1};        public static void main(String[] args) throws Exception {        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));        String[] params = buf.readLine().split(" ");        int r = Integer.valueOf(params[0]);        int c = Integer.valueOf(params[1]);                for (int i = 1; i <= r; ++i) {            String[] info = buf.readLine().split(" ");            for (int j = 1; j <= c; ++j) {                t[i][j] = Integer.valueOf(info[j-1]);            }        }                // 按照高度从高到低排序        int[][] sorted = new int[R+1][C+1];        for (int i = 1; i <= r; ++i) {            for (int j = 1; j <= c; ++j) {                sorted[i][j] = t[i][j];            }        }                // 按照高度从高到低排序后的坐标        List
    coords = new ArrayList<>(); for (int i = 1; i <= r; ++i) { for (int j = 1; j <= c; ++j) { coords.add(new int[]{i, j, t[i][j]}); } } // 按高度从高到低排序 Collections.sort(coords, new Comparator
    () { @Override public int compare(int[] a, int[] b) { return Integer.compare(a[2], b[2]); } }); for (int i = 1; i <= r; ++i) { for (int j = 1; j <= c; ++j) { dp[i][j] = 1; // 每个点至少可以滑动自己 } } for (int[] coord : coords) { int a = coord[0]; int b = coord[1]; for (int dir = 0; dir < 4; ++dir) { int x = a + dx[dir]; int y = b + dy[dir]; if (x >= 1 && x <= r && y >= 1 && y <= c) { if (t[a][b] > t[x][y]) { dp[a][b] = Math.max(dp[a][b], dp[x][y] + 1); } } } } int max = 1; for (int i = 1; i <= r; ++i) { for (int j = 1; j <= c; ++j) { if (dp[i][j] > max) { max = dp[i][j]; } } } System.out.print(max); }}

    代码解释

  • 读取输入:从标准输入读取矩阵的尺寸和数据,构建矩阵t。
  • 排序处理:将矩阵中的每个元素按照高度从高到低排序,确保处理较高的区域在前。
  • 动态规划初始化:创建dp数组,初始值为1,因为每个区域至少可以滑动到自己。
  • 更新dp数组:对于每个区域,检查其四个相邻区域,如果相邻区域的高度低于当前区域的高度,更新当前区域的dp值。
  • 寻找最大值:遍历dp数组,找到最大的值作为最长滑雪轨迹的长度。
  • 这种方法通过动态规划优化了暴力解法,确保在合理的时间复杂度内解决问题。

    转载地址:http://syre.baihongyu.com/

    你可能感兴趣的文章
    poj 1321(回溯)
    查看>>
    Qt高级——Qt元对象系统源码解析
    查看>>
    qt调用vs2008编写的dll动态库(隐式调用)
    查看>>
    Qt读取注册表默认值
    查看>>
    poj 1679 判断MST是不是唯一的 (次小生成树)
    查看>>
    POJ 1703 Find them, Catch them
    查看>>
    POJ 1703 Find them, Catch them 并查集
    查看>>
    POJ 1738 An old Stone Game(石子合并)
    查看>>
    POJ 1740 A New Stone Game(博弈)题解
    查看>>
    Qt网络编程之实例二POST方式
    查看>>
    POJ 1765 November Rain
    查看>>
    poj 1860 Currency Exchange
    查看>>
    POJ 1961 Period
    查看>>
    POJ 2019 Cornfields (二维RMQ)
    查看>>
    poj 2057 The Lost House 贪心思想在动态规划上的应用
    查看>>
    poj 2057 树形DP,数学期望
    查看>>
    poj 2112 最优挤奶方案
    查看>>
    Qt编写自定义控件12-进度仪表盘
    查看>>
    SpringBoot主启动原理在SpringApplication类《第六课》
    查看>>
    poj 2186 Popular Cows :求能被有多少点是能被所有点到达的点 tarjan O(E)
    查看>>