Posted on

Description

Submission

class Solution {
    vector<vector<int>> ds;
    int m, n;

    bool dfs(vector<vector<int>>& visited, vector<vector<int>>& maze, vector<int> cur, vector<int>& dest) {
        if(cur[0] == dest[0] && cur[1] == dest[1]) return true;

        bool ret = false;

        for(auto& d: ds) {
            int x = cur[0], y = cur[1];
            for(; x >= 0 && x < n && y >= 0 && y < m && !maze[x][y]; x += d[0], y += d[1]) continue;
            x -= d[0], y -= d[1];
            if(visited[x][y]) continue;
            visited[x][y] = 1;
            ret = ret || dfs(visited, maze, {x, y}, dest);
        }

        return ret;
    }

public:
    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        n = maze.size();
        m = maze[0].size();
        vector<vector<int>> visited(n, vector<int>(m, 0));
        ds = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

        return dfs(visited, maze, start, destination);
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *