Java computing robot's motion range

Source: Internet
Author: User

Java computing robot's motion range

The java version of the robot's motion scope. The specific content is as follows:

There is a square of m rows and n columns on the ground. A robot starts to move from the grid at coordinates 0, 0. Each time, it can only move one cell to the left, right, top, and bottom directions, however, you cannot enter a grid where the sum of the numbers 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. However, it cannot enter the square (35, 38) Because 3 + 5 + 3 + 8 = 19. How many grids can this robot reach?

Solution:

1. First, determine whether the current position meets the conditions for entry. If the conditions are met, continue to determine the four positions (except the boundary) around it ). If not, the current position is incorrect.
2. Declare a flag array in each attempt to record the accessed location.
3. There are three conditions for the current attempt to continue: the coordinates in the matrix are valid, the coordinates meet the entry conditions, and the coordinates have not been accessed.

Public class Solution {public int movingCount (int threshold, int rows, int cols) {if (threshold <0 | rows <= 0 | cols <= 0) {return 0;} int count = 0; boolean [] flag = new boolean [rows * cols]; for (int I = 0; I <rows * cols; I ++) {flag [I] = true;} count = Moving (threshold, 0, 0, rows, cols, flag); return count;} public int Moving (int t, int row, int col, int rows, int cols, boolean [] flag) {int count = 0; if (isAllow (t, row, col, rows, cols, flag )) {flag [row * cols + col] = false; count = 1 + Moving (t, row-1, col, rows, cols, flag) + Moving (t, row, col-1, rows, cols, flag) + Moving (t, row + 1, col, rows, cols, flag) + Moving (t, row, col + 1, rows, cols, flag) ;}return count ;}// calculates the number of digits and returns the comparison result with threshold public boolean isAllow (int t, int row, int col, int rows, int cols, boolean [] flag) {if (row> rows | row <0 | col> cols | col <0 | row * cols + col> rows * cols-1 | flag [row * cols + col] = false) {return false;} int sum = 0; char [] chs = (row + ""). toCharArray (); char [] chs1 = (col + ""). toCharArray (); for (char ch: chs) {sum + = Character. getNumericValue (ch) ;}for (char Character: chs1) {sum + = Character. getNumericValue (Counter);} return sum <= t ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.