剑指offer:地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。

剑指offer算法题


数组 回溯

题目描述
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

题目分析

1.从(0,0)开始走,每成功走一步标记当前位置为true,然后从当前位置往四个方向探索,返回值为1 + 4 个方向的探索值之和。

2.探索时,判断当前节点是否可达的标准为:

1)当前节点在矩阵内;
2)当前节点未被访问过;
3)当前节点满足threshold限制。

下面是Java代码

public class Solution {
    public int movingCount(int threshold, int rows, int cols)
    {
        boolean[][] visited = new boolean[rows][cols];
        return movingCount(threshold,rows,cols,0,0,visited);
    }
    
    private int movingCount(int threshold,int rows,int cols,int r,int c, boolean[][] visited){
        
        if(r<0||r>=rows||c<0||c>=cols||visited[r][c]==true||bitSum(r)+bitSum(c)>threshold){
            return 0;
        }
        visited[r][c] = true;
        
        return movingCount(threshold,rows,cols,r-1,c,visited)+
               movingCount(threshold,rows,cols,r+1,c,visited)+
               movingCount(threshold,rows,cols,r,c-1,visited)+
               movingCount(threshold,rows,cols,r,c+1,visited)
               +1;
    }
    
    private int bitSum(int t){
        int count = 0;
        while(t!=0){
            count += t%10;
            t/=10;
        }
        return count;
    }
}

参考https://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8?tpId=13&&tqId=11219&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这道题可以使用回溯法来解决,时间复杂度为O(mn),空间复杂度为O(mn)。 具体实现思路如下: 1. 定义一个二维数组来表示这个方格,并初始化为0。 2. 从起点(0, 0)开始,进回溯搜索。 3. 在搜索的过程中,对于每个可的位置,我们需要检查它的坐标坐标数位之和是否小于等于k。如果小于等于k,则继续向下搜索;否则,返回上一层继续搜索。 4. 同时,我们需要记录已经搜索过的格子,以免重复搜索。可以使用一个布尔类型的二维数组来记录。 5. 最后,搜索结束后,我们可以统计一下已经搜索到的格子数,即为机器人能够到达的格子数。 下面是一个C语言的实现代码: ``` bool check(int i, int j, int k) // 判断坐标(i, j)的数位之和是否小于等于k { int sum = 0; while(i) { sum += i % 10; i /= 10; } while(j) { sum += j % 10; j /= 10; } return sum <= k; } int movingCountCore(int threshold, int rows, int cols, int i, int j, bool* visited) { int count = 0; if(i >= 0 && i < rows && j >= 0 && j < cols && !visited[i * cols + j] && check(i, j, threshold)) { visited[i * cols + j] = true; count = 1 + movingCountCore(threshold, rows, cols, i + 1, j, visited) + movingCountCore(threshold, rows, cols, i - 1, j, visited) + movingCountCore(threshold, rows, cols, i, j + 1, visited) + movingCountCore(threshold, rows, cols, i, j - 1, visited); } return count; } int movingCount(int threshold, int rows, int cols) { if(threshold < 0 || rows <= 0 || cols <= 0) return 0; bool *visited = new bool[rows * cols]; memset(visited, 0, rows * cols); int count = movingCountCore(threshold, rows, cols, 0, 0, visited); delete[] visited; return count; } ``` 其中,movingCountCore函数是回溯搜索的核心函数,movingCount函数是对外提供的接口函数。 时间复杂度分析:每个格子最多只会被访问一次,因此时间复杂度为O(mn)。 空间复杂度分析:需要一个布尔类型的二维数组来记录已经搜索过的格子,因此空间复杂度为O(mn)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值