Backtracking robot's motion range __ The robot's total motion lattice

Source: Internet
Author: User

Problem: There is a square of M row n columns on the ground. A robot moves from the grid of coordinates (0,0), and it can move one grid to the left, right, top, and bottom at a time, but it cannot enter a lattice with a number of rows and columns of coordinates 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 but it cannot enter the square (35,38), because 3+5+3+8=19 how many squares the robot can reach.

Analysis:

Similar to the path in the previous matrix, this square can also see a m*n matrix. Also in this matrix, other squares except those on the boundary have four adjacent squares.

The robot began to move from the coordinates (0,0). When it is ready to enter the coordinates (I,J) of the lattice, by examining the coordinates of the digits and to determine whether the robot can enter. If the robot can enter a grid of coordinates (I,J), we will then judge whether it can enter four adjacent lattices (i,j-1), (I-1,j), (i,j+1) and (I+1,J).

The code implemented by backtracking method:

#include <stdio.h>//Get a number of digits and int getdigitsum (int number) {int sum = 0;
        while (number > 0) {sum = = number% 10;
    Number/= 10;
return sum; }//Determine whether to enter coordinates (ROW,COL) of checkers Check (int threshold, int rows, int cols, int row, int col, bool* visited) {if (row >= 0 && Row < rows && Col >= 0 && Col < cols && getdigitsum (row) + Getdigits
    
    Um (col) <= threshold &&!visited[row* cols + col]) return true;
return false;
    int Movingcountcore (int threshold, int rows, int cols, int row, int col, bool* visited) {int count = 0;
    
        if (check (threshold, rows, cols, row, col, visited)) {Visited[row * cols + col] = true; Count = 1 + movingcountcore (threshold, rows, cols, Row-1, col, visited) + Movingcou Ntcore (threshold, rows, cols, row, col-1, visited) + movIngcountcore (threshold, rows, cols, row + 1, col, visited) + Movingcountcore (threshol
    D, rows, cols, row, col + 1, visited);
return count;
    //Get all the lattice int movingcount (int threshold, rows, int cols) {bool *visited = new Bool[rows * Cols] that the robot can move;
        
    for (int i = 0; i < rows * COLS; ++i) visited[i] = false;
    
    int count = Movingcountcore (threshold, rows, cols, 0, 0, visited);
    
    Delete[] visited; 
return count; }//================================ test code ================================ void Test (char* testname, int threshold
    
    , int rows, int cols, int expected) {if (testname!= NULL) printf ("%s begins:", testname);
    if (Movingcount (threshold, rows, cols) = = expected) printf ("passed.\n");
else printf ("failed.\n");

} void Test1 () {Test ("Test1", 5, 10, 10, 21);} void Test2 () {Test ("Test2", 15, 20,20, 359);

} void Test3 () {Test ("Test3", 10, 1, 100, 29);}

void Test4 () {Test ("Test4", 10, 1, 10, 10);}

void Test5 () {Test ("Test5", 15, 100, 1, 79);}

void Test6 () {Test ("Test6", 15, 10, 1, 10);}

void Test7 () {Test ("Test7", 15, 1, 1, 1);}

void Test8 () {Test ("Test8",-10, 10, 10, 0);}
    int main (int agrc, char* argv[]) {test1 ();
    Test2 ();
    Test3 ();
    Test4 ();
    Test5 ();
    Test6 ();
    Test7 ();
Test8 (); }



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.