From 4fadd992921506acf5b8f9a3053f0d081c3426da Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sat, 19 Oct 2024 13:17:19 +0000 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20#123=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=86=94=EB=A3=A8=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeetCode/Keys_and_Rooms.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 LeetCode/Keys_and_Rooms.cpp diff --git a/LeetCode/Keys_and_Rooms.cpp b/LeetCode/Keys_and_Rooms.cpp new file mode 100644 index 0000000..48e99ed --- /dev/null +++ b/LeetCode/Keys_and_Rooms.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + bool canVisitAllRooms(vector>& rooms) { + int n = rooms.size(); + vector visited(n, false); + stack s; + s.push(0); + visited[0] = true; + + while (!s.empty()) { + int curr = s.top(); + s.pop(); + for (int key : rooms[curr]) { + if (!visited[key]) { + visited[key] = true; + s.push(key); + } + } + } + + for (bool v : visited) { + if (!v) return false; + } + + return true; + } +};