67 - 机器人的移动范围

题目:
地上有个m行n列的方格。一个机器人从坐标(0,0)的格子开始移动,它每一次可以向左、右、上、下移动一格,但不能进入行坐标和列坐标的数位之和大于k的格子。

  例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7=18.但它不能进入方格(35,38),因为3+5+3+8=19.请问该机器人能够达到多少格子?


和上一题66 - 矩阵中的路径类似,只是约束条件发生了改变。
当机器人在某一个格子时,它能够向上下左右四个方向运动,当访问周围的格子时,标记为已访问,判断该格子坐标是否满足要求,满足则计数加 1,否则计数加 0. 当遍历完所有格子时,计数完毕。

bool CheckNum(int row, int col, int threshold); // 检查坐标是否满足要求
int MoveCountsCore(int rows, int cols, int row, int col, bool* visited, int threshold);

int MoveCounts(int threshold, int rows, int cols) {
    if (rows < 1 || cols < 1 || threshold < 0) {
        return 0;
    }
    bool* visited = new bool [rows*cols]; //访问记录
    for (int i = 0; i < rows*cols; i++)
        visited[i] = false;
    int counts = MoveCountsCore(rows, cols, 0, 0, visited, threshold);
    delete []visited;
    return counts;
}

int MoveCountsCore(int rows, int cols, int row, int col, bool* visited, int threshold) {
    if (rows < 1 || cols < 1 || row < 0 || col < 0 || threshold < 0)
        return 0;
    if (row < rows && col < cols && !visited[row*cols + col] && CheckNum(row, col, threshold)) {
        visited[row*cols + col] = true;
        return 1 + MoveCountsCore(rows, cols, row-1, col, visited, threshold) +
                         MoveCountsCore(rows, cols, row, col-1, visited, threshold) +
                         MoveCountsCore(rows, cols, row+1, col, visited, threshold) +
                         MoveCountsCore(rows, cols, row, col+1, visited, threshold);
    }
    return 0;
}

// 检查坐标是否,满足要求
bool CheckNum(int row, int col, int threshold) {
    if (row < 0 || col < 0 || threshold< 0)
        return false;
    int bit_sum = 0;
    while (row != 0) {
        bit_sum += row % 10;
        row /= 10;
    }
    while (col != 0) {
        bit_sum += col % 10;
        col /= 10;
    }
    if (bit_sum <= threshold)
        return true;
    else
        return false;
}

测试样例:
From 《剑指offer》 GitHub


// ================================ test code ================================
void test(char* testName, int threshold, int rows, int cols, int expected)
{
    if(testName != NULL)
        printf("%s begins: ", testName);

    if(MoveCounts(threshold, rows, cols) == expected)
        printf("Passed.\n");
    else
        printf("FAILED.\n");
}

void test1()
{
    test("Test1", 5, 10, 10, 21);
}

void test2()
{
    test("Test2", 15, 20, 20, 359);
}

void test3() 
{
    test("Test3", 10, 1, 100, 29);
}

void test4() 
{
    test("Test4", 10, 1, 10, 10);
}

void test5()
{
    test("Test5", 15, 100, 1, 79);
}

void test6()
{
    test("Test6", 15, 10, 1, 10);
}

void test7()
{
    test("Test7", 15, 1, 1, 1);
}

void test8()
{
    test("Test8", -10, 10, 10, 0);
}

int main(int agrc, char* argv[])
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<param name="laser_likelihood_max_dist" value="2.0"/><!--机器人激光雷达数据与地图之间的匹配程度,机器人激光雷达数据在地图上搜索的最大距离--> <param name="update_min_d" value="0.1"/><!--机器人定位算法中的更新频率,移动的距离超过值后雷达进一次更新--> <param name="update_min_a" value="0.1"/><!--机器人定位算法中的更新频率,移动的弧度超过这个值后雷达进一次更新--> <param name="resample_interval" value="2"/><!--根据粒子的权重对粒子进重新采样的频率--> <param name="transform_tolerance" value="0.1"/><!--机器人在进位姿变换时,允许的最大误差范围--> <param name="recovery_alpha_slow" value="0.0"/><!--慢速恢复(slow recovery)过程中的旋转速度,运动过程中出现了位姿误差较大的情况,机器人定位算法会尝试通过慢速旋转来调整机器人的位姿,以减小位姿误差的影响,一般在0.1-0.5之间,--> <param name="recovery_alpha_fast" value="0.1"/><!--快速恢复(slow recovery)过程中的旋转速度,运动过程中出现了位姿误差较大的情况,机器人定位算法会尝试通过快速旋转来调整机器人的位姿,以减小位姿误差的影响,一般在0.1-0.5之间,--> <param name="use_map_topic" value="true"/><!--这个参数用于控制机器人定位算法是否使用地图话题来更新机器人的位姿--> <param name="first_map_only" value="false"/><!--控制机器人定位算法是否只使用第一次接收到的地图信息--> <param name="tf_broadcast" value="true"/> <param name="odom_frame_id" value="odom"/> <param name="global_frame_id" value="map"/> <param name="base_frame_id" value="base_footprint"/> <param name="odom_model_type" value="diff"/> <param name="initial_pose_x" value="$(arg init_x)"/> <param name="initial_pose_y" value="$(arg init_y)"/> <param name="initial_pose_a" value="$(arg init_a)"/> <param name="initial_cov_xx" value="0.25" /> <param name="initial_cov_yy" value="0.25" /> <param name="initial_cov_aa" value="0.2" />
最新发布
06-01

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值