Keys and Rooms
给一个rooms的列表, 里面只有第一个room是开锁的, 其他room的锁的钥匙在各个房间, 求是否能访问所有的room
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
int n = rooms.size();
boolean[] visited = new boolean[n];
Queue<Integer> q = new LinkedList<>();
visited[0] = true;
for(int nn : rooms.get(0)){
q.add(nn);
}
while(!q.isEmpty()){
int t = q.poll();
if(!visited[t]){
visited[t] = true;
for(int nn : rooms.get(t))
q.add(nn);
}
}
for(boolean b : visited)
if(!b)
return false;
return true;
}
}