每日一题:机器人的运动范围

剑指Offer上的原题
地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。但它不能进入方格 [35, 38],因为3+5+3+8=19。请问该机器人能够到达多少个格子?

示例1:
输入:m = 2, n = 3, k = 1
输出:3
示例2:
输入:m = 3, n = 1, k = 0
输出:1

方法一:BFS

话不多说直接BFS开搜。

class Solution {
public:
    int movingCount(int m, int n, int k) {
        queue<pair<int, int> > bfs;
        bfs.push(make_pair(0, 0));
        int reached[m][n];
        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                reached[i][j] = 0;
            }
        }
        int addition_x[] = {0, 0, -1, 1};
        int addition_y[] = {1, -1, 0, 0};
        int countCell = 0;
        while (!bfs.empty()){
            int currentX = bfs.front().first, currentY = bfs.front().second;
            bfs.pop();
            if (reached[currentX][currentY] == 0){
                reached[currentX][currentY] = 1;
                countCell++;
                // cout << countCell << endl;
            }
            else if (reached[currentX][currentY] == 1){
                continue;
            }
            for (int i = 0; i < 4; i++){
                int newX = currentX + addition_x[i], newY = currentY + addition_y[i];
                if (newX < 0 || newX >= m || newY < 0 || newY >= n || reached[newX][newY] == 1){
                    continue;
                }
                else{
                    int digitSum = 0, tempX = newX, tempY = newY;
                    for (; newX; digitSum += newX % 10, newX /= 10);
                    for (; newY; digitSum += newY % 10, newY /= 10);
                    if (digitSum <= k) bfs.push(make_pair(tempX, tempY));
                }
            }
        }
        return countCell;
    }
};
方法二:回溯法
class Solution {
public:
    int movingCount(int m, int n, int k) {
        if (k < 0 || m <= 0 || n <= 0) return 0;
        bool visited[m*n];
        for (int i = 0; i < m*n; i++) {
            visited[i] = false;
        }
        int count = this->movingCountCore(k, m, n, 0, 0, visited);
        return count;
    }

    // 回溯算法核心
    int movingCountCore(int k, int m, int n, int startRow, int startCol, bool* visited) {
        int count = 0;
        if (check(k, m, n, startRow, startCol, visited)) {
            visited[startRow*n+startCol] = true;
            count = 1 + movingCountCore(k, m, n, startRow - 1, startCol, visited) + \
                        movingCountCore(k, m, n, startRow, startCol - 1, visited) + \
                        movingCountCore(k, m, n, startRow + 1, startCol, visited) + \
                        movingCountCore(k, m, n, startRow, startCol + 1, visited);
        }
        return count;
    }

    // check函数用来判断一个机器人能否进入一个方格
    bool check(int k, int rows, int cols, int row, int col, bool* visited) {
        if (row >= 0 && row < rows && col >= 0 && col < cols && getDigitSum(row) + getDigitSum(col) <= k && !visited[row*cols+col]) {
            return true;
        }
        return false;
    }

    // 得到一个数字的数位和
    int getDigitSum(int number) {
        int digitSum = 0;
        for (; number; digitSum += number % 10, number /= 10);
        return digitSum;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值