题目描述
写一个Java版的dfs,这道题也可以用暴力解决
样例
class Solution {
public int movingCount(int threshold, int rows, int cols)
{
boolean visit[][]=new boolean[rows][cols];
return dfs(threshold,0,0,rows,cols,visit);
}
public int dfs(int k,int m,int n,int row,int cols,boolean visit[][]){
if(m>=row||m<0)return 0;
if(n>=cols||n<0)return 0;
if(visit[m][n])return 0;
if(get_number(m, n)>k)return 0;
visit[m][n]=true;
return 1+dfs(k, m-1, n, row, cols, visit)+
dfs(k, m+1, n, row, cols, visit)+
dfs(k, m, n-1, row, cols, visit)+
dfs(k, m, n+1, row, cols, visit);
}
public int get_number(int m,int n){
int total=0;
while(m!=0){
total+=m%10;
m=m/10;
}
while(n!=0){
total+=n%10;
n=n/10;
}
return total;
}
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
blablabla