# uberj/N-Queens

Due tomorrow Do tomorrow (actually I did it today).

`I did this fast and dirty. It works. Deal with it.`
uberj committed Nov 18, 2011
1 parent a0e2e17 commit 6e57c01a1e8a5c692aed5152dccaf257af3f7b63
Showing with 88 additions and 3 deletions.
1. +49 −0 board_utils.py
2. +17 −3 nqueens.py
3. +22 −0 rr.py
 @@ -0,0 +1,49 @@ + +""" +Convert a board (matrix) into +and array with vectors to note +where the queens were. +""" +def board_to_coord(board, n): + vecs = [] + for i in range(n): + for j in range(n): + if board[i][j]: + vecs.append((i,j)) + return vecs + +""" +Given a board (in matrix form). Check to see if it is +contained in the group of boards. +@return True if the board isn't in boards + False if the board *is* in boards +""" +def check_for_board(boards, board): + board_vec = board_to_coord(board) + if board_vec in boards: + print "Duplicate" + return False + else: + print "Unique" + return True + +def coord_to_board(vec): + n = len(vec) + board = [ [0]*n for i in range(n) ] + for i in range(len(vec)): + for j in range(len(vec)): + if (i,j) in vec: + board[i][j] = 1 + else: + continue + return board + +def pbc(vec): + print "============" + for i in range(len(vec)): + for j in range(len(vec)): + if (i,j) in vec: + print '1', + else: + print '0', + print
 @@ -2,6 +2,11 @@ import pdb import unittest import copy +import pprint +from board_utils import * +from rr import * + +pp = pprint.PrettyPrinter(indent=4) """ Solve nqueens for a generic NxN board @@ -100,8 +105,17 @@ def pb( board, n ): if __name__ == "__main__": + n = int(sys.argv[1]) solve_nqueens(int(sys.argv[1])) - print solutions - - + meta_solutions = [] + uniq_solutions = [] + for solution in solutions: + s_board = coord_to_board(solution) + if solution not in meta_solutions: + uniq_solutions.append(solutions) + meta_solutions += gen_all_rr(s_board,n) + meta_solutions.append(solution) + #print gen_all_rr(s_board, n) + print len(uniq_solutions) + print "There were "+str(len(uniq_solutions))+" unique soltions."
22 rr.py
 @@ -1,6 +1,7 @@ # Rotation and Reflections functions import copy from nqueens import * +from board_utils import * def rotate90(matrix, n): # OH GOD THIS IS TERRIBLE. @@ -16,3 +17,24 @@ def reflect(matrix,n): reflected_matrix.append(copy.deepcopy(matrix[n-1-i])) return reflected_matrix + +""" +Generate all rotations. +""" +def gen_all_rot(matrix,n): + rote90 = rotate90(matrix,n) + rote180 = rotate90(rote90,n) + rote270 = rotate90(rote180,n) + rots = [board_to_coord(rote90,n),\ + board_to_coord(rote180,n),\ + board_to_coord(rote270,n)] + return rots + +""" +Generate all rotations and reflections +""" +def gen_all_rr(matrix,n): + reflected = reflect(matrix,n) + rrs = [] + return gen_all_rot(matrix,n)+gen_all_rot(reflected,n)+[board_to_coord(reflect(matrix,n),n)] +