题解 | #机器人的运动范围#
机器人的运动范围
http://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
void DFS(int threshold, int rows, int cols, int x, int y, int visit[][101]);
int movingCount(int threshold, int rows, int cols)
{
int visit[101][101];
memset(visit, 0, sizeof(visit));
int count = 0;
DFS(threshold, rows, cols, 0, 0, visit);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (visit[i][j])
count++;
return count;
}
void DFS(int threshold, int rows, int cols, int x, int y, int visit[][101])
{
//边界条件
if (x < 0 || x >= rows || y < 0 || y >= cols || visit[x][y] == 1)
return;
int tempSum;
tempSum = x % 10 + x / 10 + y % 10 + y / 10;
if (tempSum <= threshold)
{
visit[x][y] = 1;
DFS(threshold, rows, cols, x + 1, y, visit);
DFS(threshold, rows, cols, x, y + 1, visit);
}
return;
}