剑指 Offer 12. 矩阵中的路径

剑指 Offer 12. 矩阵中的路径

给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

 

例如,在下面的 3×4 的矩阵中包含单词 "ABCCED"(单词中的字母已标出)。

示例 1:

输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
输出:true
示例 2:

输入:board = [["a","b"],["c","d"]], word = "abcd"
输出:false
 

提示:

1 <= board.length <= 200
1 <= board[i].length <= 200
board 和 word 仅由大小写英文字母组成

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

题解:写了一个DFS,但是一直超时,没搞懂怎么回事。。。日后再研究。

 

class Solution {
public:
    bool ans;
    Solution () {}
    void find_matrix(vector<vector<char>>& f_board, string &f_word, vector<vector<int>> &f_vis, int strpos, int x, int y, int row, int col) {
        if (strpos == f_word.size())
        {
            ans = true;
            return;
        }
        int dx[4] = {0, 1, 0, -1};
        int dy[4] = {1, 0, -1, 0};
        for (int i = 0; i < 4; ++i)
        {
            int tmp_x = x + dx[i];
            int tmp_y = y + dy[i];
            //cout << "now:" << tmp_x << " " << tmp_y << endl;
            if (tmp_x < 0 || tmp_x >= row || tmp_y < 0 || tmp_y >= col)
                continue;
            if (f_vis[tmp_x][tmp_y] == 0 && f_board[tmp_x][tmp_y] == f_word[strpos])
            {
                //cout << "now:" << f_board[tmp_x][tmp_y] << endl;
                f_vis[tmp_x][tmp_y] = 1;
                strpos++;
                //cout << "strpos:" << strpos << endl;
                find_matrix(f_board, f_word, f_vis, strpos, tmp_x, tmp_y, row, col);
                strpos--;
                f_vis[tmp_x][tmp_y] = 0;
                //f_solve.pop();
            }
        }
        return;
    }
    bool exist(vector<vector<char>>& board, string word) {
        if (board.empty() || board[0].empty())
            return word.empty();
        ans = false;
        int row = board.size();
        int col = board[0].size();

        vector<int> v(board[0].size());
        vector< vector<int> > vis;
        for (int i = 0; i < row; ++i)
            vis.push_back(v);

        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j < col; ++j)
            {
                if (board[i][j] == word[0] && vis[i][j] == 0)
                {
                    vis[i][j] = 1;
                    //cout << word[0] << endl;
                    //cout << i << j << endl;
                    //cout << board[2][3] << endl;
                    find_matrix(board, word, vis, 1, i, j, row, col);
                    if (ans) return true;
                    vis[i][j] = 0;
                }
                //printf("%d %d\n", i, j);
            }
        }
        if (ans) return true;
        return false;
    }
};

12:15更新,我过了,把递归入口用或运算||一下,可以剪枝掉一些无用情况,学到了……

class Solution {
public:
    Solution () {}
    bool find_matrix(vector<vector<char>>& f_board, string &f_word, vector<vector<int>> &f_vis, int strpos, int x, int y, int row, int col) {
        if (x < 0 || x >= row || y < 0 || y >= col || f_vis[x][y] == 1)
            return false;
        if (f_board[x][y] != f_word[strpos])
            return false;
        if (strpos == f_word.size()-1)
            return true;
        f_vis[x][y] = 1;
        bool res = (
        find_matrix(f_board, f_word, f_vis, strpos+1, x+1, y, row, col)||
        find_matrix(f_board, f_word, f_vis, strpos+1, x-1, y, row, col)||
        find_matrix(f_board, f_word, f_vis, strpos+1, x, y-1, row, col)||
        find_matrix(f_board, f_word, f_vis, strpos+1, x, y+1, row, col));
        f_vis[x][y] = 0;
        return res;
    }
    bool exist(vector<vector<char>>& board, string word) {
        bool ans = false;
        if (board.empty() || board[0].empty())
            return word.empty();
        int row = board.size();
        int col = board[0].size();

        vector<int> v(board[0].size());
        vector< vector<int> > vis;
        for (int i = 0; i < row; ++i)
            vis.push_back(v);

        for (int i = 0; i < row; ++i)
        {
            for (int j = 0; j < col; ++j)
            {
                if (board[i][j] == word[0] && vis[i][j] == 0)
                {
                    ans = find_matrix(board, word, vis, 0, i, j, row, col);
                    if (ans) return true;
                }
            }
        }
        if (ans) return true;
        return false;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值