zoukankan      html  css  js  c++  java
  • 矩阵中的路径(回溯法)

    问题描述

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

    解题思路

    使用回溯法解决此题,设置一个和矩阵大小相同的布尔型数组表示是否已经访问。每次访问一个点,递归的访问其向左、向右、向上、向下未被访问且与字符串下一个字符相同的点。

    实现代码

    public class HasPath {
    
        private boolean[][] table;   // 辅助矩阵,已访问的点记为true
        private int rows, cols;      // 矩阵的行数与列数
        private char[] matrix;       // 字符矩阵(一维表示二维)
        private char[] str;          // 字符串
        private boolean res = false; // 矩阵中是否存在一条包含字符串所有字符的路径
    
        // 从点(r,c)开始寻找下一个满足要求的点
        // 在点(r,c)的上下左右,且未被访问,且字符与str[n]相同
        private void test(int r, int c, int n) {
            if (res) return;
            if (n == str.length) {
                res = true;
                return;
            }
    
            System.out.println("r=" + r + "   c=" + c + "   n=" + n);
    
            if (r>0 && !table[r-1][c] && str[n]==matrix[cols*(r-1)+c]) {
                table[r-1][c] = true;
                test(r-1, c, n+1);
                table[r-1][c] = false;
            }
            if (r+1<rows && !table[r+1][c] && str[n]==matrix[cols*(r+1)+c]) {
                table[r+1][c] = true;
                test(r+1, c, n+1);
                table[r+1][c] = false;
            }
            if (c>0 && !table[r][c-1] && str[n]==matrix[cols*r+c-1]) {
                table[r][c-1] = true;
                test(r, c-1, n+1);
                table[r][c-1] = false;
            }
            System.out.println(matrix[cols*r+c]);
            if (c+1<cols && !table[r][c+1] && str[n]==matrix[cols*r+c+1]) {
                table[r][c+1] = true;
                test(r, c+1, n+1);
                table[r][c+1] = false;
            }
        }
    
        public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
            this.matrix = matrix;
            this.rows = rows;
            this.cols = cols;
            this.str = str;
            table = new boolean[rows][cols];
    
            int r, c;       // 表示字符矩阵的行与列(一维/二维换算)
            // 寻找matrix中的起始点,即与str[0]相同的点
            for (int i=0; !res && i<matrix.length; i++) {
                if (matrix[i] == str[0]) {
                    r = i/cols; c = i - cols*r;
                    table[r][c] = true;
                    test(r, c, 1);
                    table[r][c] = false;
                }
            }
            return res;
        }
    
    
    
    
        // 测试
        public static void main(String[] args) {
            HasPath path = new HasPath();
            char[] matrix = "ABCESFCSADEE".toCharArray();
            int rows = 3;
            int cols = 4;
            char[] str = "ABCCED".toCharArray();
            System.out.println(path.hasPath(matrix, rows, cols, str));
            
        }
    }
  • 相关阅读:
    【色彩&职业】识“色”穿衣,个人色调定位体系新概念推荐会_豆瓣
    Pythoner | 你像从前一样的Python学习笔记
    sae-v2ex 一个运行在SAE上的类似v2ex的轻型python论坛
    Micro Python
    【零基础学习服装设计】服装色彩搭配试听课_豆瓣
    【服装设计师之路】灵感的表达:服装效果图(手绘&电脑)_豆瓣
    【布艺DIY】 零基础 做包包 2小时 就OK!_豆瓣
    都市白领个人整体形象打造,想要变美的可以来参加_豆瓣
    【免费】2小时的色彩搭配时尚公开课_豆瓣
    春季心动款外套搭配 彩色小西装令气质飙升
  • 原文地址:https://www.cnblogs.com/deltadeblog/p/9596901.html
Copyright © 2011-2022 走看看