The motion range of the robot for the offer (66)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.