题解 | #机器人的运动范围#
机器人的运动范围
https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
class Solution { public: // 深度优先搜索也是会全部找完的 int cnt = 0; queue<pair<int, int>> q; vector<vector<bool>> visited; int movingCount(int threshold, int rows, int cols) { if(threshold <= 0) return 1; visited.resize(rows, vector<bool>(cols, false)); q.push(make_pair(0, 0)); int i, j; while (!q.empty()) { i = q.front().first, j = q.front().second; cout<<i<<' '<<j<<' '<<q.size()<<' '; q.pop(); if (i>=rows||j>=cols||count_num(i, j)>threshold||visited[i][j]) continue; else{ visited[i][j] = true; ++cnt; } cout<<cnt<<'-'; q.push(make_pair(i+1, j)); q.push(make_pair(i, j+1)); } return cnt; } int count_num(int i, int j) { int c = 0; while (i != 0) { c += i % 10; i /= 10; } while (j != 0) { c += j % 10; j /= 10; } return c; } };
front写成back检查了一个小时。。。牛客调试也用不了。。。