PackageCom.exe8.offer;/*** "title" There is a M-row and N-column squares on the ground. A robot moves from a grid of coordinates 0,0, * each time it can only move one block to the left, right, top, and bottom four directions, but cannot enter the sum of the digits of the row and column coordinates greater than the lattice of 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? * @authorWGS **/ Public classRobotmovingcount { Public intMovingcount (intThresholdintRowsintcols) { //create an array to record whether a grid is accessed Boolean[] visited =New Boolean[Rows][cols]; returnMovingcountcore (threshold, rows, cols, 0, 0, visited); } Private intMovingcountcore (intThresholdintRowsintColsintI,intJBoolean[] visited) { intCount = 0; //Exception Handling if(I < 0 | | I >= rows | | J < 0 | | J >=cols|| Numberindexcount (i) + Numberindexcount (j) >Threshold||Visited[i][j])return0; VISITED[I][J]=true; Count= 1 + movingcountcore (threshold, rows, cols, i-1, J, visited)+ Movingcountcore (threshold, rows, cols, i + 1, J, visited)+ Movingcountcore (threshold, rows, cols, I, j-1, visited)+ Movingcountcore (threshold, rows, cols, I, j + 1, visited); returncount; } //the sum of the digits used to calculate each coordinate Private intNumberindexcount (intNumber ) { intsum = 0; while(Number > 0) {sum+ = number% 10; number/= 10; } returnsum; } Public Static voidMain (string[] args) {intsum =NewRobotmovingcount (). Movingcount (5, 10, 10); SYSTEM.OUT.PRINTLN (sum); }}
There is a M-row and N-column squares on the ground. A robot moves from the grid of coordinates 0,0, each time only to the left, right, upper and lower four directions, but cannot enter the coordinates of the row and column coordinates of the sum of the squares greater than K. For example, when K is 18 o'clock, the robot can enter the square (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?
Sword refers to the range of motion of the offer series----robot