Leetcode 841. 钥匙和房间

 基本思路: 最基本的深度优先遍历算法的应用。

class Solution {
public:
    void DFS(int room_Num,vector>& rooms,vector& HavenOpen){
        HavenOpen[room_Num] = true;
        for(int n:rooms[room_Num]){
            if(HavenOpen[n] == false){
                DFS(n,rooms,HavenOpen);
            }
        }
        return;
    }
    
    bool canVisitAllRooms(vector>& rooms) {
        int Len = rooms.size();
        vector HavenOpen(Len - 1, false);
        DFS(0,rooms,HavenOpen);
        for(int i=0;i

 

你可能感兴趣的:(Leetcode)