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

测试用例:

  • 功能测试(方格为多行多列;k为正数)。
  • 边界值测试(方格只有一行或者只有一列;k等于0)。
  • 特殊输入测试(k为负数)。

测试代码:

void test(char* testName, int threshold, int rows, int cols, int expected)
{
if(testName != nullptr)
printf("%s begins: ", testName);
if(movingCount(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", 0, 1, 1, 1);
} // 机器人不能进入任意一个方格
void test9()
{
test("Test9", -10, 10, 10, 0);
}

本题考点:

  • 考查应聘者对回溯法的理解。通常物体或者人在二维方格运动这类问题都可以用回溯法解决。
  • 考查应聘者对数组的编程能力。我们一般都把矩阵看成一个二维的数组。只有对数组的特性充分了解,才有可能快速、正确地实现回溯法的代码编写。

实现代码:

#include <cstdio>

int movingCountCore(int threshold, int rows, int cols, int row, int col, bool* visited);
bool check(int threshold, int rows, int cols, int row, int col, bool* visited);
int getDigitSum(int number); int movingCount(int threshold, int rows, int cols)
{
if(threshold < 0 || rows <= 0 || cols <= 0)
return 0;
bool *visited = new bool[rows * cols];
for(int i = 0; i < rows * cols; ++i)
visited[i] = false;
int count = movingCountCore(threshold, rows, cols,
0, 0, visited);
delete[] visited;
return count;
} int movingCountCore(int threshold, int rows, int cols, int row,
int col, bool* visited)
{
int count = 0;
if(check(threshold, rows, cols, row, col, visited))
{
visited[row * cols + col] = true;
count = 1 + movingCountCore(threshold, rows, cols,
row - 1, col, visited)
+ movingCountCore(threshold, rows, cols,
row, col - 1, visited)
+ movingCountCore(threshold, rows, cols,
row + 1, col, visited)
+ movingCountCore(threshold, rows, cols,
row, col + 1, visited);
}
return count;
} bool check(int threshold, int rows, int cols, int row, int col,
bool* visited)
{
if(row >= 0 && row < rows && col >= 0 && col < cols
&& getDigitSum(row) + getDigitSum(col) <= threshold
&& !visited[row* cols + col])
return true;
return false;
} int getDigitSum(int number)
{
int sum = 0;
while(number > 0)
{
sum += number % 10;
number /= 10;
}
return sum;
}
int main(int agrc, char* argv[])
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
return 0;
}

最新文章

  1. Python中的条件判断和循环
  2. System V IPC(1)-消息队列
  3. C++ 中指针与引用的区别
  4. [stm32] Systick
  5. 捉虫记:SHGetSpecialFolderPath返回错误码为2
  6. 使用HighCharts实现实时数据展示
  7. bash中(),{},(()),[],[[]]的区别
  8. &quot;==&quot;和equals方法究竟有什么区别?
  9. 深入理解JVM(七)&mdash;&mdash;性能监控工具
  10. 编译安装httpd 2.4
  11. RSS简介
  12. 序列化、反序列化(Serializable特性)
  13. Node.js实战项目学习系列(1) 初识Node.js
  14. G面经Prepare: Longest All One Substring
  15. MySQL 内置函数
  16. JSF action actionListner 详解
  17. P2709 小B的询问(莫队入门)
  18. jQuery之jQuery扩展和事件
  19. Eclipse中代码格式化配置
  20. mysql 权限管理介绍

热门文章

  1. Windows Terminal 美化分享
  2. Power BI 的数据源及数据刷新
  3. Winform中实现拖拽文件到ListView获取文件类型(附代码下载)
  4. 平时常用sql
  5. opencv图像倾斜校正和切边
  6. gradle环境搭建
  7. 精通awk系列(1):安装新版本的gawk
  8. 天下代码一大抄,整个案例的搬是什么鬼!疑似冒充蚂蚁金服高级Java开发工程师?你大爷
  9. linux用户身份与文件权限
  10. leaflet 结合 Echarts4 实现统计图(附源码下载)