当前位置:网站首页>leetcode841. Keys and rooms (medium)
leetcode841. Keys and rooms (medium)
2022-07-06 07:03:00 【Heavy garbage】



Template questions for search
Train of thought :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();
}
};
Train of thought two :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();
}
};
边栏推荐
- Applied stochastic process 01: basic concepts of stochastic process
- 接口自动化测试框架:Pytest+Allure+Excel
- Call, apply, bind rewrite, easy to understand with comments
- Every API has its foundation when a building rises from the ground
- Introduction to ros2 installation and basic knowledge
- [daily question] 729 My schedule I
- Compile, connect -- notes-2
- 呆错图床系统源码图片CDN加速与破解防盗链功能
- 同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励
- UWA pipeline version 2.2.1 update instructions
猜你喜欢

微信脑力比拼答题小程序_支持流量主带最新题库文件

医疗软件检测机构怎么找,一航软件测评是专家

3. Business and load balancing of high architecture

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

云上有AI,让地球科学研究更省力

Pallet management in SAP SD delivery process

UWA pipeline version 2.2.1 update instructions

SAP SD发货流程中托盘的管理

Development of entity developer database application

LeetCode 78:子集
随机推荐
软件测试外包到底要不要去?三年真实外包感受告诉你
Day 246/300 SSH connection prompt "remote host identification has changed!"
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
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
Reflex WMS中阶系列3:显示已发货可换组
MPLS experiment
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
leetcode59. 螺旋矩阵 II(中等)
What is the difference between int (1) and int (10)? Senior developers can't tell!
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
[advanced software testing step 1] basic knowledge of automated testing
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
编译,连接 -- 笔记 -2
Zhongqing reading news
19. Actual memory management of segment page combination
LeetCode 78:子集
BIO模型实现多人聊天