【剑指offer】【搜索与回溯】13. 机器人的运动范围
搜索与回溯
class Solution { public: int get_single_sum(int x){ int s = 0; while(x){ s += x % 10; x /= 10; } return s; } int get_sum(pair<int, int> p){ return get_single_sum(p.first) + get_single_sum(p.second); } int movingCount(int m, int n, int k) { int res = 0; if(!n || !m) return 0; //判断边界 vector<vector<bool>> st(m, vector<bool>(n)); queue<pair<int, int>> q; q.push({0, 0}); int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; while(q.size()){ auto t = q.front(); q.pop(); if(get_sum(t) > k || st[t.first][t.second]) continue; res++; st[t.first][t.second] = true; for(int i = 0; i < 4; i++){ int x = t.first + dx[i], y = t.second + dy[i]; if(x >= 0 && x < m && y >= 0 && y < n){ q.push({x, y}); } } } return res; } };
相关推荐
quyunfei 2020-11-19
机器人智力研究 2020-11-18
聊天终结者机器人 2020-11-18
txq0 2020-11-20
zCSDN 2020-11-09
机器人智力研究 2020-11-05
ARMOTO机器人 2020-11-06
txq0 2020-11-06
遇见人工智能 2020-11-03
聊天终结者机器人 2020-11-02
clliuhust 2020-10-30
yatou0 2020-10-29
雨燕 2020-10-29
nodid 2020-10-29
yatou0 2020-10-29
zCSDN 2020-10-27
dhyddy 2020-10-27
聊天终结者机器人 2020-10-26