• 017 矩阵中的路径


    1.题目  

      请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。

      路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。

      如果一条路径经过了矩阵中的某一个格子,则之后不能再次进入这个格子。

      例如 a b c e s f c s a d e e 这样的3 X 4 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

    2.思路

      分析:回溯算法
      这是一个可以用回朔法解决的典型题。首先,在矩阵中任选一个格子作为路径的起点。如果路径上的第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个相邻的格子都没有匹配字符串中下一个的字符,表明当前路径字符串中字符在矩阵中的定位不正确,我们需要回到前一个,然后重新定位。
      一直重复这个过程,直到路径字符串上所有字符都在矩阵中找到合适的位置。
     
    3.程序
      1 package first;
      2 
      3 public class HasPath {
      4     public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
      5         if (matrix == null || rows < 1 || cols < 1 || str == null) {
      6             return false;
      7         }
      8         boolean[] isVisited = new boolean[rows * cols];
      9         for (boolean v : isVisited) {
     10             v = false;
     11         }
     12         int pathLength = 0;
     13         for (int row = 0; row < rows; row++) {
     14             for (int col = 0; col < cols; col++) {
     15                 if (hasPathCore(matrix, rows, cols, row, col, str, pathLength, isVisited))
     16                     return true;
     17             }
     18         }
     19         return false;
     20     }
     21 
     22     private boolean hasPathCore(char[] matrix, int rows, int cols, int row, int col, char[] str, int pathLength, boolean[] isVisited) {
     23         if (row < 0 || col < 0 || row >= rows || col >= cols || isVisited[row * cols + col] == true || str[pathLength] != matrix[row * cols + col])
     24             return false;
     25         if (pathLength == str.length - 1)
     26             return true;
     27         boolean hasPath = false;
     28         isVisited[row * cols + col] = true;
     29         hasPath = hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength + 1, isVisited)
     30                 || hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength + 1, isVisited)
     31                 || hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength + 1, isVisited)
     32                 || hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength + 1, isVisited);
     33 
     34         if (!hasPath) {
     35             isVisited[row * cols + col] = false;
     36         }
     37         return hasPath;
     38     }
     39 
     40     // =======测试代码========
     41 
     42     // A B T G
     43     // C F C S
     44     // J D E H
     45 
     46     // BFCTB
     47     public void test1() {
     48         char[] matrix = "ABTGCFCSJDEH".toCharArray();
     49         int rows = 3;
     50         int cols = 4;
     51         char[] str = "BFCTB".toCharArray();
     52         if (!hasPath(matrix, rows, cols, str))
     53             System.out.println("Test1 passed.");
     54         else
     55             System.out.println("Test1 failed.");
     56     }
     57 
     58     // A B T G
     59     // C F C S
     60     // J D E H
     61 
     62     // BFCE
     63     public void test2() {
     64         char[] matrix = "ABTGCFCSJDEH".toCharArray();
     65         int rows = 3;
     66         int cols = 4;
     67         char[] str = "BFCE".toCharArray();
     68         if (hasPath(matrix, rows, cols, str))
     69             System.out.println("Test2 passed.");
     70         else
     71             System.out.println("Test2 failed.");
     72     }
     73 
     74     // matrix=null
     75     public void test3() {
     76         char[] matrix = null;
     77         int rows = 0;
     78         int cols = 0;
     79         char[] str = "BFCE".toCharArray();
     80         if (!hasPath(matrix, rows, cols, str))
     81             System.out.println("Test3 passed.");
     82         else
     83             System.out.println("Test3 failed.");
     84     }
     85 
     86     // str=null
     87     public void test4() {
     88         char[] matrix = "ABTGCFCSJDEH".toCharArray();
     89         int rows = 3;
     90         int cols = 4;
     91         char[] str = null;
     92         if (!hasPath(matrix, rows, cols, str))
     93             System.out.println("Test4 passed.");
     94         else
     95             System.out.println("Test4 failed.");
     96     }
     97 
     98     // A
     99 
    100     // A
    101     public void test5() {
    102         char[] matrix = "A".toCharArray();
    103         int rows = 1;
    104         int cols = 1;
    105         char[] str = "A".toCharArray();
    106         if (hasPath(matrix, rows, cols, str))
    107             System.out.println("Test5 passed.");
    108         else
    109             System.out.println("Test5 failed.");
    110     }
    111 
    112     // A
    113 
    114     // B
    115     public void test6() {
    116         char[] matrix = "A".toCharArray();
    117         int rows = 1;
    118         int cols = 1;
    119         char[] str = "B".toCharArray();
    120         if (!hasPath(matrix, rows, cols, str))
    121             System.out.println("Test6 passed.");
    122         else
    123             System.out.println("Test6 failed.");
    124     }
    125 
    126     // AAAA
    127     // AAAA
    128     // AAAA
    129 
    130     // AAAAAAAAAAAA
    131     public void test7() {
    132         char[] matrix = "AAAAAAAAAAAA".toCharArray();
    133         int rows = 3;
    134         int cols = 4;
    135         char[] str = "AAAAAAAAAAAA".toCharArray();
    136         if (hasPath(matrix, rows, cols, str))
    137             System.out.println("Test7 passed.");
    138         else
    139             System.out.println("Test7 failed.");
    140     }
    141 
    142     public static void main(String[] args) {
    143         HasPath demo = new HasPath();
    144         demo.test1();
    145 //        demo.test2();
    146 //        demo.test3();
    147 //        demo.test4();
    148 //        demo.test5();
    149 //        demo.test6();
    150 //        demo.test7();
    151     }
    152 }
  • 相关阅读:
    Servlet入门
    序列化
    ConcurrentHashMap红黑树的实现
    ConcurrentHashMap1.7和1.8的源码分析比较
    TCP/IP中的传输层协议TCP、UDP
    Java内存模型和ConcurrentHashMap 1.7源码分析
    JAVA研发面试题
    面试题(Python)
    初识Python
    Python解释器安装与环境变量添加
  • 原文地址:https://www.cnblogs.com/juncaoit/p/10495733.html
Copyright © 2020-2023  润新知