Skip to main content
bugfix - kings must be considered too
Source Link

Benjamin's answer agrees with a brute (and by brute I mean brute) force calculation using Python. The following code was already used in parts for this question and recycled (see also there for code hints). For the variant with castling rights, just delete ])# to check your result. (Edited code tip: The inner if checks whether all neighbors of knights are border or bishop or another knight...or rook if castling counts.)

def nextperm(permo):
    n = len(permo)
    y = n - 1
    while permo[y - 1] > permo[y]:
        y -= 1
    yy = n - 1
    while permo[y - 1] > permo[yy]:
        yy -= 1
    permn = permo[0:y - 1] + [permo[yy]] + sorted(permo[y - 1:yy] + permo[yy + 1:n])
    return permn

def perminv(perm):
    
    num_sqsize = len(perm)
    perm_inv = [0 for _ in range(num_sqsize)]
    for y in range(num_sqsize):
        x = perm[y]
        perm_inv[x] = y
    return perm_inv

ch = ['K','Q','R','R','B','B','N','N']
#01234567 = kqrrbbnn
perm = [_ for _ in range(8)]
ct = 0
ca = [0]*7
for i in range(40320):
    ki = perm[0]
    qu = perm[1]
    lr = perm[2]
    rr = perm[3]
    lb = perm[4]
    rb = perm[5]
    ln = perm[6]
    rn = perm[7]
    if lr < ki < rr and ln < rn and lb < rb and (lb+rb)%2 == 1:
        out = ''
        permi = perminv(perm)
        for j in range(8):
            out += ch[permi[j]]
        set1=set([ln+1,ln-1,rn+1,rn-1])
        set2=set([-1,8,lb,rb,ln,rn])#,lr,rr]rr,ki])
        if set1.issubset(set2):
            ct += 1
            print(ct,out)
    perm = nextperm(perm)

Benjamin's answer agrees with a brute (and by brute I mean brute) force calculation using Python. The following code was already used in parts for this question and recycled (see also there for code hints). For the variant with castling rights, just delete ])# to check your result. (Edited code tip: The inner if checks whether all neighbors of knights are border or bishop or another knight...or rook if castling counts.)

def nextperm(permo):
    n = len(permo)
    y = n - 1
    while permo[y - 1] > permo[y]:
        y -= 1
    yy = n - 1
    while permo[y - 1] > permo[yy]:
        yy -= 1
    permn = permo[0:y - 1] + [permo[yy]] + sorted(permo[y - 1:yy] + permo[yy + 1:n])
    return permn

def perminv(perm):
    
    num_sqsize = len(perm)
    perm_inv = [0 for _ in range(num_sqsize)]
    for y in range(num_sqsize):
        x = perm[y]
        perm_inv[x] = y
    return perm_inv

ch = ['K','Q','R','R','B','B','N','N']
#01234567 = kqrrbbnn
perm = [_ for _ in range(8)]
ct = 0
ca = [0]*7
for i in range(40320):
    ki = perm[0]
    qu = perm[1]
    lr = perm[2]
    rr = perm[3]
    lb = perm[4]
    rb = perm[5]
    ln = perm[6]
    rn = perm[7]
    if lr < ki < rr and ln < rn and lb < rb and (lb+rb)%2 == 1:
        out = ''
        permi = perminv(perm)
        for j in range(8):
            out += ch[permi[j]]
        set1=set([ln+1,ln-1,rn+1,rn-1])
        set2=set([-1,8,lb,rb,ln,rn])#,lr,rr])
        if set1.issubset(set2):
            ct += 1
            print(ct,out)
    perm = nextperm(perm)

Benjamin's answer agrees with a brute (and by brute I mean brute) force calculation using Python. The following code was already used in parts for this question and recycled (see also there for code hints). For the variant with castling rights, just delete ])# to check your result. (Edited code tip: The inner if checks whether all neighbors of knights are border or bishop or another knight...or rook if castling counts.)

def nextperm(permo):
    n = len(permo)
    y = n - 1
    while permo[y - 1] > permo[y]:
        y -= 1
    yy = n - 1
    while permo[y - 1] > permo[yy]:
        yy -= 1
    permn = permo[0:y - 1] + [permo[yy]] + sorted(permo[y - 1:yy] + permo[yy + 1:n])
    return permn

def perminv(perm):
    
    num_sqsize = len(perm)
    perm_inv = [0 for _ in range(num_sqsize)]
    for y in range(num_sqsize):
        x = perm[y]
        perm_inv[x] = y
    return perm_inv

ch = ['K','Q','R','R','B','B','N','N']
#01234567 = kqrrbbnn
perm = [_ for _ in range(8)]
ct = 0
ca = [0]*7
for i in range(40320):
    ki = perm[0]
    qu = perm[1]
    lr = perm[2]
    rr = perm[3]
    lb = perm[4]
    rb = perm[5]
    ln = perm[6]
    rn = perm[7]
    if lr < ki < rr and ln < rn and lb < rb and (lb+rb)%2 == 1:
        out = ''
        permi = perminv(perm)
        for j in range(8):
            out += ch[permi[j]]
        set1=set([ln+1,ln-1,rn+1,rn-1])
        set2=set([-1,8,lb,rb,ln,rn])#,lr,rr,ki])
        if set1.issubset(set2):
            ct += 1
            print(ct,out)
    perm = nextperm(perm)
Source Link

Benjamin's answer agrees with a brute (and by brute I mean brute) force calculation using Python. The following code was already used in parts for this question and recycled (see also there for code hints). For the variant with castling rights, just delete ])# to check your result. (Edited code tip: The inner if checks whether all neighbors of knights are border or bishop or another knight...or rook if castling counts.)

def nextperm(permo):
    n = len(permo)
    y = n - 1
    while permo[y - 1] > permo[y]:
        y -= 1
    yy = n - 1
    while permo[y - 1] > permo[yy]:
        yy -= 1
    permn = permo[0:y - 1] + [permo[yy]] + sorted(permo[y - 1:yy] + permo[yy + 1:n])
    return permn

def perminv(perm):
    
    num_sqsize = len(perm)
    perm_inv = [0 for _ in range(num_sqsize)]
    for y in range(num_sqsize):
        x = perm[y]
        perm_inv[x] = y
    return perm_inv

ch = ['K','Q','R','R','B','B','N','N']
#01234567 = kqrrbbnn
perm = [_ for _ in range(8)]
ct = 0
ca = [0]*7
for i in range(40320):
    ki = perm[0]
    qu = perm[1]
    lr = perm[2]
    rr = perm[3]
    lb = perm[4]
    rb = perm[5]
    ln = perm[6]
    rn = perm[7]
    if lr < ki < rr and ln < rn and lb < rb and (lb+rb)%2 == 1:
        out = ''
        permi = perminv(perm)
        for j in range(8):
            out += ch[permi[j]]
        set1=set([ln+1,ln-1,rn+1,rn-1])
        set2=set([-1,8,lb,rb,ln,rn])#,lr,rr])
        if set1.issubset(set2):
            ct += 1
            print(ct,out)
    perm = nextperm(perm)