提交代码
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
boolean[] access=new boolean[rooms.size()];
Arrays.fill(access, false);
access[0] = true;
Queue<Integer> next=new LinkedList<>();
next.add(0);
while(next.size()>0) {
int curRoom = next.poll();
for(int i=0;i<rooms.get(curRoom).size();i++) {
if(access[rooms.get(curRoom).get(i)])
continue;
access[rooms.get(curRoom).get(i)] = true;
next.add(rooms.get(curRoom).get(i));
}
}
for(int i=0;i<access.length;i++)
if(!access[i])
return false;
return true;
}
}
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1b6be07120a365fe154111c7b64ea80f.png)