The Maze (Leetcode 490)

這道題是一道很新穎的矩陣搜索題巡验。不同于以往的沿著四個方向依次search,這道題是一道沿著一個方向走到底的題目碘耳,而我們所需要考慮的點深碱,也僅僅是那些一直走,直到走不下去的邊界點藏畅。

所以BFS與DFS都建立在邊界點上,只需search這些點功咒,而且沿著四個方向走到底愉阎,直到找到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++){  //注意這里: 沿著一個方向走到底
                 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)容合作請聯(lián)系作者
  • 序言:七十年代末力奋,一起剝皮案震驚了整個濱河市榜旦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌景殷,老刑警劉巖溅呢,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件澡屡,死亡現(xiàn)場離奇詭異,居然都是意外死亡咐旧,警方通過查閱死者的電腦和手機驶鹉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铣墨,“玉大人室埋,你說我怎么就攤上這事∫猎迹” “怎么了姚淆?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長屡律。 經(jīng)常有香客問我腌逢,道長,這世上最難降的妖魔是什么超埋? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任搏讶,我火速辦了婚禮,結(jié)果婚禮上纳本,老公的妹妹穿的比我還像新娘窍蓝。我一直安慰自己,他們只是感情好繁成,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布吓笙。 她就那樣靜靜地躺著,像睡著了一般巾腕。 火紅的嫁衣襯著肌膚如雪面睛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天尊搬,我揣著相機與錄音叁鉴,去河邊找鬼。 笑死佛寿,一個胖子當(dāng)著我的面吹牛幌墓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播冀泻,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼常侣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了弹渔?” 一聲冷哼從身側(cè)響起胳施,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎肢专,沒想到半個月后舞肆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體焦辅,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年椿胯,在試婚紗的時候發(fā)現(xiàn)自己被綠了筷登。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡压状,死狀恐怖仆抵,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情种冬,我是刑警寧澤镣丑,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站娱两,受9級特大地震影響莺匠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜十兢,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一趣竣、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧旱物,春花似錦遥缕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至宝穗,卻和暖如春户秤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背逮矛。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工鸡号, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人须鼎。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓鲸伴,卻偏偏與公主長得像,于是被迫代替她去往敵國和親晋控。 傳聞我的和親對象是個殘疾皇子挑围,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容