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; }
};