[LeetCode] 面试题13. 机器人的运动范围
一开始想出来的解法就是错误的,ohmo
class Solution { public int movingCount(int m, int n, int k) { int res=0; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ int h=i%10+i/10+i%100+i/100+j%10+j/10; if(h<=k){ res++; } } } return res; } }
正确的应用DFS或者BFS啊!!!!
class Solution { public int movingCount(int m, int n, int k) { boolean[][] visited=new boolean[m][n]; return dfs(0,0,m,n,k,visited); } public int dfs(int i,int j,int m,int n,int k,boolean[][] visited){ if(i>=m||j>=n||k<getSum(i)+getSum(j)||visited[i][j]){ return 0; } visited[i][j]=true; return 1+dfs(i+1,j,m,n,k,visited)+dfs(i,j+1,m,n,k,visited); } private int getSum(int a){ int sum=a%10; int tmp=a/10; while(tmp>0){ sum+=tmp%10; tmp/=10; } return sum; } }