AD3311-AI Lab Manual-Ex1a and 1b
AD3311-AI Lab Manual-Ex1a and 1b
: 1 a)
IMPLEMENT BASIC SEARCH STRATEGIES – 8-PUZZLE PROBLEM
AIM:
To write a Python program to solve 8-puzzle problem by implementing basic search strategies.
ALGORITHM:
This algorithm uses a breadth-first search strategy, which guarantees finding the shortest path to the
goal state because it explores states level by level.
1. Define the goal state and the initial state of the 8-Puzzle.
2. Create an empty queue for BFS. Each element in the queue should consist of a state and its
corresponding path from the initial state.
3. Initialize the queue with the initial state and an empty path.
a. Dequeue the front element from the queue. This represents the current state and its path.
- Generate all possible states that can be reached from the current state by moving the
blank tile (0) up, down, left, or right.
- For each possible state, ensure that it has not been visited before (to prevent
revisiting states and loops).
- Create a new path by appending the current possible state to the current path.
- Enqueue the possible state and its new path into the queue.
5. If the queue becomes empty and no solution is found, return that no solution exists.
3
PROGRAM:
for i in range(3):
for j in range(3):
if state[i][j] == 0:
blank_row, blank_col = i, j
return moves
while queue:
current_state, path = queue.popleft()
if is_goal(current_state):
return path
4
for move in get_possible_moves(current_state):
if move not in path: # Prevent revisiting states to avoid loops
new_path = path + [move]
queue.append((move, new_path))
OUTPUT:
RESULT:
Thus, a Python program is successfully written and executed to solve 8-puzzle problem by implementing
BFS algorithm.
5
EX. NO.: 1 b)
IMPLEMENT BASIC SEARCH STRATEGIES – 8-QUEENS PROBLEM
AIM:
To write a Python program to solve 8-queens problem by implementing basic search strategies.
ALGORITHM:
The algorithm uses depth-first search and backtracking to explore all possible combinations of queen
placements while ensuring no two queens threaten each other.
➢ If any cell in the same row as row and in a column to the left of col contains a queen
(board[row][i] == 1), return False.
➢ Iterate through rows from row to 0 (inclusive) and columns from col to 0 (inclusive).
➢ If any cell in the upper left diagonal contains a queen (board[i][j] == 1), return False.
➢ Iterate through rows from row to the last row (inclusive) and columns from col to 0
(inclusive).
➢ If any cell in the lower left diagonal contains a queen (board[i][j] == 1), return False.
- Base Case: If col equals the width of the board (8 in this case), return True as all queens are
placed successfully.
➢ Check if it's safe to place a queen at board[row][col] using the is_safe function.
6
➢ Recursively call solve_queens with the next column col + 1.
➢ If the recursive call returns True, return True to propagate the success up the call stack.
➢ If the recursive call returns False, backtrack by setting board[row][col] back to 0 and
continue the loop.
- Print the board with queens placed as "Q" and empty cells as ".".
- If solve_queens returns True, call the print_solution function to print the solution.
PROGRAM:
return True
for i in range(len(board)):
if is_safe(board, i, col):
board[i][col] = 1 # Place queen
if solve_queens(board, col + 1): # Recur for next column
return True
board[i][col] = 0 # If placing queen doesn't lead to a solution, backtrack
return False
def print_solution(board):
for row in board:
print(" ".join(["Q" if cell == 1 else "." for cell in row]))
def solve_8_queens():
board = [[0 for _ in range(8)] for _ in range(8)]
solve_8_queens()
OUTPUT:
RESULT:
Thus, a Python program is successfully written and executed to solve 8-queens problem by implementing
DFS and backtracking.