题解 | #机器人的运动范围#

机器人的运动范围

https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8

#include <string>
#include <utility>

class Solution {
  public:
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};


    bool vis[101][101] = {false};
    bool ans_v[101][101]={false};
    // typedef struct the_point {
    //     int x;
    //     int y;
    //     the_point() {};
    //     the_point(int _x, int _y): x(_x), y(_y) {}
    //     bool operator<(const the_point& n) const {
    //         return x < n.x;
    //     }


    // };

    // set<the_point>see_set;
    set< pair<int, int> >new_set;

    int number_bit(int x) {
        // x=20;
        string tmpstr = to_string(x);
        int sum = 0;
        for (int i = 0; i < tmpstr.size(); i++) {
            sum += tmpstr[i] - '0';
        }
        // cout << x << " 的数位和为:" << sum << endl;
        return sum;
    }
    int dfs(int threshold, int cur_row, int cur_col, int rows, int cols) {
        if (cur_row < 0 || cur_row >= rows || cur_col < 0 || cur_col >= cols ||
                vis[cur_row][cur_col] ||
                number_bit(cur_row) + number_bit(cur_col) > threshold) {
            return -1;
        }
        vis[cur_row][cur_col] = true;
        // the_point tmp_point{cur_row, cur_col};
        // see_set.insert(tmp_point);
        // new_set.insert(make_pair(cur_row, cur_col));
        ans_v[cur_row][cur_col] = true;
        // cout << tmp_point.x << " " << tmp_point.y << endl;

        for (int i = 0; i < 4; i++) {
            int next_rows = cur_row + dx[i];
            int next_cols = cur_col + dy[i];

            dfs(threshold, next_rows, next_cols, rows, cols);
        }
        // vis[cur_row][cur_col] = false;
        return 0;
    }
    int movingCount(int threshold, int rows, int cols) {
        dfs( threshold, 0, 0,  rows,  cols);
        int cnt =0;
        for (int i=0; i<101; i++) {
            for (int j=0; j<101; j++) {
                if(ans_v[i][j])
                cnt++;
            }
        }
        return cnt;
    }
};

本题:其实无需回溯,这样会加大运行时间,导致过不去,当我注释掉 //vis[]时//
  便通过了,对结果也无影响。
  
  另外:本题也暴露了我的set集合不熟悉,当插入typedef时,需要在里面写一个  
  //     bool operator<(const the_point& n) const {
    //         return x < n.x;
    //     }
	没写就报错了。

全部评论

相关推荐

哈哈哈,你是老六:百度去年裁员分评不好,赶紧弄点红包
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
14007次浏览 132人参与
# AI面会问哪些问题? #
845次浏览 21人参与
# 米连集团26产品管培生项目 #
6935次浏览 223人参与
# 你的实习产出是真实的还是包装的? #
2487次浏览 48人参与
# AI时代,哪个岗位还有“活路” #
2565次浏览 49人参与
# 长得好看会提高面试通过率吗? #
2687次浏览 41人参与
# MiniMax求职进展汇总 #
24679次浏览 313人参与
# 你做过最难的笔试是哪家公司 #
1039次浏览 18人参与
# HR最不可信的一句话是__ #
959次浏览 31人参与
# 沪漂/北漂你觉得哪个更苦? #
990次浏览 29人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7921次浏览 43人参与
# XX请雇我工作 #
51131次浏览 171人参与
# 简历中的项目经历要怎么写? #
310803次浏览 4252人参与
# 简历第一个项目做什么 #
32008次浏览 354人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152752次浏览 888人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187504次浏览 1123人参与
# AI时代,哪些岗位最容易被淘汰 #
64429次浏览 860人参与
# 如果重来一次你还会读研吗 #
229954次浏览 2011人参与
# 正在春招的你,也参与了去年秋招吗? #
364069次浏览 2640人参与
# 腾讯音乐求职进展汇总 #
160800次浏览 1114人参与
# 你怎么看待AI面试 #
180570次浏览 1291人参与
# 投格力的你,拿到offer了吗? #
178092次浏览 889人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务