インタビュー質問12:マトリックス内のパスマトリックス内の
文字列のすべての文字を含むパスがあるかどうかを判断する関数を設計してください。
パスはマトリックス内の任意のグリッドから開始でき、各ステップで1つのグリッドをマトリックス内で左、右、上、下に移動できます。パスがマトリックスのグリッドを通過する場合、パスはグリッドに再び入ることはできません。
#include <cstdio>
#include <string>
#include <stack>
using namespace std;
bool hasPathCore(const char* matrix, int rows, int cols, int row,
int col, const char* str, int& pathLength, bool* visited)
{
// 此时路径字符串上的所有字符都在矩阵中找到合适的位置
if (str[pathLength] == '\0')
return true;
// 判断当前矩阵字符前,先将hasPath置为false
bool hasPath = false;
// 此时路径字符串上待判断的字符 == 当前矩阵字符 且 当前矩阵字符没有被使用
if (row >= 0 && row < rows && col >= 0 && col < cols
&& matrix[row * cols + col] == str[pathLength]
&& !visited[row * cols + col])
{
// 开始遍历路径字符串上下一个字符
++pathLength;
// 将刚才已确定的字符在布尔值矩阵置为true,防止下次遍历被重复使用
visited[row * cols + col] = true;
// 围绕着当前已确定的字符,开始判断上下左右四个方向新的矩阵字符
hasPath = hasPathCore(matrix, rows, cols, row, col - 1,
str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row - 1, col,
str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col + 1,
str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row + 1, col,
str, pathLength, visited);
// hasPath==0:已确定的字符的上下左右四个方向新的矩阵字符均不符合要求
// 证明当前路径不符合要求
// 只能回退到上一个字符重新遍历
if (!hasPath)
{
--pathLength;
visited[row * cols + col] = false;
}
}
return hasPath;
}
bool hasPath(const char* matrix, int rows, int cols, const char* str)
{
if (matrix == nullptr || rows < 1 || cols < 1 || str == nullptr)
return false;
// 布尔值矩阵:用来标识路径是否已经进入了每个格子
bool* visited = new bool[rows * cols];
memset(visited, 0, rows * cols);
int pathLength = 0;
// 以矩阵中每一个字符为起点而产生的路径与str进行匹配
for (int row = 0; row < rows; ++row)
{
for (int col = 0; col < cols; ++col)
{
if (hasPathCore(matrix, rows, cols, row, col, str,
pathLength, visited))
{
return true;
}
}
}
delete[] visited;
return false;
}
void Test(const char* testName, const char* matrix, int rows,
int cols, const char* str, bool expected)
{
if (testName != nullptr)
printf("%s begins: ", testName);
if (hasPath(matrix, rows, cols, str) == expected)
printf("Passed.\n");
else
printf("FAILED.\n");
}
//ABTG
//CFCS
//JDEH
//BFCE
void Test1()
{
const char* matrix = "ABTGCFCSJDEH";
const char* str = "BFCE";
Test("Test1", (const char*)matrix, 3, 4, str, true);
}
//ABCE
//SFCS
//ADEE
//SEE
void Test2()
{
const char* matrix = "ABCESFCSADEE";
const char* str = "SEE";
Test("Test2", (const char*)matrix, 3, 4, str, true);
}
int main()
{
Test1();
Test2();
return 0;
}
インタビュー質問13:ロボットの可動域
地面にm行n列の正方形があります。ロボットは座標のグリッド(0,0)から移動を開始します。ロボットは、一度に1つのグリッドを左、右、上、下に移動できますが、行と列の座標の合計がkより大きい。
たとえば、kが18の場合、ロボットは3 + 5 + 3 + 7 = 18であるため正方形(35,37)に入ることができますが、3 + 5 + 3 + 8 =であるため正方形(35,38)に入ることができません。 19。
ロボットはいくつのグリッドに到達できますか?
int getDigitSum(int number)
{
int sum = 0;
while (number > 0)
{
sum += number % 10;
number /= 10;
}
return sum;
}
int movingCountCore(int threshold, int rows, int cols, int row, int col, bool* visited)
{
int count = 0;
if (row >= 0 && row < rows && col >= 0 && col < cols
&& getDigitSum(row) + getDigitSum(col) <= threshold
&& !visited[row * cols + col])
{
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;
}
int movingCount(int threshold, int rows, int cols)
{
if (threshold < 0 || rows <= 0 || cols <= 0)
return 0;
bool* visited = new bool[rows * cols];
memset(visited, 0, rows * cols);
int count = movingCountCore(threshold, rows, cols, 0, 0, visited);
delete[] visited;
return count;
}
int main()
{
int step = movingCount(5, 10, 10);
cout << step << endl;
// 答案:21
return 0;
}