my code stock.com

Vipin Raj Manayil Rajan

Combinations of QUEENs at chess board without attacking each other
by Vipin Raj Manayil Rajan

Place QUEENs on a 8*8 chess board in such a way that these queens are not attacking each other. Write program for finding different combinations of Queues positions

Snippet options

Download: Download snippet as combinations-of-queens-at-chess-board-without-attacking-each-other.py.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

def matrix(m,n,a):
    r1 = "+-------" * n
    r1 = r1 + "+"
    r2 = "|       " * n
    r2 = r2 + "|"

    row = 0
    cell = 1
    while(row<m):
        i=1
        print(r1)
        while(i<4):
            if(i==2):
                k=0
                r3 = ""
                while(k<n):
                    cell = a[row][k]
                    r3 = r3 + "| " + str(cell).ljust(6)
                    k+=1
                r3 = r3 + "|"
                print(r3)
                i+=1
                continue
            print(r2)
            i+=1
        row+=1
    print(r1)

def set_attacking_positions(r,c):

    # Same row
    for i in range(SIZE):
        if (i == c):
            a[r][c] = 1
        else:
            a[r][i] = -1

    # Same column
    for i in range(SIZE):
        if (i == r):
            a[r][c] = 1
        else:
            a[i][c] = -1

    # Forward diagonal
    i, j = r, c
    while (i+1 < SIZE and j+1 < SIZE):
        a[i + 1][j + 1] = -1
        i += 1
        j += 1

    i, j = r, c
    while (i + 1 < SIZE and j != 0):
        a[i + 1][j - 1] = -1
        i += 1
        j -= 1

    # Backward diagonal
    i, j = r, c
    while (i != 0 and j != 0):
        a[i - 1][j - 1] = -1
        i -= 1
        j -= 1

    i, j = r, c
    while (i != 0 and j + 1 < SIZE):
        a[i - 1][j + 1] = -1
        i -= 1
        j += 1

def check_next_position():
    for t1 in range(SIZE):
        for t2 in range(SIZE):
            if(a[t1][t2] == 0):
                return [t1,t2]
    return None


def clear():
    global a
    a = [0] * SIZE
    for i in range(SIZE):
        a[i] = [0] * SIZE

if __name__ == '__main__':
    global SIZE
    global a
    SIZE = 8
    clear()
    dict = {}
    list = []
    for k in range(SIZE):
        a[0][k] = 1
        count = 1
        set_attacking_positions(0,k)
        pos = check_next_position()
        while(pos != None):
            i,j = pos[0],pos[1]
            a[i][j] = 1
            count +=1
            set_attacking_positions(i, j)
            pos = check_next_position()

        for i in range(SIZE):
            for j in range(SIZE):
                if(a[i][j]== -1):
                    a[i][j]=" "
                else:
                    a[i][j]="Q"
        dict[k] = count
        list.append(a)
        #matrix(SIZE,SIZE,a)
        clear()
    key_max = max(dict.keys(), key=(lambda p: dict[p]))
    print(dict[key_max])
    matrix(SIZE,SIZE,list[key_max])

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.