面试题12:矩阵中的路径

package com.offer;

/**
 * @authore Xavier
 * @description // 面试题12:矩阵中的路径
 * // 题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有
 * // 字符的路径。路径可以从矩阵中任意一格开始,每一步可以在矩阵中向左、右、
 * // 上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入
 * // 该格子。例如在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字
 * // 母用下划线标出)。但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个
 * // 字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。
 * // A B T G
 * // C F C S
 * // J D E H
 * @date 2018/7/24
 */
public class StringPathInMatrix {

    static Boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
        if (matrix == null || rows < 1 || cols < 1 || str == null) {
            return false;
        }
        //用于存储当前的点是否已被访问过
        boolean[] visited = new boolean[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;
                }
            }
        }

        return false;
    }

    static boolean hasPathCore(char[] matrix, int rows, int cols, int row, int col, char[] str, int pathLength, boolean[] visited) {
        boolean hasPath = false;
        //当矩阵中坐标为(row,col)的格子和路路径与字符串下标为pathLength的字符一样时,从4个相邻的格子中去定位
        if (row >= 0 && row < rows && col >= 0 && col < cols && matrix[row * cols + col] == str[pathLength] && !visited[row * cols + col]) {
            ++pathLength;
            if (str.length == pathLength) {
                return true;
            }
            visited[row * cols + col] = true;
            hasPath = hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength, visited)
                    || 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);
            //如果相邻4个格子都没有匹配,则回到上一个字符,重新定位
            if (!hasPath) {
                --pathLength;
                visited[row * cols + col] = false;
            }
        }
        return hasPath;
    }
}

2.Junit

package com.offer;

import org.junit.Assert;
import org.junit.Test;

/**
 * @authore Xavier
 * @description
 * @date 2018/7/25
 */
public class StringPathInMatrixTest {

    @Test
    public void hasNoPath() throws Exception {
        char[] matrix = "ABTGCFCSJDEH".toCharArray();
        Assert.assertEquals(false, StringPathInMatrix.hasPath(matrix, 3, 4, "afcd".toCharArray()));
    }

    @Test
    public void hasPath() throws Exception {
        char[] matrix = "ABTGCFCSJDEH".toCharArray();
        Assert.assertEquals(true, StringPathInMatrix.hasPath(matrix, 3, 4, "ABFD".toCharArray()));
    }

    @Test
    public void hasNullPath() throws Exception {
        Assert.assertEquals(false, StringPathInMatrix.hasPath(null, 3, 4, "ABFD".toCharArray()));
    }

    @Test
    public void hasOneRowPath() throws Exception {
        Assert.assertEquals(true, StringPathInMatrix.hasPath("ABCD".toCharArray(), 1, 4, "ABCD".toCharArray()));
    }

    @Test
    public void hasOneColomnPath() throws Exception {
        Assert.assertEquals(true, StringPathInMatrix.hasPath("ABCD".toCharArray(), 4, 1, "ABCD".toCharArray()));
    }

}

猜你喜欢

转载自blog.csdn.net/huaixiaozi90/article/details/81204393