LeetCode841. Keys and Rooms

news/2024/7/20 20:58:57 标签: 算法, 数据结构, 深度优先, leetcode, c++

文章目录

    • 一、题目
    • 二、题解

一、题目

There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.

When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.

Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true if you can visit all the rooms, or false otherwise.

Example 1:

Input: rooms = [[1],[2],[3],[]]
Output: true
Explanation:
We visit room 0 and pick up key 1.
We then visit room 1 and pick up key 2.
We then visit room 2 and pick up key 3.
We then visit room 3.
Since we were able to visit every room, we return true.
Example 2:

Input: rooms = [[1,3],[3,0,1],[2],[0]]
Output: false
Explanation: We can not enter room number 2 since the only key that unlocks it is in that room.

Constraints:

n == rooms.length
2 <= n <= 1000
0 <= rooms[i].length <= 1000
1 <= sum(rooms[i].length) <= 3000
0 <= rooms[i][j] < n
All the values of rooms[i] are unique.

二、题解

class Solution {
public:
    void dfs(vector<vector<int>>& rooms,vector<bool>& visited,int key){
        if(visited[key]) return;
        visited[key] = true;
        vector<int> keys = rooms[key];
        for(int i = 0;i < keys.size();i++){
            dfs(rooms,visited,keys[i]);
        }
    }
    bool canVisitAllRooms(vector<vector<int>>& rooms) {
        vector<bool> visited(rooms.size(),false);
        dfs(rooms,visited,0);
        for(int i = 0;i < visited.size();i++){
            if(visited[i] == false) return false;
        }
        return true;
    }
};

http://www.niftyadmin.cn/n/5235617.html

相关文章

【C++】异常处理 ⑦ ( 异常类的继承层次结构 | 抛出 / 捕获 多个类型异常对象 | 抛出子类异常对象 / 捕获并处理 父类异常对象 )

文章目录 一、抛出 / 捕获 多个类型异常对象1、抛出 / 捕获 多个类型异常对象2、操作弊端3、完整代码示例 二、异常类的继承层次结构1、抛出子类异常对象 / 捕获并处理 父类异常对象2、完整代码示例 - 抛出子类异常对象 / 捕获并处理 父类异常对象 自定义的 异常类 , 可能存在 …

MyBatis自动生成代码(扩展)

可以利用Mybatis-Generator来帮我们自动生成文件 1、自动生成实体类 可以帮助我们针对数据库中的每张表自动生成实体类 2、自动生成SQL映射文件 可以帮助我们针对每张表自动生成SQL配置文件&#xff0c;配置文件里已经定义好对于该表的增删改查的SQL以及映射 3、自动生成接…

微信小程序 scrollview 滚动到指定位置

在微信小程序中&#xff0c;实现 ScrollView 滚动到指定位置有多种方法&#xff0c;下面将介绍三种主要的实现方式。 一、使用scroll-top属性实现滚动 通过设置 scroll-view 组件的 scroll-top 属性&#xff0c;我们可以实现滚动到指定位置。以下是具体实现方式&#xff1a; …

enote笔记法之附录1——“语法词”(即“关联词”)(ver0.24)

enote笔记法之附录1——“语法词”&#xff08;即“关联词”&#xff09;&#xff08;ver0.24&#xff09; 最上面的是截屏的完整版&#xff0c;分割线下面的是纯文字版本&#xff1a; 作者姓名&#xff08;本人的真实姓名&#xff09;&#xff1a;胡佳吉 居住地&#xff1…

【java+vue+微信小程序项目】从零开始搭建——健身房管理平台(3)路由导航卫士、主页实现

项目笔记为项目总结笔记,若有错误欢迎指出哟~ 【项目专栏】 【java+vue+微信小程序项目】从零开始搭建——健身房管理平台(1)spring boot项目搭建、vue项目搭建、微信小程序项目搭建 【java+vue+微信小程序项目】从零开始搭建——健身房管理平台(2)后端跨域、登录模块、sp…

【C语言】结构体

目录 1. 前言2. 结构体类型的声明2.1 结构体的概念2.2 结构的创建2.3 特殊的声明2.4 结构的自引用 3. 结构成员访问操作符4. 结构体内存对齐4.1 对齐规则4.2 为什么存在内存对齐&#xff1f;4.3 修改默认对齐数 5. 结构体传参6. 结构体实现位段6.1 什么是位段6.2 位段的内存分配…

Lag-Llama:基于 LlaMa 的单变量时序预测基础模型

文章构建了一个通用单变量概率时间预测模型 Lag-Llama&#xff0c;在来自Monash Time Series库中的大量时序数据上进行了训练&#xff0c;并表现出良好的零样本预测能力。在介绍Lag-Llama之前&#xff0c;这里简单说明什么是概率时间预测模型。概率预测问题是指基于历史窗口内的…

非标设计之螺纹螺丝选型二

目录 一、螺丝的表面处理工艺&#xff1a;镀锌工艺&#xff1a;渗锌工艺&#xff1a;热浸锌工艺&#xff1a;达克罗工艺&#xff1a;镀镍工艺&#xff1a;氧化&#xff08;发黑&#xff09;工艺&#xff1a;电泳黑工艺&#xff1a;不锈钢螺钉&#xff1a; 二、按照颜色分工艺&a…