力扣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 + 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

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;
    }
}
相关推荐
君义_noip16 小时前
信息学奥赛一本通 1661:有趣的数列 | 洛谷 P3200 [HNOI2009] 有趣的数列
c++·算法·组合数学·信息学奥赛·csp-s
程序员:钧念16 小时前
深度学习与强化学习的区别
人工智能·python·深度学习·算法·transformer·rag
英英_17 小时前
MATLAB数值计算基础教程
数据结构·算法·matlab
一起养小猫17 小时前
LeetCode100天Day14-轮转数组与买卖股票最佳时机
算法·leetcode·职场和发展
hele_two18 小时前
快速幂算法
c++·python·算法
l1t18 小时前
利用DeepSeek将python DLX求解数独程序格式化并改成3.x版本
开发语言·python·算法·数独
jllllyuz18 小时前
基于子集模拟的系统与静态可靠性分析及Matlab优化算法实现
算法·matlab·概率论
程序员-King.19 小时前
day143—递归—对称二叉树(LeetCode-101)
数据结构·算法·leetcode·二叉树·递归
BlockChain88819 小时前
字符串最后一个单词的长度
算法·go
爱吃泡芙的小白白19 小时前
深入解析:2024年AI大模型核心算法与应用全景
人工智能·算法·大模型算法