单个if破解刑侦题 Python版

前段时间很火的刑侦推理题,网上已经有人用C语言暴力破解了,下面是Python版.

单个if破解刑侦题 Python版_第1张图片

代码如下:

def count_least(A):
    a = 'A'
    num = A.count('A')
    if A.count('B') < num:
        num = A.count('B')
        a = 'B'
    if A.count('C') < num:
        num = A.count('C')
        a = 'C'
    if A.count('D') < num :
        num = A.count('D')
        a = 'D'
    return a, num
def count_most(A):
    a = 'A'
    num = A.count('A')
    if A.count('B') > num:
        num = A.count('B')
        a = 'B'
    if A.count('C') > num:
        num = A.count('C')
        a = 'C'
    if A.count('D') > num :
        num = A.count('D')
        a = 'D'
    return a, num
def in_neighbor(a, b):
    lst = ['A', 'B', 'C', 'D']
    a_i = lst.index(a)
    b_i = lst.index(b)
    if abs(a_i - b_i) == 1:
        return True
    else:
        return False

def main():
    alphas = ['A','B','C','D']
    A = ['A'] * 11
    idxs = [0] * 10
    n = 0
    while(n < 4 ** 10):
        tmp_n = n
        for i in range(10):
            idxs[i] = tmp_n % 4
            tmp_n //= 4
            A[i+1] = alphas[idxs[i]]
        n += 1
        A_least, num_least = count_least(A[1:])
        A_most, num_most = count_most(A[1:])
        sub = num_most - num_least
        res = (((A[2] == 'A' and A[5] == 'C') or (A[2] == 'B' and A[5] == 'D') or (A[2] == 'C' and A[5] == 'A') or (A[2] == 'D' and A[5] == 'B')) and 
        ((A[3] == 'A' and (A[3] != A[6] and A[3] != A[2] and A[3] != A[4])) or (A[3] == 'B' and A[6] != A[3] and A[6] != A[2] and A[6] != A[4]) or \
        (A[3] == 'C' and A[2] != A[3] and A[2] != A[6] and A[2] != A[4]) or (A[3] == 'D' and A[4] != A[3] and A[4] != A[6] and A[4] != A[2])) and 

        ((A[4] == 'A' and A[1] == A[5]) or (A[4] == 'B' and A[2] == A[7]) or (A[4] == 'C' and A[1] == A[9]) or (A[4] == 'D' and A[6] == A[10])) and
        ((A[5] == 'A' and A[8] == A[5]) or (A[5] == 'B' and A[4] == A[5]) or (A[5] == 'C' and A[9] == A[5]) or (A[5] == 'D' and A[7] == A[5])) and
        ((A[6] == 'A' and A[2] == A[8] and A[4] == A[8]) or (A[6] == 'B' and A[1] ==A[8] and A[6] == A[8]) or (A[6] == 'C' and A[3] == A[8] and A[10] == A[8]) \
         or (A[6] == 'D' and A[5] == A[8] and A[9] == A[8])) and 

        ((A[7] == 'A' and A_least == 'C') or (A[7] == 'B' and A_least == 'B') or (A[7] == 'C' and A_least == 'A') or (A[7] == 'D' and A_least == 'D')) and
        ((A[8] == 'A' and not in_neighbor(A[1], A[7])) or (A[8] == 'B' and not in_neighbor(A[1], A[5])) or (A[8] == 'C' and not in_neighbor(A[1], A[2])) or \
        (A[8] == 'D' and not in_neighbor(A[1], A[10]))) and
        ((A[9] == 'A' and (A[1] == A[6]) != (A[6] == A[5])) or (A[9] == 'B' and (A[1] == A[6]) != (A[10] == A[5])) or (A[9] == 'C' and (A[1] == A[6]) != (A[2] == A[5])) \
        or (A[9] == 'D' and (A[1] == A[6]) != (A[9] == A[5])) ) and

        ((A[10] == 'A' and sub == 3) or (A[10]=='B' and sub == 2) or (A[10]=='C' and sub ==4) or (A[10]=='D' and sub ==1)))
        if res == True:
            print('step %d, result:' % n)
            print(A[1:])
            break
if __name__ == '__main__':
    main()

运行结果如下:


需要的时间为0.4秒.

你可能感兴趣的:(单个if破解刑侦题 Python版)