剑指offer:矩阵中的路径(Java、Python)

补上自己上周做的题,上周忘记写了,希望不会忘记。。。
决定从今往后先用Java解题了,Python实现有的时候太轻松,轻松的不真实;而且Python无论是对数据结构的封装还是操作,来的太容易;作为一个主体语言依然用Java的我而言,还是先精通Java吧~;这道题是牛客网-剑指offer中倒数第二道题,同时附上Java和Python的解法。可能是用Python太多,感觉思路有点偏Python的思路…

题目描述

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

解题思路

上述例子所组成的矩阵样式:

a b c e
s f c s
a d e e

分别以数组中的每个元素为起点,判断从该起点开始,是否包含给定的字符串的路径。方法采用递归即可。不难;我看一些是说用回溯法,和递归一个样。

Java版

自己在实现的过程中需要测试代码,因为main函数是静态的,因此实现的方法也就是静态的了;懒得删,就一起放上来了:

import java.util.Arrays;
public class MatrixPath {
    public static boolean hasPath(char[] matrix, int rows, int cols, char[] str){
            boolean[] assistMatrix = new boolean[rows*cols];
            Arrays.fill(assistMatrix, true);
            for(int i=0; i<rows; i++) {
                for(int j=0; j<cols; j++) {
                    if(hasPathAtAStartPoint(matrix, rows, cols, str, i, j, assistMatrix)) {
                        return true;
                    }
                }
            }
            return false;
    }

    public static boolean hasPathAtAStartPoint(char[] matrix, int rows, int cols, char[] str, int row, int col, boolean[] assistMatrix) {
            if(str.length <= 0) {
                return true;
            }
            int index = row*cols + col;
            if(row<0 || row>=rows ||col<0 ||col>=cols || assistMatrix[index]==false || matrix[index] != str[0]) {
                return false;
            }
            assistMatrix[index] = false;
            char[] strRest = Arrays.copyOfRange(str, 1, str.length);
            if(hasPathAtAStartPoint(matrix,rows,cols,strRest,row+1,col,assistMatrix)||
                    hasPathAtAStartPoint(matrix,rows,cols,strRest,row-1,col,assistMatrix)||
                    hasPathAtAStartPoint(matrix,rows,cols,strRest,row,col+1,assistMatrix)||
                    hasPathAtAStartPoint(matrix,rows,cols,strRest,row,col-1,assistMatrix)) {
                return true;
            }
            assistMatrix[index] = true;
            return false;
    }
//    A B C E
//    S F C S
//    A D E F

    public static void main(String[] args) {
            char[] matrix = {'A','B','C','E','S','F','C','S','A','D','E','F'};
            char[] str = {'A','B','C','C','E','D'};
            System.out.println(hasPath(matrix,3,4,str));
    }
}

Python版

class Solution:
    def hasPath(self, matrix, rows, cols, path):
        assistMatrix = [True]*rows*cols
        for i in range(rows):
            for j in range(cols):
                if(self.hasPathAtAStartPoint(matrix,rows,cols, i, j, path, assistMatrix)):
                    return True
        return False

    def hasPathAtAStartPoint(self, matrix, rows, cols, i, j, path, assistMatrix):
        if not path:
            return True
        index = i*cols+j
        if i<0 or i>=rows or j<0 or j>=cols or matrix[index]!=path[0] or assistMatrix[index]==False:
            return False
        assistMatrix[index] = False
        if(self.hasPathAtAStartPoint(matrix,rows,cols,i+1,j,path[1:],assistMatrix) or
               self.hasPathAtAStartPoint(matrix,rows,cols,i-1,j,path[1:],assistMatrix) or
               self.hasPathAtAStartPoint(matrix,rows,cols,i,j-1,path[1:],assistMatrix) or
               self.hasPathAtAStartPoint(matrix,rows,cols,i,j+1,path[1:],assistMatrix)):
            return True
        assistMatrix[index] = True
        return False

猜你喜欢

转载自blog.csdn.net/u010005281/article/details/80412415
今日推荐