剑指offer Leetcode 13. 机器人的运动范围

image-20201125151603197

解法一:DFS

思想:

https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/solution/mian-shi-ti-13-ji-qi-ren-de-yun-dong-fan-wei-dfs-b/

其中证明了只要往右和下走就能访问所有可达解。

复杂度分析:

时间复杂度:O(MN),最差情况下,机器人遍历矩阵所有单元格,此时时间复杂度为O(MN)

空间复杂度:O(MN)

代码:
class Solution {
public:
    int movingCount(int m, int n, int k) {
        //visited数组,记录是否访问过,vector<bool>容易出问题,最好不要使用
        vector<vector<int>>visited(m, vector<int>(n, 0));
        //当前从0,0开始
        return dfs(m, n, 0, 0, k, visited);
    }
    int dfs(int row, int col, int cur_row, int cur_col, int k, vector<vector<int>>&visited){
        //如果访问越界或已经访问过,直接返回
        if(cur_col < 0 || cur_col >= col || cur_row < 0 || cur_row >= row || visited[cur_row][cur_col])
            return 0;
        //如果不满足和的条件,返回
        if(sumDigit(cur_col, cur_row) > k)
            return 0;
        //已访问
        visited[cur_row][cur_col] = 1;
        //遍历四个方向,其实向着右、下就可以
        return 1 + dfs(row, col, cur_row + 1, cur_col, k, visited)
                 + dfs(row, col, cur_row - 1, cur_col, k, visited)
                 + dfs(row, col, cur_row, cur_col + 1, k, visited)
                 + dfs(row, col, cur_row, cur_col - 1, k, visited);
    }
	
    //求和函数
    int sumDigit(int num1, int num2){
        int res = 0;
        while(num1){
            res += num1 % 10;
            num1 /= 10;
        }

        while(num2){
            res += num2 % 10;
            num2 /= 10;
        }
        return res;
    }
};

解法2:BFS

class Solution {
public:
    vector<pair<int, int>>dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    int movingCount(int m, int n, int k) {
        vector<vector<int>>visited(m, vector<int>(n, 0));
        queue<pair<int, int>>q;
        q.push({0, 0});
        int res = 0;
        visited[0][0] = 1;
        while(!q.empty()){
            auto front = q.front();
            q.pop();
            int x = front.first;
            int y = front.second;
            res += 1;
            for(auto d : dir){
                int new_x = x + d.first;
                int new_y = y + d.second;
                if(new_x < 0 || new_y < 0 || new_x >= m || new_y >= n || 
                    sumDigit(new_x, new_y) > k || visited[new_x][new_y] == 1)
                    continue;
                q.push({new_x, new_y});
                visited[new_x][new_y] = 1;
            }
        }
        return res;
    }

    //求和函数
    int sumDigit(int num1, int num2){
        int res = 0;
        while(num1){
            res += num1 % 10;
            num1 /= 10;
        }

        while(num2){
            res += num2 % 10;
            num2 /= 10;
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值