矩阵中的路径
2015-08-05 10:08
183 查看
题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。#include <iostream> using namespace std; bool hasPathCore(char* matrix, int rows, int cols, int row, int col, int pathLength, char* str,bool* visited) { bool hasPath = false; if(str[pathLength] == '\0') { return true; } if (row >= 0 && row < rows && col >=0 && col < cols && !visited[row * cols + col] && str[pathLength] == matrix[row * cols +col]) { ++pathLength; visited[row * cols + col] = true; hasPath = hasPathCore( matrix, rows, cols, row-1, col, pathLength, str, visited) ||hasPathCore( matrix, rows, cols, row, col-1, pathLength, str, visited) ||hasPathCore( matrix, rows, cols, row+1, col, pathLength, str, visited) ||hasPathCore( matrix, rows, cols, row, col+1, pathLength, str, visited); if (!hasPath) { --pathLength; visited[row * cols + col] = false; } } return hasPath; } bool hasPath(char* matrix, int rows, int cols, char* str) { int i,row,col; if(matrix == NULL && rows < 1 && cols < 1 && str == NULL) return false; bool* visited = new bool[rows * cols]; //将visited指向的某块内存空间的前rows*cols字节的内容全部设置为0的ASCII值 //memset(visited, 0, rows * cols); for (i = 0; i < rows * cols; i++) { visited[i] = false; } int pathLength = 0; for(row = 0; row < rows; row++) { for (col = 0; col < cols; col++) { if(hasPathCore(matrix, rows, cols, row, col, pathLength, str, visited)) { return true; } } } delete[] visited; return false; } void Test(char* testName, char* matrix, int rows, int cols, char* str, bool expected) { if(testName != NULL) printf("%s begins: ", testName); if(hasPath(matrix, rows, cols, str) == expected) printf("Passed.\n"); else printf("FAILED.\n"); } //ABCE //SFCS //ADEE //ABCCED void Test1() { char matrix[] = "ABCESFCSADEE"; char* str = "ABCCED"; Test("Test1", (char*)matrix, 3, 4, str, true); } //ABCE //SFCS //ADEE //SEE void Test2() { char matrix[] = "ABCESFCSADEE"; char* str = "SEE"; Test("Test2", (char*)matrix, 3, 4, str, true); } //ABCE //SFCS //ADEE //ABCB void Test3() { char matrix[] = "ABCESFCSADEE"; char* str = "ABCB"; Test("Test3", (char*)matrix, 3, 4, str, false); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SLHECCEIDEJFGGFIE void Test4() { char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; char* str = "SLHECCEIDEJFGGFIE"; Test("Test4", (char*)matrix, 5, 8, str, true); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SGGFIECVAASABCEHJIGQEM void Test5() { char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; char* str = "SGGFIECVAASABCEHJIGQEM"; Test("Test5", (char*)matrix, 5, 8, str, true); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SGGFIECVAASABCEEJIGOEM void Test6() { char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; char* str = "SGGFIECVAASABCEEJIGOEM"; Test("Test6", (char*)matrix, 5, 8, str, false); } //ABCEHJIG //SFCSLOPQ //ADEEMNOE //ADIDEJFM //VCEIFGGS //SGGFIECVAASABCEHJIGQEMS void Test7() { char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS"; char* str = "SGGFIECVAASABCEHJIGQEMS"; Test("Test7", (char*)matrix, 5, 8, str, false); } //AAAA //AAAA //AAAA //AAAAAAAAAAAA void Test8() { char matrix[] = "AAAAAAAAAAAA"; char* str = "AAAAAAAAAAAA"; Test("Test8", (char*)matrix, 3, 4, str, true); } //AAAA //AAAA //AAAA //AAAAAAAAAAAAA void Test9() { char matrix[] = "AAAAAAAAAAAA"; char* str = "AAAAAAAAAAAAA"; Test("Test9", (char*)matrix, 3, 4, str, false); } //A //A void Test10() { char matrix[] = "A"; char* str = "A"; Test("Test10", (char*)matrix, 1, 1, str, true); } //A //B void Test11() { char matrix[] = "A"; char* str = "B"; Test("Test11", (char*)matrix, 1, 1, str, false); } int main(int argc, char* argv[]) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7(); Test8(); Test9(); Test10(); Test11(); return 0; }
相关文章推荐
- C语言中的volatile关键字
- org.hibernate.util.ReflectHelper.getConstructor 空指针异常
- 理解SVG坐标系统和变换: transform属性
- Can you find it?
- Java 反射机制
- js继承如何实现继承?
- 上传大文件问题 413 (Request Entity Too Large)
- Program received signal SIGUSR1, User defined signal 1.解决方法
- linux环境安装Nginx
- [乐意黎原创] php 页面提示 Fatal error: Call to undefined function curl_init()
- HTML 列表中的dl,dt,dd,ul,li,ol区别及应用
- JSP基本语法
- Java多线程——<三>简单的线程执行:Executor
- velocity问题积累
- 南邮 OJ 1151 Find Your Present
- 开启“自修”模式
- 南邮 OJ 1150 整除的尾数
- JavaScript的作用域与作用域链
- for_each的用法
- JavaScript 常用方法总结