0% found this document useful (0 votes)
15 views4 pages

Ai 2

The document presents a C program that implements the Breadth-First Search (BFS) algorithm to solve the 8-puzzle problem. It defines a structure for the puzzle state, checks for goal states, manages a queue for state exploration, and prints the solution path if found. The main function initializes the puzzle and calls the solving function to execute the BFS algorithm.

Uploaded by

devanshipatel514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Ai 2

The document presents a C program that implements the Breadth-First Search (BFS) algorithm to solve the 8-puzzle problem. It defines a structure for the puzzle state, checks for goal states, manages a queue for state exploration, and prints the solution path if found. The main function initializes the puzzle and calls the solving function to execute the BFS algorithm.

Uploaded by

devanshipatel514
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AI(3161608) E.

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}
};

// Moves: left, right, up, down


int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

// Check if two states are equal


int is_goal(State *s) {
return memcmp(s->board, goal, sizeof(goal)) == 0;
}

void print_board(int board[N][N]) {


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
SPCE 9 |P a ge
AI(3161608) E.R No:- 221240116001

// Check if a state has already been visited


int is_visited(State s, int count) {
for (int i = 0; i < count; i++) {
if (memcmp(queue[i].board, s.board, sizeof(s.board)) == 0) {
return 1; // Already visited
}
}
return 0;
}

// Add state to queue


void enqueue(State s, int parent) {
s.parent = parent;
queue[rear++] = s;
}

// BFS to solve the puzzle


void solve_puzzle(State initial) {
enqueue(initial, -1);

int solution_path[MAX_QUEUE];
int path_len = 0;
int found = 0;

while (front < rear) {


State current = queue[front];

// If we reach the goal, backtrack to print the path


if (is_goal(&current)) {
found = 1;
int index = front;
while (index != -1) {
solution_path[path_len++] = index;
index = queue[index].parent;
}
break;
}

// Generate next states


for (int i = 0; i < 4; i++) {
int new_x = current.x + dx[i];
int new_y = current.y + dy[i];

if (new_x >= 0 && new_x < N && new_y >= 0 && new_y < N) {
State next = current;
next.x = new_x;
next.y = new_y;

// Swap empty tile with adjacent tile


next.board[current.x][current.y] = next.board[new_x][new_y];
SPCE 10 | P a g e
AI(3161608) E.R No:- 221240116001

next.board[new_x][new_y] = 0;

// Ensure the new state hasn't been visited before


if (!is_visited(next, rear)) {
enqueue(next, front);
}
}
}

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

You might also like