Lab 2 AI
Lab 2 AI
Problem Formulation:
• State Space: Any arrangement of k queens in the first k rows such that none are attacked
• Initial state: 0 queens on the board
• Successor function: Add a queen to the (k+1)th row so that none are attacked.
• Goal test: 8 queens on the board, none are attacked
Source Code:
import pprint
def isSafe(board, x, y, n):
#Checking whether the column is filled
for row in range(x):
if(board[row][y] == 'Q'):
return False
#Checking for top left diagonals are filled
row = x
col = y
while(row>=0 and col>=0):
if(board[row][col] == 'Q'):
return False
row -= 1
col -= 1
#Checking for top right diagonals are filled
row = x
col = y
while(row>=0 and col<n):
if(board[row][col] == 'Q'):
return False
row -= 1
col += 1
#return True if all the aforementioned tests is passed
return True
Output: