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. 平时工作常用linux命令总结

    mkdir 创建目录  make dir cp 拷贝文件  copy mv 移动文件   move rm  删除文件 remove # 创建连级目录 mkdir -p a/b/c # 拷贝文件夹a到文 ...

  2. Win10系统如何利用蓝牙设置动态锁?

    很多小伙伴都会有这样的经历,出门之后没走多远,却已然忘记是否锁门,有强迫症的人就会重新返回查看,以确保门是否反锁. 我们在使用电脑时也是这样,遇到事情要临时离开,却忘记是否锁屏,再返回来就耽误时间了. ...

  3. oracle下关于table的常用sql整理

    创建表,create TABLE table( 列名称1 数据类型1, 列名称2 数据类型2, 列名称3 数据类型3, ......); eg: create table TABLE_24751( i ...

  4. 下拉菜单 Spinner 简单纯字符串版

    下拉菜单 Spinner 简单纯字符串版 public class MainActivity extends Activity implements AdapterView.OnItemSelecte ...

  5. Linux----Ubuntu虚拟机(VMWare)学习

    1.在安装虚拟机系统完成后,如果忘记密码则 https://jingyan.baidu.com/article/c843ea0b9e851077931e4aea.html 2.如何拖动桌面软件移动 长 ...

  6. 小程序UI设计(8)-布局分解-FlexBox的align-content应用

    FlexBox的align-content到底是什么鬼,我也搞了好半天才开发出来,目前工具中WViewRow组件使用了此属性,WViewColumn中此属性不起作用.下图是justify-conten ...

  7. java8学习之Optional深入详解

    自上次[http://www.cnblogs.com/webor2006/p/8243874.html]函数式接口的学习告一段落之后,这次来学习一下Optional,它并非是函数式接口的概念,点击查看 ...

  8. 关于多线程使用sqlite3的问题

    在window系统中使用sqlite3时,如果是多线程,如果设置不当会导致程序崩溃. 首先使用sqlite3_threadsafe()函数,确定当前使用的是线程安全. 之后在初始化的时候,sqlite ...

  9. 一些需要禁用的PHP危险函数(disable_functions)

    一些需要禁用的PHP危险函数(disable_functions)   有时候为了安全我们需要禁掉一些PHP危险函数,整理如下需要的朋友可以参考下 phpinfo() 功能描述:输出 PHP 环境信息 ...

  10. C# 数组转字符串显示

    , , , , , , , }; arry[] = (] | ); string result = String.Join("", arry); Console.WriteLine ...