Range of motion of the robot-sword

Source: Internet
Author: User

Description of the robot's motion range topic

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?

Ideas
    1. This is similar to the "path in the Matrix" topic, all using backtracking, but the starting point of this question is fixed, that is, starting from the (0,0) position.
    2. We also create a matrix based on the number of rows and columns, and this matrix is a matrix of packet wildest, where each location stores the number of each bit in the position subscript, and then the direct comparison can be
    3. This topic is about the maximum number of squares that can be reached, so we don't have to reduce the result by 1 when we're on the stack, because if we go through, we can get there.
Code
Import Java.util.Stack;PublicClass Solution {PublicIntMovingcount(int threshold,int rows,int cols) {int result =0;if (Threshold <=0 | | Rows <=0 | | Cols <=0) {return result; }int[][] data =NewInt[rows +2][cols +2];for (int i =0; I < rows +2; i++) {for (Int J =0; J < cols +2; J + +) {if (i = =0 | | i = = (rows +1) | | j = =0 | | j = = (cols +1)) {Data[i][j] =-1; }else {Data[i][j] =0;int Rowvar = i-1;int Colvar = J-1;while (Rowvar >0) {Data[i][j] + = (rowvar%10); Rowvar/=10; }while (Colvar >0) {Data[i][j] + = (colvar%10); Colvar/=10; }}}} Stackstack =New Stack (); boolean[][] Flag =New Boolean[rows +2][cols +2];for (int i =0; I < rows +1; i++) {for (Int J =0; J < cols +1; J + +) {Flag[i][j] =True } }Int[] init = {1,1}; flag[1][1] =FalseStack.push (init); result++;while (!Stack.isempty ()) {int[] Temp = (Int[])Stack.peek ();int row = temp[0];int col = temp[1];if (Data[row-1][col] <= threshold && Data[row-1][col] >0 && Flag[row-1][col] = =True) {Int[] Position = {row-1, col};Stack.push (position); flag[position[0]][position[1]] =False result++; }Elseif (Data[row][col +1] <= threshold && Data[row][col +1] >0 && Flag[row][col +1] = =True) {Int[] Position = {row, col +1};Stack.push (position); flag[position[0]][position[1]] =False result++; }Elseif (Data[row +1][col] <= threshold && Data[row +1][col] >0 && Flag[row +1][col] = =True) {Int[] Position = {row +1, col};Stack.push (position); flag[position[0]] [position[1]] = false; result++;} Else if (Data[row][col- 1] <= threshold && Data[row][col- 1] > 0 && flag[row][ Col- 1] = = true) { int[] position = {row, col- 1}; Stack.push (position); flag[position[0]][position[1]] = false; result++;} else { stack.pop ();}} return result;}}                  

Range of motion of the robot-sword

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.