<gh_stars>0
from graphics import *

def search(size,G,display_freq,msg_time,cs,N,nopartial):
    """The algorithm attempts to set one queen per line,
     starting with line 0 and processing lines in order,
     backtracking when it is stuck."""

    print("Searching for size %d..." % size)

    # Window initialisation
    if G:
        win = Window(size,display_freq=display_freq,msg_time=msg_time,cs=cs)
    # In order to test rapidly for possible positions, it
    # maintains tables of free columns and diagonals:
    #  - col[j] indicates that column j is free
    #  - up[k] indicates that the upward-going diagonal k is free
    #  - up[k] indicates that the downward-going diagonal k is free
    # Here j belongs to range(size) and k belong to range(2*size-1).
    col = [-1 for _ in range(size)]
    up = [-1 for _ in range(2*size-1)]
    down = [-1 for _ in range(2*size-1)]
    # The current solution is maintained for display as a list of
    # successive column indices.
    sol = []

    def searchline(i,num_sol=0):
        if i == size:
            if not G and not N: print("Solution found:")
            if G and nopartial:# la solution n'a pas été affichée avant
                win.display(sol)
            if not G:
                print(sol)
            if N: return False, num_sol + 1
            else: return True, 0# 0 will not be used
            #exit(0)
        for j in range(size):
            if col[j] and down[i+j] and up[i-j+size-1]:
                col[j] = down[i+j] = up [i-j+size-1] = False
                sol.append(j)
                #A partial would-be solution has been found.
                if not G and not N and not nopartial:
                    print(sol)
                if G and not nopartial:
                    win.display(sol)
                #Now we test the next line.
                varbool,num_sol = searchline(i+1,num_sol)
                if varbool : return True, 0
                # Is searchline(i+1) returned False, either no solution has been
                # found, or num_sol count is active.
                sol.pop()
                col[j] = down[i+j] = up [i-j+size-1] = True
        return False, num_sol
    solution_found, num_sol = searchline(0)
    if not solution_found and not N:
        print("No solution!")
        exit(0)
    else:
        if N:
            text_num_sol = "There are %d solutions !" % num_sol
            if G: win.msg(text_num_sol)
            else: print(text_num_sol)
        else:
            if G and size < 5: win.msg("Press K",delay=False)
            elif G: win.msg("Press any key to continue",delay=False)
        while G:
            #waiting for a key to be pressed
            pygame.time.Clock().tick(display_freq)
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    pygame.display.quit()
                    exit(0)


from graphics import *

def search(size,G,display_freq,msg_time,cs,N,nopartial):
    """The algorithm attempts to set one queen per line,
     starting with line 0 and processing lines in order,
     backtracking when it is stuck."""

    print("Searching for size %d..." % size)

    # Window initialisation
    if G:
        win = Window(size,display_freq=display_freq,msg_time=msg_time,cs=cs)
    # In order to test rapidly for possible positions, it
    # maintains tables of free columns and diagonals:
    #  - col[j] indicates that column j is free
    #  - up[k] indicates that the upward-going diagonal k is free
    #  - up[k] indicates that the downward-going diagonal k is free
    # Here j belongs to range(size) and k belong to range(2*size-1).
    col = [-1 for _ in range(size)]
    up = [-1 for _ in range(2*size-1)]
    down = [-1 for _ in range(2*size-1)]
    # The current solution is maintained for display as a list of
    # successive column indices.
    sol = []

    def searchline(i,num_sol=0):
        if i == size:
            if not G and not N: print("Solution found:")
            if G and nopartial:# la solution n'a pas été affichée avant
                win.display(sol)
            if not G:
                print(sol)
            if N: return False, num_sol + 1
            else: return True, 0# 0 will not be used
            #exit(0)
        for j in range(size):
            if col[j] and down[i+j] and up[i-j+size-1]:
                col[j] = down[i+j] = up [i-j+size-1] = False
                sol.append(j)
                #A partial would-be solution has been found.
                if not G and not N and not nopartial:
                    print(sol)
                if G and not nopartial:
                    win.display(sol)
                #Now we test the next line.
                varbool,num_sol = searchline(i+1,num_sol)
                if varbool : return True, 0
                # Is searchline(i+1) returned False, either no solution has been
                # found, or num_sol count is active.
                sol.pop()
                col[j] = down[i+j] = up [i-j+size-1] = True
        return False, num_sol
    solution_found, num_sol = searchline(0)
    if not solution_found and not N:
        print("No solution!")
        exit(0)
    else:
        if N:
            text_num_sol = "There are %d solutions !" % num_sol
            if G: win.msg(text_num_sol)
            else: print(text_num_sol)
        else:
            if G and size < 5: win.msg("Press K",delay=False)
            elif G: win.msg("Press any key to continue",delay=False)
        while G:
            #waiting for a key to be pressed
            pygame.time.Clock().tick(display_freq)
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    pygame.display.quit()
                    exit(0)
The algorithm attempts to set one queen per line,      starting with line 0 and processing lines in order,      backtracking when it is stuck. # Window initialisation # In order to test rapidly for possible positions, it # maintains tables of free columns and diagonals: #  - col[j] indicates that column j is free #  - up[k] indicates that the upward-going diagonal k is free #  - up[k] indicates that the downward-going diagonal k is free # Here j belongs to range(size) and k belong to range(2*size-1). # The current solution is maintained for display as a list of # successive column indices. # la solution n'a pas été affichée avant # 0 will not be used #exit(0) #A partial would-be solution has been found. #Now we test the next line. # Is searchline(i+1) returned False, either no solution has been # found, or num_sol count is active. #waiting for a key to be pressed