Road trials of the LeetCode (1): Open wheel lock

Topic Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/open-the-lock

Do you have a turntable with four circular dial lock. Each dial has 10 digits: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. Each dial may rotate freely: for example, the '9' to '0', '0' to '9'. Each rotation can only rotate the dial of a digit.

Lock initial number is '0000', a number of the dial string represents four.

Deadends list contains a set number of deaths, the same number and once any element in the list click wheel, the lock will be permanently locked and can no longer be rotated.

String target representatives can unlock digital, you need to give the minimum number of rotations, if any case can not unlock, returns -1.

Solution:
BFS (breadth-first algorithm) general solution: Under normal circumstances, its neighbors have not yet been checked nodes will be placed in a container is called open (for example queue or list), is Verified nodes were placed in a container referred to in closed;

The title ideas: starting from the root, look for the current lock adjacent to the lock, and then add it to open the collection, and then lock each time after the close of the visit added to the collection;

public static int openLock(String[] deadends,String target){

    Set<String> dead = new HashSet<>(Arrays.asList(deadends));
    Set<String> visited = new HashSet<>();
    String init = "0000";
    if (dead.contains(init) || dead.contains(target)) {
        return -1;
    }

    if (target.equals(init)) {
        return 0;
    }

    Set<String> set1 = new HashSet<>();
    set1.add(init);
    Set<String> set2 = new HashSet<>();
    set2.add(target);
    int count=0;
    while (!set1.isEmpty() && !set2.isEmpty()) {
        //将最小的集合遍历
        if (set1.size() > set2.size()) {
            Set<String> temp = set1;
            set1 = set2;
            set2 = temp;
        }
        Set<String> set3 = new HashSet<>();
        for (String curLock : set1) {
            List<String> neibors=findNeibors(curLock);
            for (String nextLock : neibors) {
                //如果set2中包含了这个Lock,则表示初试和目标在途中相遇到了
                if(set2.contains(nextLock)) {
                    return count+1;
                }
                if (!dead.contains(nextLock) && !visited.contains(nextLock)) {
                    visited.add(nextLock);
                    set3.add(nextLock);
                }
            }
        }
        count++;
        set1 = set3;
    }
    return -1;
}

/**
 *  找当前锁相邻的锁
 */
public static List<String> findNeibors(String currLock){
    int size = currLock.length();
    List<String> result = new ArrayList<String>();
    //对锁的4位数分别处理
    for(int i=0;i<size;i++){
        String temp = currLock;
        char[] cs = temp.toCharArray();
        if(cs[i]=='9'){
            cs[i]= '0';
        }else{
            cs[i] = (char)(cs[i] +1);
        }
        temp = new String(cs);
        result.add(temp);

        //执行-1操作
        temp = currLock;
        cs = temp.toCharArray();
        if(cs[i] =='0'){
            cs[i] = '9';
        }else{
            cs[i]=(char)(cs[i]-1);
        }
        temp = new String(cs);
        result.add(temp);
    }
    return result;
}

一步一个脚印,记录自己的成长

Guess you like

Origin www.cnblogs.com/yaphse-19/p/12026650.html