Question: m plement the given code for solving the 4-queens problem using the depth-first (backtrack) search algorithm. This time, solve it in a 4X4 grid. Submit

mplement the given code for solving the 4-queens problem using the depth-first (backtrack) search algorithm. This time, solve it in a 4X4 grid.

Submit a screenshot of your code, including your grid and the returned solution matrix/grid (solved 4-queens in a 4X4 chessboard)

please please use my code and change according to the requirment

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!