题解 | #机器人的运动范围#
机器人的运动范围
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
#include <vector> class Solution { public: bool isvalid(int threshold, int rows, int cols, int x, int y){ int sum = 0; if (x >= rows || y >= cols) return false; while (x != 0){ sum += x % 10; x /= 10; } while (y != 0){ sum += y % 10; y /= 10; } return sum > threshold ? false : true; } int result = 0; void backtracking(int threshold, int rows, int cols, int x, int y, vector<vector<bool>>& used){ if (isvalid(threshold, rows, cols, x, y) && used[x][y] == true){ used[x][y] = false; result++; backtracking(threshold, rows, cols, x + 1, y, used); backtracking(threshold, rows, cols, x, y + 1, used); } else return; } int movingCount(int threshold, int rows, int cols) { vector<vector<bool>> used(rows, vector<bool>(cols, true)); backtracking(threshold, rows, cols, 0, 0, used); return result; } };