Sword refers to the range of motion of the offer series----robot

Source: Internet
Author: User

 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

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.