The range of motion of a offer-robot the sword refers to an offer

Source: Internet
Author: User

Huadian North Wind Blows
Key laboratory of cognitive computing and application, Tianjin University
Date: 2015/10/12

Topic description
There is a square of M row and N columns on the ground. A robot moves from the grid of the coordinate 0,0, each time only to the left, right, up, in the next four directions to move a grid, but can not enter the row coordinates and column coordinates of the digits and greater than k of the lattice. 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 square (35,38) because 3+5+3+8 = 19. How many squares can the robot reach?

Parsing: Backtracking method (DFS). Look for the next feasible point clockwise around the feasible point, without backtracking.

Class Solution {public:int movingcount (int threshold, int rows, int cols) {bool* visited = new Bool[ro
        Ws*cols];
        Memset (visited, false, Rows*cols);
        int count = 0;
        Recallfunction (rows, cols, threshold, 0, 0, visited, count);
    return count; } void recallfunction (const int &row, const int &COL,CONST int &limit, int x, int y, BOOL * visited, int &
        Amp;count) {if (x < 0 | | | x >= COL | | y < 0 | | y >= row) return;
        bool T1 = Visited[y*col + x];
        BOOL T2 = Access (x, y, limit);
            if ((t1==false) && T2) {cout << x << "" << y << Endl;
            ++count;
            Visited[y*col + x] = true;
            Recallfunction (Row, col, limit, x + 1, y, visited, count);
            Recallfunction (Row, col, limit, x, Y + 1, visited, count);
            Recallfunction (Row, col, limit, x-1, y, visited, count); ReCallFunction (Row, col, limit, X, Y-1, visited, count);
        } bool Access (int x, int y,const int & limit) {int val = 0;
            while (x>0) {val = = x 10;
        x/= 10;
            while (y>0) {val + = y% 10;
        Y/= 10; Return val > Limit?
    False:true; }
};

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.