[剑指offer刷题 --- 24] 矩阵中的路径

错误思路

默认路径中的所有字符只能出现一次,其实完整的路径如ABCB,第一个B和第二个B应该在不同的地方出现。

class Solution {
public:
    bool hasPath(char* matrix, int rows, int cols, char* str)
    {
        vector chs;
        while (*str != '\0') {
            if (find(chs.begin(), chs.end(), *str) == chs.end()) 
                chs.push_back(*str);
            str++;
        }
        vector> mat;
        vector _mat;
        while (*matrix != '\0') {
            _mat.push_back(*matrix);
            if ((int)_mat.size() == cols) {
                mat.push_back(_mat);
                _mat.clear();
            }
            matrix++;
        }
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                // 起点(i, j)
                vector flags (rows * cols, 0);
                if (hasSubPath(mat, chs, rows, cols, i, j, flags))
                    return true;
            }
        }
        return false;
    }
    bool hasSubPath(vector> matrix, vector &chs, int rows, int cols, 
                    int x, int y, vector &visiting_flags) {
        if (chs.size() == 0)
            return true;
        if (x >= 0 && x < rows && 
            y >= 0 && y < cols &&
            visiting_flags[x * cols + y] == 0) {
            // 中
            visiting_flags[x * cols + y] = 1;
            vector::iterator idx = find(chs.begin(), chs.end(), matrix[x][y]);
            if (idx != chs.end())
                chs.erase(idx);
            return hasSubPath(matrix, chs, rows, cols, x - 1, y, visiting_flags) || 
                hasSubPath(matrix, chs, rows, cols, x + 1, y, visiting_flags) || 
                hasSubPath(matrix, chs, rows, cols, x, y - 1, visiting_flags) || 
                hasSubPath(matrix, chs, rows, cols, x, y + 1, visiting_flags);
        } else {
            return false;
        }
    }
};

正确思路

class Solution {
public:
    bool hasPath(char* matrix, int rows, int cols, char* str)
    {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                // 起点(i, j)
                vector flags (rows * cols, 0);
                if (hasSubPath(matrix, str, rows, cols, i, j, flags))
                    return true;
            }
        }
        return false;
    }
    bool hasSubPath(char* matrix, char* str, int rows, int cols, 
                    int x, int y, vector &flags) {
        if(x < 0 || x >= rows || y < 0 || y >= cols) //越界
            return false;

        if (flags[x * cols + y] == 0 && 
            matrix[x * cols + y] == *str) {
            
            flags[x * cols + y] = 1;
            str++;
            
            if (*str == '\0')
                return true;
            return 
                hasSubPath(matrix, str, rows, cols, x - 1, y, flags) || 
                hasSubPath(matrix, str, rows, cols, x + 1, y, flags) || 
                hasSubPath(matrix, str, rows, cols, x, y - 1, flags) || 
                hasSubPath(matrix, str, rows, cols, x, y + 1, flags);
        } else {
            return false;
        }
    }
};

 

你可能感兴趣的:(剑指Offer)