There are N rooms and you start in room 0.  Each room has a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room.

Formally, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = rooms.length.  A key rooms[i][j] = v opens the room with number v.

Initially, all the rooms start locked (except for room 0).

You can walk back and forth between rooms freely.

Return true if and only if you can enter every room.

Example 1:

Input: [[1],[2],[3],[]]
Output: true
Explanation:
We start in room 0, and pick up key 1.
We then go to room 1, and pick up key 2.
We then go to room 2, and pick up key 3.
We then go to room 3. Since we were able to go to every room, we return true.

Example 2:

Input: [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can't enter the room with number 2.

Note:

  1. 1 <= rooms.length <= 1000
  2. 0 <= rooms[i].length <= 1000
  3. The number of keys in all rooms combined is at most 3000.

Runtime: 8 ms, faster than 63.18% of C++ online submissions for Keys and Rooms.

class Solution {
public:
bool canVisitAllRooms(vector<vector<int>>& rooms) {
vector<bool> visited(rooms.size(),false);
visited[] = true;
queue<int> q;
q.push();
while(!q.empty()){
int idx = q.front(); q.pop();
for(auto x : rooms[idx]){
if(!visited[x]) {
visited[x] = true;
q.push(x);
}
}
}
for(auto x : visited) if(!x) return false;
return true;
}
};

LC 841. Keys and Rooms的更多相关文章

  1. LeetCode 841. Keys and Rooms

    原题链接在这里:https://leetcode.com/problems/keys-and-rooms/ 题目: There are N rooms and you start in room 0. ...

  2. 【LeetCode】841. Keys and Rooms 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  3. 841. Keys and Rooms —— weekly contest 86

    题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 p ...

  4. LeetCode 841:钥匙和房间 Keys and Rooms

    题目: ​ 有 N 个房间,开始时你位于 0 号房间.每个房间有不同的号码:0,1,2,...,N-1,并且房间里可能有一些钥匙能使你进入下一个房间. ​ 在形式上,对于每个房间 i 都有一个钥匙列表 ...

  5. [LeetCode] Keys and Rooms 钥匙与房间

    There are N rooms and you start in room 0.  Each room has a distinct number in 0, 1, 2, ..., N-1, an ...

  6. [Swift]LeetCode841. 钥匙和房间 | Keys and Rooms

    There are N rooms and you start in room 0.  Each room has a distinct number in 0, 1, 2, ..., N-1, an ...

  7. leetcode841 Keys and Rooms

    """ There are N rooms and you start in room 0. Each room has a distinct number in 0, ...

  8. LeetCode题解之Keys and Rooms

    1.题目描述 2.问题分析 使用深度优先遍历 3.代码 bool canVisitAllRooms(vector<vector<int>>& rooms) { int ...

  9. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

随机推荐

  1. 利用PyMySQL模块操作数据库

    连接到数据库 import pymysql # 创建链接得到一个链接对象 conn = pymysql.Connect( host="127.0.0.1", # 数据库服务器主机地 ...

  2. js动态添加控件(输入框为例)

    写在前面 昨天得到一个需求,需要在账户登记页面中动态添加输入框,经过半天的捣鼓,最终完美成型,写下来跟大家分享下, 供大家参考 开始复制代码了 如果复制了我所有代码的话,注意看js最后面方法的备注,最 ...

  3. 十二,k8s集群访问控制之RBAC授权

    目录 角色访问控制RBAC (Role-Based Access Control) 常用的授权插件: RBAC控制: role 和 clusterrole rolebinding 和 clusterr ...

  4. Summer training #6

    A:水.看0多还是1多就行 B:模拟二进制运算 ,,卡了好久 不应该 #include <bits/stdc++.h> #include <cstring> #include ...

  5. .NET平台的发展

    .NET平台的发展.NET从1.0到.NET Core3.0:C#从1.0到8.0: ASP.NET从1.0到Core3.0: ASP.NET MVC1.0到ASP.NET MVC6.0,

  6. Java线程池详解及实例

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/aa1215018028/article/ ...

  7. Hadoop-No.10之列簇

    HBase中包含列簇(column family)的概念.列簇本质上是列的存储容器.一张表可以有一个或多个列簇.每个列簇都有自己的HFile结婚,而且在执行合并操作时,同一个表的其他列簇不受影响 在很 ...

  8. Hadoop-No.2之标准文件格式

    标准文件格式可以指文本格式,也可以指二进制文件类型.前者包括逗号分隔值(Comma-Separated Value,CSV和可扩展的标记语言文本(Extensible Markup Language. ...

  9. 【Python之路】特别篇--Python文件操作

    文件操作 open函数,该函数用于文件处理 操作文件时,一般需要经历如下步骤: (1)打开文件 (2)操作文件 一.打开文件 文件句柄 = open('文件路径', '模式','编码') 打开文件时, ...

  10. jQuery.getJSON(url, [data], [callback])

    jQuery.getJSON(url, [data], [callback]) 概述 通过 HTTP GET 请求载入 JSON 数据. 在 jQuery 1.2 中,您可以通过使用JSONP形式的回 ...