首页 > 代码库 > 解锁图案-九宫格有多少种组合?安全吗?用程序来解答

解锁图案-九宫格有多少种组合?安全吗?用程序来解答

如图所示的九宫格密码有多少种组合呢?这么密码是不是比数字密码更安全呢?下面用数字来说话。

 

通常设置密码至少4个点,最多9个点,规则通常是两点之间有一点,必须要过中间这个点,比如从1开始,必须要经过2才可以到3。1是可以直接到6的,但通常这种设置比较少。

运行附录的程序得到如下的数据:
size: 4 count0: 144 count1: 96 count2:40
size: 5 count0: 600 count1: 336 count2:152
size: 6 count0: 2880 count1: 1344 count2:304
size: 7 count0: 15120 count1: 4272 count2:496
size: 8 count0: 80640 count1: 18432 count2:1024
size: 9 count0: 362880 count1: 32256 count2:784
sum:  count0: 462264 count1: 56736 count2:2800
use time: 453ms

例如:4个点组合可能有144种可能,排除飞点(1直接到3)的情况,剩下96种可能,如果不考虑跨点(1到6)的情况,仅剩下40种可能。

所以得出的结论是:4-9个点任意组合的九宫格有56736种组合,排除跨点的情况,有2800种可能。
一般人设置在5-7个点,通常形状并不复杂的可能性仅为1000种左右,试出来的可能性还是很大的,而使用4位数字密码的组合则为10000种左右。

 

源码附录:

package string;

import java.security.InvalidParameterException;

public class HowMany {
    public static void main(String[] args) {
        String[] except1 = new String[]{
                "46", "64", "28", "82", "19", "91", "37", "73",
                "13", "31", "39", "93", "97", "79", "17", "71"};

        String[] except2 = new String[]{
                "16", "61", "18", "81", "34", "43", "38", "83",
                "72", "27", "76", "67", "92", "29", "94", "49"};

        long lastTime = System.currentTimeMillis();

        long sum0 = 0;
        long sum1 = 0;
        long sum2 = 0;
        for (int i = 4; i <= 9; i++) {
            long count0 = 0;
            long count1 = 0;
            long count2 = 0;
            for (int j = 1; j <= 10 - i; j++) {
                Combines num = new Combines(j, i);
                String str;
                boolean flag1 = false;
                boolean flag2 = false;

                do {
                    flag1 = false;
                    flag2 = false;
                    str = num.getOne();
                    for (String except : except1) {
                        if (str.contains(except)) {
                            flag1 = true;
                            break;
                        }
                    }
                    for (String except : except2) {
                        if (str.contains(except)) {
                            flag2 = true;
                            break;
                        }
                    }
                    
                    count0++;
                    if (!flag1) {
                        count1++;
                    }
                    if (!flag1 && !flag2) {
                        count2++;
                        //System.out.println(str);
                    }
                } while (num.moveNext());
            }
            sum0 += count0;
            sum1 += count1;
            sum2 += count2;
            System.out.println("size: " + i + " count0: " + count0 + " count1: " + count1 + " count2:" + count2);
        }

        System.out.println("sum: " + " count0: " + sum0 + " count1: " + sum1 + " count2:" + sum2);
        System.out.println("use time: " + (System.currentTimeMillis() - lastTime) + "ms");
    }
    
    private static class Combines {
        private int base;
        private int[] number;
        private int[] poll;
        private long amount;
        private long count;

        public Combines(int start, int size) {
            if (size <= 1) {
                throw new InvalidParameterException();
            }

            base = start;
            number = new int[size];
            poll = new int[size];
            amount = this.jieCheng(size);

            this.reset();
        }

        public void reset() {
            initNumber();
            initPoll();
            count = 0;
        }

        public String getOne() {
            StringBuffer strBuf = new StringBuffer();

            for (int i = 0; i < poll.length; i++) {
                strBuf.append(getNumber(poll[i]));
            }

            return strBuf.toString();
        }

        public boolean moveNext() {
            if (++count < amount) {
                this.stepNext(1);
                this.initNumber();
                return true;
            } else {
                return false;
            }
        }

        private long jieCheng(int x) {
            long y = 1;

            for (int i = 1; i <= x; i++) {
                y = y * i;
            }

            return y;
        }

        private int getNumber(int index) {
            int num;
            for (int i = 0; i < number.length; i++) {
                if (number[i] != 0) {
                    if (index == 0) {
                        num = number[i];
                        number[i] = 0;
                        return num;
                    }
                    index--;
                }
            }

            return 0;
        }

        private void stepNext(int index) {
            if (poll[poll.length - 1 - index] == index) {
                this.stepNext(index + 1);
                poll[poll.length - 1 - index] = 0;
            } else {
                poll[poll.length - 1 - index]++;
                return;
            }
        }

        private void initNumber() {
            for (int i = 0; i < number.length; i++) {
                number[i] = i + base;
            }
        }

        private void initPoll() {
            for (int i = 0; i < poll.length; i++) {
                poll[i] = 0;
            }
        }
    }
}