Vulnhub——DC-6滲透
阿新 • • 發佈:2022-03-10
package leetcode; public class offer_13 { //全域性變數記錄總數 int count=0; public int movingCount(int m, int n, int k) { //防止重複計數 int[][] visit=new int[m][n]; backtrack(m, n, k, visit, 0, 0); return count; } //行座標和列座標的數位之和 public int toSum(int m,int n) {int sum=0; while(m!=0||n!=0) { sum=m%10+n%10+sum; m=m/10; n=n/10; } return sum; } public void backtrack(int m,int n,int k,int[][] visit,int i,int j) { if(visit[i][j]==0) { if(toSum(i, j)<=k) { count=count+1; visit[i][j]=1; if(i>0) { backtrack(m, n, k, visit, i-1, j); } if(i<m-1) { backtrack(m, n, k, visit, i+1, j); } if(j>0) { backtrack(m, n, k, visit, i, j-1); } if(j<n-1) { backtrack(m, n, k, visit, i, j+1); } } } } public static void main(String[] args) { // TODO Auto-generated method stub offer_13 off=new offer_13(); System.out.println(off.movingCount(2, 3, 1)); } }