Question: while True: c = input('Initial position of queens: ') attacking = int(input('Number of attacking pair: ')) successor = [] def split(input_string): return [int(char) for char
while True:
c = input('Initial position of queens: ')
attacking = int(input('Number of attacking pair: '))
successor = []
def split(input_string):
return [int(char) for char in input_string]
def check(state):
attacking_pair = 0
i = 0
while i < 8:
j = i+1
while j < 8:
if state[i] == state[j]:
attacking_pair += 1
j += 1
j += 1
while j < 8:
if (state[i]+1) == state[j]:
attacking_pair += 1
j += 1
if (state[i]-1) == state[j]:
attacking_pair += 1
j += 1
k += 1
j = i+1
k = 1
while j < 8:
if (state[i]-k) == state[j]:
attacking_pair += 1
j += 1
k += 1
i += 1
return 28-attacking_pair
def pick_first():
global h
for row in successor:
if h < row[0]:
return row[1]
def generate_successors(state):
global successor
i = 0
while i < 8:
j = 1
temp_element = state[i]
while j < 9:
if j != temp_element:
temp = state.copy()
temp[i] = j
new_successor = [check(temp), temp]
successor.append(new_successor)
j += 1
i += 1
current_state = split(c)
while True:
h = check(current_state)
if h < attacking:
generate_successors(current_state)
elected = pick_first()
if not elected:
print("stuck in local maxima")
break
else:
print("Found successor", elected)
print("value", check(elected))
current_state = elected
else:
break
decision = input(" DO you want continue? (y/n): ")
if decision == 'n':
break
HERE YOU NEED TO COMMENT EVERY LINE OF CODE.THIS IS A FIRST CHOICE HILL CLIMBING algorithm CODE.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
