Ai 2
Ai 2
R No:- 221240116001
Practical-2
Aim : Write a program to implement BFS (for 8 puzzle problem or Water Jug
problem or any AI search problem).
PROGRAM:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
#define MAX_QUEUE 100000
typedef struct {
int board[N][N];
int x, y; // Position of the empty tile (0)
int parent;
} State;
State queue[MAX_QUEUE];
int front = 0, rear = 0;
// Goal state
int goal[N][N] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};
int solution_path[MAX_QUEUE];
int path_len = 0;
int found = 0;
if (new_x >= 0 && new_x < N && new_y >= 0 && new_y < N) {
State next = current;
next.x = new_x;
next.y = new_y;
next.board[new_x][new_y] = 0;
front++;
}
if (found) {
for (int i = path_len - 1; i >= 0; i--) {
print_board(queue[solution_path[i]].board);
}
} else {
printf("No solution found.\n");
}
}
// Main function
int main() {
State initial = {
{{1, 2, 3}, {5, 6, 0}, {7, 8, 4}}, // Initial state
1, 2, // Position of 0
-1
};
solve_puzzle(initial);
return 0;
}
OUTPUT:-
SPCE 11 | P a g e
AI(3161608) E.R No:- 221240116001
SPCE 12 | P a g e