图论3(Leetcode841.钥匙和房间)

答案:

class Solution {
    public boolean canVisitAllRooms(List> rooms) {
        Set isArrive = new HashSet<>();
        Set newArrive = new HashSet<>();
        isArrive.add(0);
        newArrive.add(0);
        while(newArrive.size()!=0){
            newArrive = getNewArrive(newArrive,isArrive,rooms);
            System.out.println(newArrive);

        }
        System.out.println(newArrive);
        if(isArrive.size()==rooms.size()){
            return true;
        }else{
            return false;
        }
    }
    public Set getNewArrive(Set newArrive,Set isArrive,List> rooms){
        Set nextNewArrive = new HashSet<>();
        Iterator iterator = newArrive.iterator();
        while (iterator.hasNext()) {//遍历newArrive
            int newRoom = iterator.next();
            for(int i=0;i

你可能感兴趣的:(图论)