当前位置:网站首页>leetcode841. 钥匙和房间(中等)
leetcode841. 钥匙和房间(中等)
2022-07-06 06:55:00 【重you小垃】



搜索的模板题
思路一:dfs
class Solution {
public:
int flag[1005] = {
0};
int dfs(vector<vector<int>>& rooms, int idx) {
int cnt = 1;
flag[idx] = 1;
for (int i = 0; i < rooms[idx].size(); ++i) {
if (!flag[rooms[idx][i]]) cnt += dfs(rooms,rooms[idx][i]);
}
return cnt;
}
bool canVisitAllRooms(vector<vector<int>>& rooms) {
return dfs(rooms, 0) == rooms.size();
}
};
思路二:bfs
class Solution {
public:
int flag[1005] = {
0};
bool canVisitAllRooms(vector<vector<int>>& rooms) {
queue<int> q;
q.push(0);
flag[0] = 1;
int cnt = 0;
while (!q.empty()) {
int frt = q.front();
q.pop();
cnt++;
for (int i = 0; i < rooms[frt].size(); ++i) {
if (!flag[rooms[frt][i]]) {
q.push(rooms[frt][i]);
flag[rooms[frt][i]] = 1;
}
}
}
return cnt == rooms.size();
}
};
边栏推荐
- win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
- Latex文字加颜色的三种办法
- Redis Foundation
- SAP SD发货流程中托盘的管理
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- How to reconstruct the class explosion caused by m*n strategies?
- Embed UE4 program into QT interface display
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- leetcode1020. 飞地的数量(中等)
- Basic commands of MySQL
猜你喜欢

机器学习植物叶片识别

Windows Server 2016 standard installing Oracle

UWA Pipeline 2.2.1 版本更新说明

SQL Server Manager studio (SSMS) installation tutorial

Prefix and array series

Database basics exercise part 2

机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-

26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...

C language_ Double create, pre insert, post insert, traverse, delete

hydra常用命令
随机推荐
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
【刷题】怎么样才能正确的迎接面试?
Proteus -- Serial Communication parity flag mode
[unity] how to export FBX in untiy
Fedora/REHL 安装 semanage
LeetCode Algorithm 2181. 合并零之间的节点
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用Shap值对XGBoost模型实现可解释性案例之详细攻略
【软件测试进阶第1步】自动化测试基础知识
SQL Server Manager studio (SSMS) installation tutorial
ROS学习_基础
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
Arduino tutorial - Simon games
RichView TRVStyle 模板样式的设置与使用
Leetcode daily question (1870. minimum speed to arrive on time)
UDP攻击是什么意思?UDP攻击防范措施
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
leetcode704. 二分查找(查找某个元素,简单,不同写法)
雲上有AI,讓地球科學研究更省力