[Sword refers to Offer learning] [interview question 67: robot motion range], sword refers to offer

Source: Internet
Author: User

[Sword refers to Offer learning] [interview question 67: robot motion range], sword refers to offer
Question: There is a square in n columns of m rows on the ground. A robot moves from a grid of coordinates (0, 0). Each time it moves a grid to the left, right, top, or bottom, however, you cannot enter a grid where the sum of the numbers of the row and column coordinates is greater than k.Example Analysis

For example, when k is 18, the robot can enter the square (35, 37) Because 3 + 5 + 3 + 7 = 18. but it cannot enter the square (35, 38), because 3 + 5 + 3 + 8 = 19. how many grids can this robot reach?

Solutions

And the previous[Sword refers to Offer learning] [interview question 66: path in the matrix]Similarly, this square shows a matrix of m * n. In this matrix, there are four adjacent grids except the grids on the boundary.
The robot starts to move from the coordinate (0, 0. When it is going to enter the lattice with the coordinates (I, j), it checks the coordinates and the number to determine whether the robot can enter. If the robot can enter a grid with the coordinates of (I, j), then we determine whether it can enter four adjacent grids (I, J-1), (I-1, j), (I, j, j + 1) AND (I + 1, j ).

Code Implementation
Public class Test67 {/*** question: there is a square in the n column of the m row on the ground. A robot moves from the grid of coordinates (0, 0). * each time a robot moves a grid to the left, right, top, or bottom, however, you cannot enter a grid where the sum of * bits of the row and column coordinates is greater than k. For example, when k is 18, the robot can enter the square (35, 37), * because 3 + 5 + 3 + 7 = 18. but it cannot enter the square (35, 38), because 3 + 5 + 3 + 8 = 19. * How many grids can this robot reach? ** @ Param threshold constraint value * @ param rows number of rows * @ param cols Number of columns * @ return maximum squares */public static int movingCount (int threshold, int rows, int cols) {// parameter verification if (threshold <0 | rows <1 | cols <1) {return 0 ;} // variable initialization boolean [] visited = new boolean [rows * cols]; for (int I = 0; I <visited. length; I ++) {visited [I] = false;} return movingCountCore (threshold, rows, cols, 0, 0, visited ); }/*** Recursive Backtracking Method ** @ param threshold constraint value * @ param rows number of rows * @ param cols Number of columns * @ param row the row currently being processed * @ param the column number currently processed by col * @ param visited access tag array * @ return the maximum number of squares */private static int movingCountCore (int threshold, int rows, int cols, int row, int col, boolean [] visited) {int count = 0; if (check (threshold, rows, cols, row, col, visited )) {visited [row * cols + col] = true; count = 1 + movingCo UntCore (threshold, rows, cols, row-1, col, visited) + movingCountCore (threshold, rows, cols, row, col-1, visited) + movingCountCore (threshold, rows, cols, row + 1, col, visited) + movingCountCore (threshold, rows, cols, row, col + 1, visited);} return count ;} /*** whether the disconnected robot can enter the coordinate (row, col) ** @ param threshold constraint value * @ param rows number of rows * @ param cols Number of columns * @ param row current row number * @ param col currently processed Column Number * @ param visited access flag array * @ return whether it can enter, true Yes, false No */private static boolean check (int threshold, int rows, int cols, int row, int col, boolean [] visited) {return col >=0 & col <cols & row> = 0 & row <rows &&! Visited [row * cols + col] & (getDigitSum (col) + getDigitSum (row) <= threshold );} /*** the sum of digits of a number ** @ param number * @ return the sum of digits */private static int getDigitSum (int number) {int result = 0; while (number> 0) {result + = (number % 10); number/= 10;} return result;} public static void main (String [] args) {System. out. println (movingCount (5, 10, 10) + "[21]"); System. out. println (movingCount (15, 20, 20) + "[359]"); System. out. println (movingCount (10, 1,100) + "[29]"); System. out. println (movingCount (10, 1, 10) + "[10]"); System. out. println (movingCount (15,100, 1) + "[79]"); System. out. println (movingCount (15, 10, 1) + "[10]"); System. out. println (movingCount (5, 10, 10) + "[21]"); System. out. println (movingCount (12, 1, 1) + "[1]"); System. out. println (movingCount (-10, 10, 10) + "[0]") ;}}
Running result

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46887811]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.