The Maze (Leetcode 490)

這道題是一道很新穎的矩陣搜索題。不同于以往的沿著四個(gè)方向依次search,這道題是一道沿著一個(gè)方向走到底的題目,而我們所需要考慮的點(diǎn),也僅僅是那些一直走,直到走不下去的邊界點(diǎn)。

所以BFS與DFS都建立在邊界點(diǎn)上,只需search這些點(diǎn),而且沿著四個(gè)方向走到底,直到找到destination。

BFS (注意注釋的地方):

class Solution {
public:

    bool isValid(int x, int y, vector<vector<int>>& maze){
        if(x >= 0 && x < maze.size() && y >= 0 && y < maze[0].size() && maze[x][y] == 0) return true;
        return false;
    }

    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        if(maze.empty() || maze[0].empty() || start.empty() || destination.empty()) return 0;
        int row = maze.size(), col = maze[0].size();
        vector<vector<bool>> visited(row, vector<bool>(col, false));
        vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        queue<pair<int, int>> q;
        q.push({start[0], start[1]});
        visited[start[0]][start[1]] = true;
        
        while(!q.empty()){
            int cur_x = q.front().first, cur_y = q.front().second;
            q.pop();
            for(int i=0; i<4; i++){  //注意這里: 沿著一個(gè)方向走到底
                 int next_x = cur_x, next_y = cur_y;
                 while(isValid(next_x + directions[i].first, next_y + directions[i].second, maze)){
                     next_x += directions[i].first;
                     next_y += directions[i].second;
                 }
                 if(next_x == destination[0] && next_y == destination[1]) return true;
                 if(!visited[next_x][next_y]){
                     visited[next_x][next_y] = true;
                     q.push({next_x, next_y});
                 }
            }
        }
        return false;
    }
};

DFS:

class Solution {
public:

    bool isValid(int x, int y, vector<vector<int>>& maze){
        if(x >= 0 && x < maze.size() && y >= 0 && y < maze[0].size() && maze[x][y] == 0) return true;
        return false;
    }
    
    bool dfs(vector<vector<int>>& maze, vector<vector<bool>> &visited, vector<int> cur, vector<int>& destination){
        
        if(cur[0] == destination[0] && cur[1] == destination[1]) return true;
        else if(visited[cur[0]][cur[1]]) return false;
        visited[cur[0]][cur[1]] = true;
        vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for(int i=0; i<4; i++){
            int next_x = cur[0], next_y = cur[1];
            while(isValid(next_x + directions[i].first, next_y + directions[i].second, maze)){
                 next_x += directions[i].first;
                 next_y += directions[i].second; 
            }
            if(dfs(maze, visited, {next_x, next_y}, destination)) return true;
        }
        return false;
    }
    
    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        if(maze.empty() || maze[0].empty() || start.empty() || destination.empty()) return 0;
        int row = maze.size(), col = maze[0].size();
        vector<vector<bool>> visited(row, vector<bool>(col, false));
        return dfs(maze, visited, start, destination);
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容