前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sword To Offer 065 - 矩阵中的路径

Sword To Offer 065 - 矩阵中的路径

作者头像
Reck Zhang
发布2021-08-11 11:44:48
2670
发布2021-08-11 11:44:48
举报
文章被收录于专栏:Reck Zhang

矩阵中的路径

Desicription

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

Solution

代码语言:javascript
复制
class Solution {
private:
    vector<vector<bool>> visit;
    vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    vector<vector<char>> mat;
    unsigned int row;
    unsigned int col;
    string findString;
    bool found = false;
    void onSearch(int x, int y, int index) {
        if(found) {
            return ;
        }
        if(index == findString.size() -1) {
            found = true;
            return ;
        }
        visit[x][y] = true;
        for(int i = 0; i < 4; i++) {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            if(dx < 0 || dx >= row || dy < 0 || dy >= col || mat[dx][dy] != findString[index + 1] || visit[dx][dy]) {
                continue;
            }
            onSearch(dx, dy, index + 1);
        }

    }
public:
    bool hasPath(const char* matrix, int rows, int cols, char* str) {
        findString = str;
        row = static_cast<unsigned int>(rows);
        col = static_cast<unsigned int>(cols);
        mat = vector<vector<char>>(row, vector<char>(col));
        int index = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                mat[i][j] = matrix[index++];
            }
        }
        visit = vector<vector<bool>>(row, vector<bool>(col, false));
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(found) {
                    break;
                }
                if(mat[i][j] != findString[0]) {
                    continue;
                }
                onSearch(i, j, 0);
                for(int x = 0; x < row; x++) {
                    for(int y = 0; y < col; y++) {
                        visit[x][y] = false;
                    }
                }
            }
            if(found) {
                break;
            }
        }
        return found;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 矩阵中的路径
    • Desicription
      • Solution
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档