矩阵中的路径

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。 例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def __init__(self):
 4         self.visited = []
 5         
 6     def dfs(self,matrix,rows,cols,path,i,j,direction,k):
 7         if k >= len(path):
 8             return True
 9         if i < 0 or i >= rows or j < 0 or j >=cols:
10             return False
11         if self.visited[i][j] == 1:
12             return False
13         if matrix[i][j] == path[k]:
14             self.visited[i][j] = 1
15             for direct in direction:
16                 x = i + direct[0]
17                 y = j + direct[1]
18                 if self.dfs(matrix,rows,cols,path,x,y,direction,k+1):
19                     return True
20             self.visited[i][j] = 0
21         else:
22             return False
23     
24     def hasPath(self, matrix, rows, cols, path):
25         array = [[' ' for c in range(cols)]for r in range(rows)]
26         idx = 0
27         for i in range(rows):
28             for j in range(cols):
29                 cur = matrix[idx]
30                 idx += 1
31                 array[i][j] = cur
32 
33         direction = [[0,1],[0,-1],[1,0],[-1,0]]
34         self.visited = [[0 for c in range(cols)]for r in range(rows)]
35         for i in range(rows):
36             for j in range(cols):
37                 if self.dfs(array,rows,cols,path,i,j,direction,0):
38                     return True
39         return False
40         # write code here

 

补充java的实现:

 1 class Solution {
 2     int[][] visited;
 3 
 4     private boolean dfs(char[][] board, int rows, int cols, String path, int i, int j, int[][] direction, int k) {
 5         if (k >= path.length()) {
 6             return true;
 7         }
 8         if (i < 0 || i >= rows || j < 0 || j >= cols) {
 9             return false;
10         }
11         if (this.visited[i][j] == 1) {
12             return false;
13         }
14         if (board[i][j] == path.charAt(k)) {
15             this.visited[i][j] = 1;
16             for (int[] direct : direction) {
17                 int x = i + direct[0];
18                 int y = j + direct[1];
19                 if (this.dfs(board, rows, cols, path, x, y, direction, k + 1)) {
20                     return true;
21                 }
22             }
23             this.visited[i][j] = 0;
24         }
25         return false;
26     }
27 
28     public boolean exist(char[][] board, String word) {
29         int rows = board.length;
30         int cols = board[0].length;
31 
32         int[][] direction = new int[][] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
33         // System.out.println(direction.length);
34 
35         this.visited = new int[rows][cols];
36 
37         for (int i = 0; i < rows; i++) {
38             for (int j = 0; j < cols; j++) {
39                 if (this.dfs(board, rows, cols, word, i, j, direction, 0)) {
40                     return true;
41                 }
42             }
43         }
44 
45         return false;
46     }
47 }

 

posted on 2019-06-13 11:29  Sempron2800+  阅读(114)  评论(0编辑  收藏  举报