Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the given code for solving the 8-queens problem using the depth-first (backtrack) search algorithm. This time, start your solution by placing a queen in

Implement the given code for solving the 8-queens problem using the depth-first
(backtrack) search algorithm. This time, start your solution by placing a queen in the
position (1,8], in an 8X8 grid
Submit a screenshot of your code, including your grid and the returned solution
matrix/grid (solved 8-queens in an 8X8 chessboard)
graph={
'A':['B','C'],
'B':['D','E'],
'C':['F','G'],
'D':[],
'E':[],
'F':[],
'G':[]
}
goal='F'
visited=set()
def dfs(visited,graph,node):
if node not in visited:
print(node)
visited.add(node)
for neighbour in graph[node]:
if goal in visited:
break
else:
dfs(visited,graph,neighbour)
dfs(visited,graph,'A')
import numpy as np
import copy
grid=np.zeros([8,8],dtype=int)
print(grid)
grid=grid.tolist()
def possible(grid,y,x):
l=len(grid)
for i in range(l):
if grid[y][i]==1:
return False
for i in range(l):
if grid[i][x]==1:
return False
for i in range(l):
for j in range(l):
if grid[i][j]==1:
if abs(i-y)==abs(j-x):
return False
return True
def solve(grid):
l=len(grid)
for y in range(l):
for x in range(l):
if grid[y][x]==0:
if possible(grid,y,x):
grid[y][x]=1
solve(grid)
if sum(sum(a)for a in grid )==l:
return grid
grid[y][x]=0
return grid
solution=solve(copy.deepcopy(grid))
print(np.matrix(solution))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

Explain budgetary Control

Answered: 1 week ago

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago

Question

What are the different techniques used in decision making?

Answered: 1 week ago