当前位置:网站首页>图 钥匙和房间
图 钥匙和房间
2022-04-22 14:12:00 【借点头发吧】
841. 钥匙和房间
有 N 个房间,开始时你位于 0 号房间。每个房间有不同的号码:0,1,2,…,N-1,并且房间里可能有一些钥匙能使你进入下一个房间。
在形式上,对于每个房间 i 都有一个钥匙列表 rooms[i],每个钥匙 rooms[i][j] 由 [0,1,…,N-1] 中的一个整数表示,其中 N = rooms.length。 钥匙 rooms[i][j] = v 可以打开编号为 v 的房间。
最初,除 0 号房间外的其余所有房间都被锁住。
你可以自由地在房间之间来回走动。
如果能进入每个房间返回 true,否则返回 false。
示例 1:
输入: [[1],[2],[3],[]]
输出: true
解释:
我们从 0 号房间开始,拿到钥匙 1。
之后我们去 1 号房间,拿到钥匙 2。
然后我们去 2 号房间,拿到钥匙 3。
最后我们去了 3 号房间。
由于我们能够进入每个房间,我们返回 true。
示例 2:
输入:[[1,3],[3,0,1],[2],[0]]
输出:false
解释:我们不能进入 2 号房间。
题解:
1)广搜队列
2)深度递归
给定一张有向图(邻接表),询问从 0号节点出发是否能够到达所有的节点。
从0出发按广度优先搜索遍历图;使用辅助队列和访问标志数组
sloution:
1)广搜
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
boolean[] visit=new boolean[rooms.size()];
Queue<Integer> queue=new LinkedList<>();
List<Integer> list=rooms.get(0);
for (int i = 0; i <list.size() ; i++) {
queue.offer(list.get(i));
}
visit[0]=true;
while (true){
int tmp=0;
if (!queue.isEmpty()){
tmp=queue.poll();
}else break;
if (visit[tmp]) continue;
List<Integer> list1=rooms.get(tmp);
for (int i = 0; i <list1.size(); i++) {
if (!visit[list1.get(i)]){
queue.offer(list1.get(i));
}
}
visit[tmp]=true;
}
for (int i = 0; i < visit.length; i++) {
if (!visit[i]){
return false;
}
}
return true;
}
}
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
int n=rooms.size(),num=0;
boolean[]vis =new boolean[n];
Queue<Integer> que=new LinkedList<Integer>();
vis[0]=true;
que.offer(0);
while(!que.isEmpty()){
int x=que.poll();
num++;
for(int it:rooms.get(x)){
if(!vis[it]){
vis[it]=true;
que.offer(it);
}
}
}
return num==n;
}
}
2)官解 深度
class Solution {
boolean[] vis;
int num;
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
int n = rooms.size();
num = 0;
vis = new boolean[n];
dfs(rooms, 0);
return num == n;
}
public void dfs(List<List<Integer>> rooms, int x) {
vis[x] = true;
num++;
for (int it : rooms.get(x)) {
if (!vis[it]) {
dfs(rooms, it);
}
}
}
}
版权声明
本文为[借点头发吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47866171/article/details/108330528
边栏推荐
- Genesis creative comics [stable pass]
- 华为云媒体査勇:华为云在视频AI转码领域的技术实践
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- 2022 examination questions and simulation examination of operation certificate for safety management personnel of hazardous chemical business units
- Thoughts on dealing with high concurrency problems
- 定时器--
- Lors de l'obtention d'une valeur dans la base de données, la base de données a une valeur, mais elle est vide.
- Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
- Golang: package management
- I changed the bug in the morning and was informed of being laid off in the afternoon
猜你喜欢

Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s

leetcode:215. 数组中的第K个最大元素

Independent station operation | 6 Facebook promotion tips, do you know?

Method of running uniapp to applet simulator - uniapp opens wechat developer tool preview support - hbuilderx

树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
![[finally waiting for you] wechat voice forwarding method - voice message forwarding](/img/48/03df12b9787fcc50474d39d618e511.png)
[finally waiting for you] wechat voice forwarding method - voice message forwarding

定时器--

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system

eBPF学习 - 入门

机器越“智能”,数据标注员越容易被淘汰?丨曼孚科技
随机推荐
C Primer Plus --- program list 13.2 reduto c
uniapp转微信开发者工具报错 - [ app.json 文件内容错误] app.json: 未找到 [“sitemapLocation“] 对应的 sitemap.json 文件
Redis batch delete data (wildcard)
[summary of Kunpeng migration and practice Posts] the first play~~
[paper notes] vision transformers for dense prediction
Redis相比memcached
Timer--
Seven years of "one sword" and standing at the edge of the cloud, how to accelerate the digital transformation of enterprises?
SQL optimized by us in those years
Is it safe to open an account in Zhujiang futures?
Crater encountered in calling bash script by makefile
iMeta: 整合宏组学重新认识生命和环境科学
【pytorch】自己实现精简版YOLOV3【五】,实现YOLOV3损失函数(一)
uniapp运行到小程序模拟器的方法 - uniapp开启微信开发者工具预览支持 - HBuilderX
获取数据库中数值时,数据库有值,却为空??
专家系统的开发环境
定时器--
QT explorer and Use of QRC file
二月份,我靠这一份PDF文档面试BAT,没想到竟然收到了5个offer
ipv6相关