题目:
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
bool hasPath(char* matrix, int rows, int cols, char* str) {}
回溯
以下是我最初相当丑陋的代码
#include<iostream>
#include<string.h>
using namespace std;
int isfind[1000][1000];
bool helper(char* matrix, int rows, int cols, char* str, int i, int j)
{
if (matrix[i * cols + j] == str[0])
{
isfind[i][j] = 1;
if (str[1] == '\0')
{
return true;
}
bool r1 = false, r2 = false, r3 = false, r4 = false;
if (j - 1 >= 0 && isfind[i][j - 1] != 1)
{
r1 = helper(matrix,rows,cols,str+1,i,j-1);
if (r1 == true)
{
return true;
}
}
if (j + 1 < cols && isfind[i][j + 1] != 1)
{
r2 = helper(matrix, rows, cols, str + 1, i, j + 1);
if (r2 == true)
{
return true;
}
}
if (i - 1 >= 0 && isfind[i - 1][j] != 1)
{
r3 = helper(matrix, rows, cols, str + 1, i - 1, j);
if (r3 == true)
{
return true;
}
}
if (i + 1 < rows && isfind[i + 1][j] != 1)
{
r4 = helper(matrix, rows, cols, str + 1, i + 1, j);
if (r4 == true)
{
return true;
}
}
isfind[i][j] = 0;
return false;
}
else
{
return false;
}
}
bool hasPath(char* matrix, int rows, int cols, char* str)
{
if (strlen(str) == 0)
{
return false;
}
if (strlen(str) == 1 && strlen(matrix) == 1 && (*str) == (*matrix))
{
return true;
}
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (matrix[i*cols+j] == str[0])
{
isfind[i][j] = 1;
bool r1 = false, r2 = false, r3 = false, r4 = false;
if (j - 1 >= 0 && isfind[i][j - 1] != 1)
{
r1 = helper(matrix, rows, cols, str + 1, i, j - 1);
if (r1 == true)
{
return true;
}
}
if (j + 1 < cols && isfind[i][j + 1] != 1)
{
r2 = helper(matrix, rows, cols, str + 1, i, j + 1);
if (r2 == true)
{
return true;
}
}
if (i - 1 >= 0 && isfind[i - 1][j] != 1)
{
r3 = helper(matrix, rows, cols, str + 1, i - 1, j);
if (r3 == true)
{
return true;
}
}
if (i + 1 < rows && isfind[i + 1][j] != 1)
{
r4 = helper(matrix, rows, cols, str + 1, i + 1, j);
if (r4 == true)
{
return true;
}
}
memset(isfind, 0, sizeof(isfind) / sizeof(int));
}
}
}
return false;
}
int main()
{
memset(isfind, 0, sizeof(isfind) / sizeof(int));
char str[] = "A";
char s[] = "A";
if (hasPath(str, 1, 1, s))
{
cout << "true";
}
else
{
cout << "false";
}
}
这是改过的代码:
#include<iostream>
#include<string.h>
using namespace std;
int isfind[1000][1000];
bool helper(char* matrix, int rows, int cols, char* str, int i, int j)
{
if (*str == '\0')
{
return true;
}
if (i < 0 || i >= rows || j < 0 || j >= cols || matrix[i * cols + j] != *str || isfind[i][j] == 1)
{
return false;
}
isfind[i][j] = 1;
if (helper(matrix, rows, cols, str + 1, i - 1, j) ||
helper(matrix, rows, cols, str + 1, i + 1, j) ||
helper(matrix, rows, cols, str + 1, i, j - 1) ||
helper(matrix, rows, cols, str + 1, i, j + 1))
{
return true;
}
isfind[i][j] = 0;
return false;
}
bool hasPath(char* matrix, int rows, int cols, char* str)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (helper(matrix,rows,cols,str,i,j))
{
return true;
}
}
}
return false;
}
int main()
{
memset(isfind, 0, sizeof(isfind) / sizeof(int));
char str[] = "A";
char s[] = "A";
if (hasPath(str, 1, 1, s))
{
cout << "true";
}
else
{
cout << "false";
}
}
这样好多了。