Title Description
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?
Code:
<span style= "color: #000099;" >import java.util.*;p Ublic class Solution {public int movingcount (int threshold, int rows, int cols) {b oolean[][] vist = new Boolean[rows][cols]; BFS Search from Start 0,0 position return Bfssearch (0, 0,rows, cols, vist, Threshold); } private int Bfssearch (int i, int j, int rows, int cols, boolean[][] vist, int threshold) {//illegal if (i<0 | | i>=rows | | j<0 | | j>=cols | | vist[i][j] | | (Numsum (i) +numsum (j) >threshold)) return 0; VIST[I][J] = true; Four directions return Bfssearch (I-1, J, rows, cols, vist, Threshold) + Bfssearch (i+1, J, rows, cols, vist , threshold) + Bfssearch (i, j-1, rows, cols, vist, Threshold) + Bfssearch (i, j+1, rows, cols, Vist, Threshold) +1; } private int Numsum (int i) {int sum = 0; while (i! = 0) {sum + = i%10; i/=10; } return suM }}</span>
The motion range of the robot for the offer (66)