def test_f(self): from nqueens import f self.assertEqual(f([1, 2, 2]), 3) self.assertEqual(f([2, 2, 2]), 3) self.assertEqual(f([0, 0, 2]), 3) self.assertEqual(f([0, 2, 0]), 2) self.assertEqual(f([0, 2, 1]), 2)
def choose_next(curr, static_x, static_y): if len(succ(curr, static_x, static_y)) == 0: return None succs = succ(curr, static_x, static_y) succs.append(curr) succs = sorted(succs) nextState = succs[0] for newState in succs: if f(newState) < f(nextState): nextState = newState return nextState
def n_queens_restart(n, k, static_x, static_y): random.seed(1) state = [0] * n list = [] for y in range(0, k): for x in range(0, n): if x == static_x: state[x] = static_y continue state[x] = random.randint(0, n - 1) list.append(n_queens(state, static_x, static_y)) if f(list[len(list) - 1]) == 0: print(list[len(list) - 1]) return list[len(list) - 1] else: continue
def n_queens(initial_state, static_x, static_y): a = initial_state message = str(initial_state) + ' - f={}' print(message.format(f(initial_state))) next_state = choose_next(initial_state, static_x, static_y) while f(next_state) != 0: if f(a) == f(next_state): break a = next_state message = str(next_state) + ' - f={}' print(message.format(f(next_state))) next_state = choose_next(next_state, static_x, static_y) message = str(next_state) + ' - f={}' print(message.format(f(next_state))) return next_state
no_nqr = "--no-nqr" in sys.argv starttime = time.time() # VERSION NUMBER IS NECESSARY. DO NOT MODIFY print("Version: %s\n" % (version)) print("succ([0, 1, 2], 0, 0)") print(succ([0, 1, 2], 0, 0)) print("succ([1, 1, 2], 0, 0)") print(succ([1, 1, 2], 0, 0)) print("succ tests complete\n") print("f([1, 2, 2])") print(f([1, 2, 2])) print("f([2, 2, 2])") print(f([2, 2, 2])) print("f([0, 0, 2])") print(f([0, 0, 2])) print("f([0, 2, 0])") print(f([0, 2, 0])) print("f([0, 2, 1])") print(f([0, 2, 1])) print("f tests complete\n") print("choose_next([1, 1, 2], 1, 1)") print(choose_next([1, 1, 2], 1, 1)) print("choose_next([0, 2, 0], 0, 0)") print(choose_next([0, 2, 0], 0, 0)) print("choose_next([0, 1, 0], 0, 0)")