• 面试题13:机器人的运动范围(回溯法)


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

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

    一、算法实现 

    该方格看作一个 m x n 的矩阵,在这个矩阵中,除边界上的格子之外,其他格子都有4个相邻的格子。

    机器人从坐标(0,0)开始移动。当它准备进入坐标为(i,j)的格子时,通过检查坐标的数位和来判断机器人是否能够进入。如果机器人能够进入坐标为(i,j)的格子,则再判断它能否进入4个相邻的格子(i,j-1)、(i-1,j)、(i,j+1)、(i+1,j)。

    因此,我们用如下的代码来实现回溯算法

    1. int movingCount(int threshold, int rows, int cols)//限定大小k,矩阵行数,列数
    2. {
    3. if (threshold < 0 || rows <= 0 || cols <= 0)//判断所给前提条件是否可行
    4. return 0;
    5. bool* visited = new bool[rows * cols];//监视矩阵,确保每个格子不会重复进入
    6. for (int i = 0; i < rows * cols; ++i)
    7. visited[i] = false;
    8. int count = movingCountCore(threshold, rows, cols,
    9. 0, 0, visited);
    10. delete[] visited;//释放空间
    11. return count;
    12. }
    1. int movingCountCore(int threshold, int rows, int cols, int row,
    2. int col, bool* visited)//统计总共能进入格子的个数
    3. {
    4. int count = 0;
    5. if (check(threshold, rows, cols, row, col, visited))//判断该格子是否满足条件
    6. {
    7. visited[row * cols + col] = true;//标记该格子,避免再次进入
    8. count = 1 + movingCountCore(threshold, rows, cols,
    9. row - 1, col, visited)
    10. + movingCountCore(threshold, rows, cols,
    11. row, col - 1, visited)
    12. + movingCountCore(threshold, rows, cols,
    13. row + 1, col, visited)
    14. + movingCountCore(threshold, rows, cols,
    15. row, col + 1, visited);
    16. }
    17. return count;
    18. }

    下面的函数check用来判断机器人能否进入坐标为(row,col)的方格,而函数getDigitSum用来得到一个数字的数位之和。

    1. bool check(int threshold, int rows, int cols, int row, int col,
    2. bool* visited)//判断当前格子是否满足行,列的数字按位相加不大于限定数字k,并且该格子是第一次进入
    3. {
    4. if (row >= 0 && row < rows && col >= 0 && col < cols
    5. && getDigitSum(row) + getDigitSum(col) <= threshold
    6. && !visited[row * cols + col])
    7. return true;
    8. return false;
    9. }
    1. int getDigitSum(int number)//实现数字按位相加
    2. {
    3. int sum = 0;
    4. while (number > 0)
    5. {
    6. sum += number % 10;
    7. number /= 10;
    8. }
    9. return sum;
    10. }

    二、测试代码 

    1. // ====================测试代码====================
    2. void test(char* testName, int threshold, int rows, int cols, int expected)
    3. {
    4. if (testName != nullptr)
    5. printf("%s begins: ", testName);
    6. if (movingCount(threshold, rows, cols) == expected)
    7. printf("Passed.\n");
    8. else
    9. printf("FAILED.\n");
    10. }
    11. // 方格多行多列
    12. void test1()
    13. {
    14. test("Test1", 5, 10, 10, 21);
    15. }
    16. // 方格多行多列
    17. void test2()
    18. {
    19. test("Test2", 15, 20, 20, 359);
    20. }
    21. // 方格只有一行,机器人只能到达部分方格
    22. void test3()
    23. {
    24. test("Test3", 10, 1, 100, 29);
    25. }
    26. // 方格只有一行,机器人能到达所有方格
    27. void test4()
    28. {
    29. test("Test4", 10, 1, 10, 10);
    30. }
    31. // 方格只有一列,机器人只能到达部分方格
    32. void test5()
    33. {
    34. test("Test5", 15, 100, 1, 79);
    35. }
    36. // 方格只有一列,机器人能到达所有方格
    37. void test6()
    38. {
    39. test("Test6", 15, 10, 1, 10);
    40. }
    41. // 方格只有一行一列
    42. void test7()
    43. {
    44. test("Test7", 15, 1, 1, 1);
    45. }
    46. // 方格只有一行一列
    47. void test8()
    48. {
    49. test("Test8", 0, 1, 1, 1);
    50. }
    51. // 机器人不能进入任意一个方格
    52. void test9()
    53. {
    54. test("Test9", -10, 10, 10, 0);
    55. }
    56. int main(int agrc, char* argv[])
    57. {
    58. test1();
    59. test2();
    60. test3();
    61. test4();
    62. test5();
    63. test6();
    64. test7();
    65. test8();
    66. test9();
    67. return 0;
    68. }
  • 相关阅读:
    C++STL——string类与模拟实现
    华为FPGA工程师面试题
    【智能合约】 函数动态调用-call函数和fallback函数
    Linux基础命令
    python基于PHP+MySQL的在线考试系统
    通过C++反射实现通过配置类名动态选择执行对象
    java: 自定义java.util.logging.Logger的日志输出格式,输出IDE(ECLIPSE)能自动识别行号的格式
    用Python计算点估计预测评价指标(误差指标RMSE、MSE、MAE、MAPE) ,画图展示
    【Linux】进程控制 —— 进程创建/终止/等待
    wpf DataGridComboBoxColumn 如何显示一个多列的下拉框?_成就一亿技术人!
  • 原文地址:https://blog.csdn.net/weixin_59179454/article/details/127623798