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

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 + directionsdi0;newJ = j + directionsdi1;**表示下一个待选位置的横纵坐标

3.3判断下一个待选位置的横纵坐标是否合法(横纵坐标不越界,没有被合法访问过,数位和小于给定数)

复杂度

时间复杂度:

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

空间复杂度:

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

Code

java 复制代码
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;
    }
}
相关推荐
mCell7 小时前
Lua 编程入门:从基础语法到元表
javascript·算法·lua
wuyk5559 小时前
98.C语言易混难点:字符数组与字符串指针的底层差异
c语言·开发语言·c++·stm32·嵌入式硬件·算法
峥无10 小时前
从0到1手撕红黑树:封装实现 my_map 与 my_set(SGI-STL 源码级深度解析)
开发语言·c++·笔记·算法·stl
不会代码的小猴10 小时前
3. 控件学习1
开发语言·c++·笔记·qt·算法
203号居民12 小时前
LeetCode hot 100 —41. 缺失的第一个正数
数据结构·算法·leetcode
专注仿真14 小时前
问答大模型技术方案算法实现-熵权法融合算法 + 交叉编码器重排算法
人工智能·python·算法·语言模型·问答大模型关键算法·熵权融合算法·交叉编码器重排算法
evans在进步15 小时前
LeetCode 322:零钱兑换——Java 动态规划详解
java·leetcode·动态规划
地平线开发者15 小时前
量化为什么会有损失:从量化误差到精度下降
算法
手写码匠16 小时前
华为云Flexus+DeepSeek征文|Dify 多 Agent 会话管理与上下文工程实战:让智能体“记住该记住的,忘掉该忘掉的“
人工智能·深度学习·算法·aigc