矩阵中的路径
- 参与人数:983时间限制:1秒空间限制:32768K
- 算法知识视频讲解
题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
分析:回溯算法
这是一个可以用回朔法解决的典型题。首先,在矩阵中任选一个格子作为路径的起点。如果路径上的第i个字符不是ch,那么这个格子不可能处在路径上的
第i个位置。如果路径上的第i个字符正好是ch,那么往相邻的格子寻找路径上的第i+1个字符。除在矩阵边界上的格子之外,其他格子都有4个相邻的格子。
重复这个过程直到路径上的所有字符都在矩阵中找到相应的位置。
由于回朔法的递归特性,路径可以被开成一个栈。当在矩阵中定位了路径中前n个字符的位置之后,在与第n个字符对应的格子的周围都没有找到第n+1个
字符,这个时候只要在路径上回到第n-1个字符,重新定位第n个字符。
由于路径不能重复进入矩阵的格子,还需要定义和字符矩阵大小一样的布尔值矩阵,用来标识路径是否已经进入每个格子。 当矩阵中坐标为(row,col)的
格子和路径字符串中相应的字符一样时,从4个相邻的格子(row,col-1),(row-1,col),(row,col+1)以及(row+1,col)中去定位路径字符串中下一个字符
如果4个相邻的格子都没有匹配字符串中下一个的字符,表明当前路径字符串中字符在矩阵中的定位不正确,我们需要回到前一个,然后重新定位。
一直重复这个过程,直到路径字符串上所有字符都在矩阵中找到合适的位置
// 58.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <memory>
class Solution {
public:
bool hasPath(char* matrix, int rows, int cols, char* str) {
if (matrix == NULL || rows < 1 || cols < 1 || str == NULL) return false;
bool* visited = new bool[rows * cols];
memset(visited, 0, rows * cols);
int pathLength = 0;
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;
}
bool hasPathCore(char* matrix, int rows, int cols, int row, int col, char* str, int& pathLength, bool* visited) {
if (str[pathLength] == '\0') return true;
if (pathLength == strlen(str)) return true;
if (row<0 || row >= rows || col<0 || col >= cols) return false;
int index = col + row*cols;
bool result = false;
if (!visited[index] && matrix[index] == str[pathLength])
{
visited[index] = true;
result = hasPathCore(matrix, rows, cols, row - 1, col, 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, col - 1, str, ++pathLength, visited);
if (result == false)
visited[index] = false;
}
if (result)
return true;
return false;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
char test[12] = { 'a', 'b', 'c', 'e', 's', 'f', 'c', 's', 'a', 'd', 'e', 'e' };
Solution s;
bool result = s.hasPath(test, 3, 4, "bcced");
char* p = new char;
return 0;
}
第二次做:
我觉得这道题就是一个尾递归
class Solution {
public:
bool hasPath(char* matrix, int rows, int cols, char* str) {
if ( matrix == NULL || rows < 1 || cols < 1 || str == NULL ) return false ;
int pathLength = 0 ;
bool* visited = new bool[ rows * cols ] ;
memset( visited, 0, rows * cols ) ;
bool rc = false ;
for ( int i = 0; i < rows; ++ i ) {
for ( int j = 0; j < cols; ++ j ) {
if ( hasPathCore (matrix, rows, cols, i, j, str, pathLength, visited ) ) {
rc = true ;
goto done ;
}
}
}
error :
goto done ;
done :
delete[] visited ;
return rc ;
}
bool hasPathCore( char* matrix, int rows, int cols, int row, int col, char* str, int pathLength, bool* visited ) {
if ( str[pathLength] == '\0' ) return true ;
if ( pathLength == strlen(str) ) return true ;
if ( row > rows || col > cols || row < 0 || col < 0 ) return false ;
bool result = false ;
if ( visited[row * cols + col] == false && str[pathLength] == matrix[row * cols + col] ) {
pathLength ++ ;
visited[row * cols + col] = true ;
result = ( hasPathCore( matrix, rows, cols, row + 1, col, 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, col - 1, str, pathLength, visited ) ) ;
if ( result == false ) {
visited[row * cols + col] = false ;
}
}
return result ;
}
};
第三次做:
class Solution {
public:
bool hasPath(char* matrix, int rows, int cols, char* str) {
if ( matrix == NULL || rows < 0 || cols < 0 || str == NULL ) return false ;
bool* visited = new bool[ rows * cols ] ;
memset( visited, 0, rows * cols ) ;
int row = 0 ;
int col = 0 ;
int pathLength = 0 ;
bool result = false ;
for ( int i = 0; i < rows; ++ i ) {
for ( int j = 0; j < cols; ++ j ) {
if ( hasPathCore( matrix, rows, cols, i, j, str, pathLength, visited ) ) {
result = true ;
goto done ;
}
}
}
error :
goto done ;
done :
delete[] visited ;
return result ;
}
bool hasPathCore( char* matrix, int rows, int cols, int row, int col, char* str, int pathLength, bool* visited ) {
if ( str[pathLength] == '\0' ) return true ;
if ( pathLength == strlen( str ) ) return true ;
if ( row > rows || row < 0 || col > cols || col < 0 ) return false ;
bool result = false ;
if ( visited[row * cols + col] == false && str[pathLength] == matrix[row * cols + col] ) {
visited[row * cols + col] = true ;
++ pathLength ;
result = ( hasPathCore( matrix, rows, cols, row + 1, col, 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, col - 1, str, pathLength, visited ) ) ;
if ( result == false ) {
-- pathLength ;
visited[row * cols + col] = false ;
}
}
return result ;
}
};