日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

發(fā)布時(shí)間:2025/5/22 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《剑指offer》第十三题(机器人的运动范围) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// 面試題:機(jī)器人的運(yùn)動(dòng)范圍 // 題目:地上有一個(gè)m行n列的方格。一個(gè)機(jī)器人從坐標(biāo)(0, 0)的格子開始移動(dòng),它 // 每一次可以向左、右、上、下移動(dòng)一格,但不能進(jìn)入行坐標(biāo)和列坐標(biāo)的數(shù)位之和 // 大于k的格子。例如,當(dāng)k為18時(shí),機(jī)器人能夠進(jìn)入方格(35, 37),因?yàn)?+5+3+7=18。 // 但它不能進(jìn)入方格(35, 38),因?yàn)?+5+3+8=19。請問該機(jī)器人能夠到達(dá)多少個(gè)格子? #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)//每個(gè)值得檢查的參數(shù)都應(yīng)該被檢查return 0;bool *visited = new bool[rows * cols];//用來檢測這個(gè)點(diǎn)有沒有走過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并繼續(xù)計(jì)算四鄰點(diǎn) }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])//只有當(dāng)滿足邊界條件,滿足題設(shè),并且沒有走過的點(diǎn)才被允許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); }// 方格只有一行,機(jī)器人只能到達(dá)部分方格 void test3() {test("Test3", 10, 1, 100, 29); }// 方格只有一行,機(jī)器人能到達(dá)所有方格 void test4() {test("Test4", 10, 1, 10, 10); }// 方格只有一列,機(jī)器人只能到達(dá)部分方格 void test5() {test("Test5", 15, 100, 1, 79); }// 方格只有一列,機(jī)器人能到達(dá)所有方格 void test6() {test("Test6", 15, 10, 1, 10); }// 方格只有一行一列 void test7() {test("Test7", 15, 1, 1, 1); }// 方格只有一行一列 void test8() {test("Test8", 0, 1, 1, 1); }// 機(jī)器人不能進(jìn)入任意一個(gè)方格 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; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/CJT-blog/p/10480421.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。