Ai 2 - 3
Ai 2 - 3
R No:- 221240116065
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;
// Swap empty tile with adjacent tile next.board[current.x]
[current.y] = next.board[new_x][new_y];
SPCE 2|Page
AI(3161608) E.R No:- 221240116065
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 3|Page
AI(3161608) E.R No:- 221240116065
SPCE 4|Page
AI (3161608) E.R No: - 221240116065
Practical-3
Aim: Write a program to implement DFS (for 8 puzzle problem or Water Jug
problem or any AI search problem).
PROGRAM: -
# Function to generate all possible next states from the current state
def get_next_states(state, capacities):
next_states = []
jug1, jug2 = state
cap1, cap2 = capacities
# Fill jug1
next_states.append((cap1, jug2))
# Fill jug2
next_states.append((jug1, cap2))
# Empty jug1
next_states.append((0, jug2))
# Empty jug2
next_states.append((jug1, 0))
# Pour from jug1 to jug2
pour_to_jug2 = min(jug1, cap2 - jug2)
next_states.append((jug1 - pour_to_jug2, jug2 + pour_to_jug2))
# Pour from jug2 to jug1
pour_to_jug1 = min(jug2, cap1 - jug1)
next_states.append((jug1 + pour_to_jug1, jug2 - pour_to_jug1))
return next_states
SPCE 13 | P a g e
AI (3161608) E.R No: - 221240116065
SPCE 14 | P a g e
AI (3161608) E.R No: - 221240116065
OUTPUT :
SPCE 15 | P a g e