当前位置:网站首页>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();
}
};
边栏推荐
- 【Hot100】739. Daily temperature
- Day 246/300 SSH connection prompt "remote host identification has changed!"
- 前缀和数组系列
- 因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
- 【每日一题】729. 我的日程安排表 I
- C language_ Double create, pre insert, post insert, traverse, delete
- 基于购买行为数据对超市顾客进行市场细分(RFM模型)
- Every API has its foundation when a building rises from the ground
- C语言_双创建、前插,尾插,遍历,删除
- Introduction to ros2 installation and basic knowledge
猜你喜欢
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Every API has its foundation when a building rises from the ground
leetcode59. 螺旋矩阵 II(中等)
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
Introduction to ros2 installation and basic knowledge
How to reconstruct the class explosion caused by m*n strategies?
C语言_双创建、前插,尾插,遍历,删除
Cif10 actual combat (resnet18)
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
What is the biggest problem that fresh e-commerce is difficult to do now
随机推荐
BIO模型实现多人聊天
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente
Machine learning plant leaf recognition
Missing monitoring: ZABBIX monitors the status of Eureka instance
UniPro甘特图“初体验”:关注细节背后的多场景探索
编译,连接 -- 笔记 -2
Entity Developer数据库应用程序的开发
C语言_双创建、前插,尾插,遍历,删除
Monotonic stack
软件测试外包到底要不要去?三年真实外包感受告诉你
SQL Server Manager studio (SSMS) installation tutorial
ROS学习_基础
Number of query fields
【每日一题】729. 我的日程安排表 I
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
雲上有AI,讓地球科學研究更省力
作者已死?AI正用艺术征服人类
LeetCode Algorithm 2181. 合并零之间的节点
基于购买行为数据对超市顾客进行市场细分(RFM模型)