题解 | #机器人的运动范围#
机器人的运动范围
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
#include <thread>
#include <vector>
class Solution {
public:
void dfs(int threshold, int rows, int cols, int i, int j, vector<vector<bool> > &hash) {
if (i < 0 || i >= rows || j < 0 || j >=cols || hash[i][j] || i/10 + i%10 + j/10 + j%10 > threshold) {
return;
}
hash[i][j] = true;
dfs(threshold, rows, cols, i-1, j, hash);
dfs(threshold, rows, cols, i+1, j, hash);
dfs(threshold, rows, cols, i, j-1, hash);
dfs(threshold, rows, cols, i, j+1, hash);
}
int movingCount(int threshold, int rows, int cols) {
int res = 0;
vector<vector<bool> > hash(rows, vector<bool>(cols, false));
dfs(threshold, rows, cols, 0, 0, hash);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (hash[i][j]) res++;
}
}
return res;
}
};
