题解 | #开锁#

开锁

https://www.nowcoder.com/practice/e7cbabbf7e0a41ec98055ee5f3d33bbe

import java.util.*;

/**
 * NC363 开锁
 * @author d3y1
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param vec string字符串ArrayList
     * @param tar string字符串
     * @return int整型
     */
    public int open (ArrayList<String> vec, String tar) {
        // return solution1(vec, tar);
        return solution2(vec, tar);
    }

    // 禁止集合
    private HashSet<String> isForbidden;
    // 已访问集合
    private HashSet<String> isVisited = new HashSet<>();
    // 旋转: 上转 下转
    private final int[] rotate = new int[]{1, -1};
    // 开始密码
    private final String START = "0000";
    // 目标密码
    private String TARGET;
    // 最终结果
    private int result = -1;
    
    /**
     * bfs
     * @param vec
     * @param tar
     * @return
     */
    private int solution1(ArrayList<String> vec, String tar){
        isForbidden = new HashSet<>(vec);
        TARGET = tar;

        bfs();

        return result;
    }

    /**
     * bfs
     * 等价于 图的最短路径
     */
    private void bfs(){
        Queue<String> queue = new LinkedList<>();
        queue.offer(START);
        isVisited.add(START);

        String curr;
        HashSet<String> nextSet;
        int level = -1;
        int size;
        while(!queue.isEmpty()){
            size = queue.size();
            level++;
            while(size-- > 0){
                curr = queue.poll();
                if(curr.equals(TARGET)){
                    result = level;
                    return;
                }
                // nextSet = nextCodes(curr);
                nextSet = nextCiphers(curr);
                for(String code: nextSet){
                    queue.offer(code);
                    isVisited.add(code);
                }
            }
        }
    }

    /**
     * 下一个密码
     * @param curr
     * @return
     */
    private HashSet<String> nextCodes(String curr){
        HashSet<String> nextSet = new HashSet<>();
        int digit,nextDigit;
        String code;
        for(int i = 0; i < 4; i++){
            digit = curr.charAt(i)-'0';
            for(int j = 0; j <= 1; j++){
                nextDigit = (digit+rotate[j]+10)%10;
                code = curr.substring(0, i) + nextDigit + curr.substring(i+1);
                if(!isForbidden.contains(code) && !isVisited.contains(code)){
                    nextSet.add(code);
                }
            }
        }

        return nextSet;
    }

    /**
     * 下一个密码
     * @param code
     * @return
     */
    private HashSet<String> nextCiphers(String code){
        HashSet<String> nextSet = new HashSet<>();
        String up,down;
        for(int i = 0; i < 4; i++){
            up = plusOne(code, i);
            if(!isForbidden.contains(up) && !isVisited.contains(up)){
                nextSet.add(up);
            }
            down = minusOne(code, i);
            if(!isForbidden.contains(down) && !isVisited.contains(down)){
                nextSet.add(down);
            }
        }

        return nextSet;
    }

    /**
     * 向上转一位
     * @param code
     * @param i
     * @return
     */
    private String plusOne(String code, int i){
        char[] chs = code.toCharArray();
        if(chs[i] == '9'){
            chs[i] = '0';
        }else{
            chs[i] += 1;
        }

        return new String(chs);
    }

    /**
     * 向下转一位
     * @param code
     * @param i
     * @return
     */
    private String minusOne(String code, int i){
        char[] chs = code.toCharArray();
        if(chs[i] == '0'){
            chs[i] = '9';
        }else{
            chs[i] -= 1;
        }

        return new String(chs);
    }

    //////////////////////////////////////////////////////////////////////////////////

    // 禁止集合
    private HashSet<String> forbid = new HashSet<>();
    // 已访问集合
    private HashSet<String> visited = new HashSet<>();
    // 旋转: 上转 下转
    private int[] turns = new int[]{-1, 1};
    // 开始密码
    private String start = "0000";
    // 目标密码
    private String target;

    /**
     * bfs
     * @param vec
     * @param tar
     * @return
     */
    private int solution2(ArrayList<String> vec, String tar){
        target = tar;
        for(String code: vec){
            forbid.add(code);
        }

        return bfs(start);
    }

    private int bfs(String start){
        int result = -1;
        Queue<String> queue = new LinkedList<>();
        queue.offer(start);

        int step = -1;
        int size;
        String code,next;
        char[] codeChs;
        char ch,nextCh;
        while(!queue.isEmpty()){
            size = queue.size();
            step++;
            while(size-- > 0){
                code = queue.poll();

                if(code.equals(target)){
                    return step;
                }
                for(int i=0; i<4; i++){
                    codeChs = code.toCharArray();
                    ch = codeChs[i];
                    for(int j=0; j<2; j++){
                        nextCh = (char) (((ch-'0'+turns[j]+10)%10)+'0');
                        codeChs[i] = nextCh;
                        next = String.valueOf(codeChs);
                        if(!forbid.contains(next)){
                            if(!visited.contains(next)){
                                visited.add(next);
                                queue.offer(next);
                            }
                        }
                    }
                }
            }
        }

        return result;
    }
}

全部评论

相关推荐

05-11 11:48
河南大学 Java
程序员牛肉:我是26届的双非。目前有两段实习经历,大三上去的美团,现在来字节了,做的是国际电商的营销业务。希望我的经历对你有用。 1.好好做你的CSDN,最好是直接转微信公众号。因为这本质上是一个很好的展示自己技术热情的证据。我当时也是烂大街项目(网盘+鱼皮的一个项目)+零实习去面试美团,但是当时我的CSDN阅读量超百万,微信公众号阅读量40万。面试的时候面试官就告诉我说觉得我对技术挺有激情的。可以看看我主页的美团面试面经。 因此花点时间好好做这个知识分享,最好是单拉出来搞一个板块。各大公司都极其看中知识落地的能力。 可以看看我的简历对于博客的描述。这个帖子里面有:https://www.nowcoder.com/discuss/745348200596324352?sourceSSR=users 2.实习经历有一些东西删除了,目前看来你的产出其实很少。有些内容其实很扯淡,最好不要保留。有一些点你可能觉得很牛逼,但是面试官眼里是减分的。 你还能负责数据库表的设计?这个公司得垃圾成啥样子,才能让一个实习生介入数据库表的设计,不要写这种东西。 一个公司的财务审批系统应该是很稳定的吧?为什么你去了才有RBAC权限设计?那这个公司之前是怎么处理权限分离的?这些东西看着都有点扯淡了。 还有就是使用Redis实现轻量级的消息队列?那为什么这一块不使用专业的MQ呢?为什么要使用redis,这些一定要清楚, 就目前看来,其实你的这个实习技术还不错。不要太焦虑。就是有一些内容有点虚了。可以考虑从PR中再投一点产出
投递美团等公司8个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 14:23
steelhead:你回的有问题,让人感觉你就是来学习的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务