66 motion range of the robot __ matrix Path

Source: Internet
Author: User
The range of motion of "66" robotTime limit: 1 seconds space limit: 32768K backtracking method

Topic Description
There is a square of M row and N columns on the ground. A robot starts moving from a grid of coordinates 0,0,
Each time you can move one grid to the left, right, top, and bottom four directions.
But cannot enter the row coordinates and the column coordinates the digit and is bigger than K's 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?
Niu ke Network topic link Click here VS2010 code:

Source:http://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpid=13&tqid=11219&rp=4

&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking//Author:yang Qiang//date:2016-8-15

#include <iostream> using namespace std; Thought analysis://1. The robot path range is a connected region.
So be able to record the reach.
2. A judgment is to be made on the four directions of each position. Class Solution {/******** bit and control function function ****************///out of control range, return 0, otherwise return 1; bool Sumofbit (int threshold1,int Curro
        W, int curcol) {int sumbit=0;
            while (Currow) {sumbit=sumbit+currow%10;
        CURROW=CURROW/10;
            while (Curcol) {sumbit=sumbit+curcol%10;
        CURCOL=CURCOL/10;
    Return sumbit>threshold1?0:1; /********** Path search function **********///input: Control thresholds, matrix ranks; current row, flag matrix//Output A pity Dorado can continue forward void Findpath (int threshold1, int ro
     ws1, int cols1, int currow, int curcol, int* flag) {//Border control: Beyond bounds, this direction cannot continue   if (currow<0 | | curcol<0 | | | currow>rows1-1 | | curcol>cols1-1) return; Bit and control: bit and not satisfied, the direction cannot continue if (!
        Sumofbit (Threshold1,currow,curcol)) return;
        Flag bit control: The path that has been traversed no longer continues if (flag[currow*cols1+curcol]==1) return;

        Current position through monitoring, update the logo bit, continue to search flag[currow*cols1+curcol]=1; Findpath (THRESHOLD1,ROWS1,COLS1, curRow-1, Curcol, flag);  Upward Findpath (THRESHOLD1,ROWS1,COLS1, Currow, curcol+1, flag);  Right Findpath (THRESHOLD1,ROWS1,COLS1, currow+1, Curcol, flag);  Downward Findpath (THRESHOLD1,ROWS1,COLS1, Currow, CURCOL-1, flag);
        Left//bool haspath=0; Haspath=findpath (THRESHOLD1,ROWS1,COLS1, curRow-1, CURCOL, flag)//Up//| | Findpath (THRESHOLD1,ROWS1,COLS1, Currow, curcol+1, flag)//Right//| | Findpath (THRESHOLD1,ROWS1,COLS1, currow+1, CURCOL, flag)//down//| |  Findpath (THRESHOLD1,ROWS1,COLS1, Currow, CURCOL-1, flag);
     Left///If the position is not all around/*if (!haspath)       return*/} public:int movingcount (int threshold, int rows, int cols) {//illegal situation definition if (thr eshold<0 | | rows<1 | |

        COLS&LT;1) return 0;
        Define a logo matrix that records the path that goes through.
        int* Flag=new Int[rows*cols]; for (int i=0; i<rows*cols; i++) flag[i]=0;

        Initializes a flag bit//search path and marks Findpath (threshold,rows,cols, 0, 0, Flag);
        Traverse the matrix to find out the number of tags.
        int reachednum=0;
        for (int i=0; i<rows*cols; i++) {if (flag[i]==1) reachednum++;//Initialize once flag bit}
    return reachednum;

}



};
    int main () {Solution S1;
Cout<<s1.movingcount (5,4,4) <<endl; }
Cattle NET customs clearance pictures:


Additional (sword refers to offer) 66 Customs card:

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.