日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《剑指offer》第十三题(机器人的运动范围)

發布時間:2025/5/22 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《剑指offer》第十三题(机器人的运动范围) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 面試題:機器人的運動范圍 // 題目:地上有一個m行n列的方格。一個機器人從坐標(0, 0)的格子開始移動,它 // 每一次可以向左、右、上、下移動一格,但不能進入行坐標和列坐標的數位之和 // 大于k的格子。例如,當k為18時,機器人能夠進入方格(35, 37),因為3+5+3+7=18。 // 但它不能進入方格(35, 38),因為3+5+3+8=19。請問該機器人能夠到達多少個格子? #include <iostream>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);//從(0,0)開始檢測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);//那就加上1并繼續計算四鄰點 }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; }// ====================測試代碼==================== void test(const 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");elseprintf("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); }int main(int agrc, char* argv[]) {test1();test2();test3();test4();test5();test6();test7();test8();test9();system("pause");return 0; }

?

轉載于:https://www.cnblogs.com/CJT-blog/p/10480421.html

總結

以上是生活随笔為你收集整理的《剑指offer》第十三题(机器人的运动范围)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。