力扣LCR 130. 衣橱整理(DFS 解法)

本文介绍了一种使用深度优先搜索(DFS)解决LCR130衣橱整理问题的方法,通过遍历二维矩阵并检查每个位置的数位和是否小于给定数,计算最多可访问的合法位置数量。算法的时间复杂度为O(mn),空间复杂度也为O(mn)。
摘要由CSDN通过智能技术生成

Problem: LCR 130. 衣橱整理

题目描述

在这里插入图片描述

思路

首先该问题可以归纳为一类遍历二维矩阵的题目,此类中的一部分题目可以利用DFS来解决,具体到本题目:

我们可以利用一个布尔类型的二维数组记录我们已经访问过的可以访问的位置(访问过则记录为true),再同时在每次遍历时我们判断当前的位置是否可以访问(依据题目要求其横纵坐标的数位和要小于给定的数字cnt),以及当前位置的上、下、左、右四个方位是否可以继续访问。

解题方法

1.定义布尔类型的二维数组(大小为mn)用于记录已经访问过的可以访问的位置。定义int类型的变量count记录可以访问的位置个数;
2.编写判断当前横纵坐标数位和是否大于给定数的函数check;
3.编写深度优先函数:

3.1每次先将当前位置标记为true,count加一
3.1用一个二维数组**int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};记录某个位置的上下左右四个方位的位置,便于DFS,
3.2for循环(从1-4表示查找四个方位),每次执行
令newI = i + directions[di][0];newJ = j + directions[di][1];**表示下一个待选位置的横纵坐标
3.3判断下一个待选位置的横纵坐标是否合法(横纵坐标不越界,没有被合法访问过,数位和小于给定数)

复杂度

时间复杂度:

O ( m n ) O(mn) O(mn)

空间复杂度:

O ( m n ) O(mn) O(mn)

Code

class Solution {
    private boolean[][] visited;
    private int count = 0;

    /**
     * Get the maximum number of collations
     *
     * @param m   The maximum range of the horizontal coordinate
     * @param n   The maximum range of the ordinate
     * @param cnt Given number
     * @return int
     */
    public int wardrobeFinishing(int m, int n, int cnt) {
        visited = new boolean[m][n];
        dfs(0, 0, m, n, cnt);
        return count;
    }

    /**
     * Use DFS to get the maximum number of collations
     *
     * @param i Abscissa
     * @param j Ordinate
     * @param m The maximum range of the horizontal coordinate
     * @param n The maximum range of the ordinate
     * @param k Given number
     */
    private void dfs(int i, int j, int m, int n, int k) {
        //Mark the current location
        visited[i][j] = true;
        count++;
        //The current position of the location of the four directions
        int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for (int di = 0; di < 4; ++di) {
            int newI = i + directions[di][0];
            int newJ = j + directions[di][1];
            if (newI >= m || newI < 0 || newJ >= n || newJ < 0
                    || visited[newI][newJ] == true || check(newI, newJ, k) == false) {
                continue;
            }
            dfs(newI, newJ, m, n, k);
        }
    }

    /**
     * Determine if the sum of digits is less than k
     *
     * @param i Abscissa
     * @param j Ordinate
     * @param k Given number
     * @return boolean
     */
    private boolean check(int i, int j, int k) {
        int sum = 0;
        while (i != 0) {
            sum += (i % 10);
            i /= 10;
        }
        while (j != 0) {
            sum += (j % 10);
            j /= 10;
        }
        return sum <= k;
    }
}
  • 18
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值