本文主要是介绍LCR 130. 衣橱整理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解题思路:
深度优先搜索(dfs)
数位之和的表示方法:
int sums(int x)int s = 0;while(x != 0) {s += x % 10;x = x / 10;}return s;
题解:
class Solution {int m, n, cnt;boolean[][] visited;public int wardrobeFinishing(int m, int n, int cnt) {this.m = m;this.n = n;this.cnt = cnt;this.visited = new boolean[m][n];return dfs(0, 0);}public int dfs(int i, int j) {if (i >= m || j >= n || sum(i)+sum(j)>cnt || visited[i][j]) return 0;visited[i][j] = true;return 1 + dfs(i + 1, j) + dfs(i, j + 1);}public int sums(int x) {int s = 0;while (x != 0) {s += x % 10;x = x / 10;}return s;}
}
这篇关于LCR 130. 衣橱整理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!