Package List;
/**
* Created by Administrator on 2015/10/10.
*/
/*
* There is a M-row and N-column squares on the ground. A robot starts moving from a grid of coordinates 0,0,
* Each time only left, right, top, bottom four move one block, but cannot enter the row and column coordinates of the number of the sum of the grid greater than K.
* For example, when K is 18 o'clock, the robot can enter the grid (35,37) because 3+5+3+7 = 18. However, it cannot enter the grid (35,38),
* because 3+5+3+8 = 19. How many grids can the robot reach?
*
* */
public class Demo2 {
/**
* Sum is a method for calculating the total number of qualifying squares
* Row is the number of rows in a two-dimensional array
* Col is the number of columns in a two-dimensional array
* */
public static int sum (int[][] grids,int row,int col,int threshold,int[] flag) {
if (numsum (row) +numsum (col) >threshold| | row<0| | Col<0
|| row>=grids.length| | col>=grids[1].length| | flag[row* (grids[1].length) +col]==1) {
return 0;
}
flag[row* (grids[1].length) +col]=1;
return sum (grids,row-1,col,threshold,flag)
+sum (Grids,row+1,col,threshold,flag)
+sum (Grids,row,col-1,threshold,flag)
+sum (Grids,row,col+1,threshold,flag) +1;
}
public static int numsum (int i) {
int result=0;
while (i%10!=0) {
result=result+i%10;
i=i%10;
}
return result;
}
}
Robot Walking grid problem