广度优先算法-打开转盘锁

在这里插入图片描述

import java.util.*;
/**
 * @Description
 * @date 2019/10/28
 */
public class OpenLock {

    private Queue<Node> queue1 = new ArrayDeque<>();
    private Queue<Node> queue2 = new ArrayDeque<>();
    private Set<Node> visited = new HashSet<>();
    private Set<String> deadends;
    private String target;

    private class Node{
        public int a;
        public int b;
        public int c;
        public int d;

        public Node(int a,int b,int c,int d) {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return a == node.a &&
                    b == node.b &&
                    c == node.c &&
                    d == node.d;
        }

        @Override
        public int hashCode() {

            return Objects.hash(a, b, c, d);
        }

        @Override
        public String toString() {
            return ""+a+b+c+d;
        }
    }

    public int openLock(String[] deadends, String target) {
        this.deadends = new HashSet<>(Arrays.asList(deadends));
        this.target = target;
        Node root = new Node(0,0,0,0);
        if (this.deadends.contains(root.toString()) || this.deadends.contains(target)) {
            return -1;
        }
        int length = 0;
        queue1.offer(root);
        visited.add(root);
        while(!queue1.isEmpty()) {
            Node cur = queue1.poll();
            if (a(cur) || b(cur) || c(cur) || d(cur)) {
                return ++length;
            }
            if (queue1.isEmpty()) {
                length++;
                queue1 = queue2;
                queue2 = new ArrayDeque<>();
            }
        }
        return -1;
    }

    private boolean a(Node node) {
        Node up = new Node(node.a+1 > 9 ? 0 : node.a+1,node.b,node.c,node.d);
        Node down = new Node(node.a-1 < 0 ? 9 : node.a-1,node.b,node.c,node.d);
        return visit(up, down);
    }

    private boolean b(Node node) {
        Node up = new Node(node.a,node.b+1 > 9 ? 0 : node.b+1,node.c,node.d);
        Node down = new Node(node.a,node.b-1 < 0 ? 9 : node.b-1,node.c,node.d);
        return visit(up, down);
    }

    private boolean c(Node node) {
        Node up = new Node(node.a,node.b,node.c+1 > 9 ? 0 : node.c+1,node.d);
        Node down = new Node(node.a,node.b,node.c-1 < 0 ? 9 : node.c-1,node.d);
        return visit(up, down);
    }

    private boolean d(Node node) {
        Node up = new Node(node.a,node.b,node.c,node.d+1 > 9 ? 0 : node.d+1);
        Node down = new Node(node.a,node.b,node.c,node.d-1 < 0 ? 9 : node.d-1);
        return visit(up, down);
    }

    private boolean visit(Node up, Node down) {
        if (up.toString().equals(this.target)) {
            return true;
        }
        if (down.toString().equals(this.target)) {
            return true;
        }
        if (!visited.contains(up) && !deadends.contains(up.toString())) {
            queue2.offer(up);
            visited.add(up);
        }
        if (!visited.contains(down) && !deadends.contains(down.toString())) {
            queue2.offer(down);
            visited.add(down);
        }
        return false;
    }


    public static void main(String[] args) {
        String[] deadends = {"8887","8889","8878","8898","8788","8988","7888","9888"};
        String target = "8888";
        OpenLock openLock = new OpenLock();
        System.out.println(openLock.openLock(deadends, target));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值