美文网首页
特殊密码锁

特殊密码锁

作者: innerpeacez | 来源:发表于2020-01-13 10:17 被阅读0次

题目

描述
有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30),按钮有凹/凸两种状态,用手按按钮会改变其状态。

然而让人头疼的是,当你按一个按钮时,跟它相邻的两个按钮状态也会反转。当然,如果你按的是最左或者最右边的按钮,该按钮只会影响到跟它相邻的一个按钮。

当前密码锁状态已知,需要解决的问题是,你至少需要按多少次按钮,才能将密码锁转变为所期望的目标状态。

输入
两行,给出两个由0、1组成的等长字符串,表示当前/目标密码锁状态,其中0代表凹,1代表凸。
输出
至少需要进行的按按钮操作次数,如果无法实现转变,则输出impossible。
样例输入
011
000
样例输出
1

解答

public class Test

    public static void main(String[] args) {
        String concurrent = "011";
        String spec = "000";
        two(concurrent, spec);
    }

    public static void two(String concurrent, String spec) {
        Assert.assertTrue("Cannot be greater than 30", concurrent.length() <= 30);
        String[] one = toArray(concurrent);
        String[] two = toArray(spec);
        if (one.length != two.length) {
            System.out.println("impossible");
            return;
        }

        Integer[] concurrentInts = toIntegers(one);
        Integer[] specInts = toIntegers(two);
        Integer count = 0;

        for (int i = 0; i < concurrentInts.length - 1; i++) {
            // 最后
            if (i == concurrentInts.length - 2) {
                if (concurrentInts[i] != specInts[i]) {
                    count++;
                    concurrentInts[concurrentInts.length - 2] = swap(concurrentInts[concurrentInts.length - 2]);
                    concurrentInts[concurrentInts.length - 1] = swap(concurrentInts[concurrentInts.length - 1]);
                }
            }

            // 第一个位置和中间位置
            if (concurrentInts[i] != specInts[i]) {
                count++;
                concurrentInts[i] = swap(concurrentInts[i + 1]);
                concurrentInts[i+1] = swap(concurrentInts[i + 1]);
                concurrentInts[i+2] = swap(concurrentInts[i + 2]);
            }
        }

        if (Arrays.toString(concurrentInts).equals(Arrays.toString(specInts))) {
            System.out.println(count);
        } else {
            System.out.println("impossible");
        }
    }

    public static Integer[] toIntegers(String[] strs) {
        Integer[] num = new Integer[strs.length];
        for (int i = 0; i < num.length; i++) {
            num[i] = Integer.parseInt(strs[i]);
        }
        return num;
    }

    public static Integer swap(Integer num) {
        if (num == 0) {
            num = 1;
        }
        if (num == 1) {
            num = 0;
        }
        return num;
    }

    public static String[] toArray(String str) {
        return str.split("");
    }
}

本文由博客一文多发平台 OpenWrite 发布!

相关文章

  • 特殊密码锁

    题目 解答 本文由博客一文多发平台 OpenWrite 发布!

  • 001:特殊密码锁

    问题还原 001:特殊密码锁 总时间限制: 1000ms 内存限制: 1024kB 描述 有一种特殊的二进制密码锁...

  • POJ-1-1-特殊密码锁

    总时间限制: 1000ms 内存限制: 1024kB 描述有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30...

  • 防盗指纹密码锁真的防盗吗?你要看这几点

    防盗指纹密码锁真的防盗吗?你要看这几点 都说防盗指纹密码锁,那么指纹密码锁真的防盗吗? 指纹锁是一种高科技智能产物...

  • MOOC算法练习题——特殊的密码锁

    ``` #include #include #include #include ...

  • 密码锁

    一直有给特殊文档加密的习惯,不想让别人看到那些东西。今天想要看一看那些陈年往事,发现……不仅别人,我自己也看不了了...

  • 密码锁

    同事今天中午是去也匆匆,来也匆匆。 原因无他,家人出门遛娃忘记戴钥匙了。大人可以忍饥挨饿,尚在襁褓里的...

  • 密码锁

    文/盐五言六 今天抽空参观了隔壁新小区正在建造的新房子,主要是去海峰家,看看他家地砖色彩的搭配。 灰色调,看来真的...

  • 密码锁

    前些时日,妹妹总是抱怨钥匙难开门,不停的跟我说了好几遍。我后来转念一想,那就换个密码锁吧,现在不是都流行这个么。 ...

  • “不惜两月工资”也要买的美国玩物,打着灯笼都难找

    创意达芬奇密码锁 创意又个性的达芬奇密码锁,可以随意更改自己想要的密码,拿来送礼物非常的好。★☆★☆↑产品地址↑☆...

网友评论

      本文标题:特殊密码锁

      本文链接:https://www.haomeiwen.com/subject/ousgactx.html