-
[leetcode] 841. Keys and Rooms알고리즘 2021. 4. 18. 11:58728x90
class Solution { public: bool DFS(vector<vector<int>>& rooms, vector<int>& visited, int now, int& chked){ bool ret = false; if(visited[now]){ return ret; } if(chked == rooms.size() - 1) return !ret; visited[now] = 1; chked++; for(int i:rooms[now]){ ret = ret | DFS(rooms, visited, i, chked); } return ret; } bool canVisitAllRooms(vector<vector<int>>& rooms) { vector<int> visited(rooms.size(), 0); int chked = 0; return DFS(rooms, visited, 0, chked); } };
너무 쉬운 DFS 문제로 코멘트 생략~~
'알고리즘' 카테고리의 다른 글
[leetcode] 128. Longest Consecutive Sequence (0) 2021.04.30 [알고리즘 이론] Tree (0) 2021.04.23 [leetcode] 1267. Count Servers that Communicate (0) 2021.04.17 [leetcode] 1557. Minimum Number of Vertices to Reach All Nodes (0) 2021.04.17 [leetcode] 581. Shortest Unsorted Continuous Subarray (0) 2021.04.16