0% found this document useful (0 votes)
8 views

Ai Lab

Uploaded by

Kamal Palariya
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)
8 views

Ai Lab

Uploaded by

Kamal Palariya
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/ 9

ARTIFICIAL INTELLIGENCE LAB

Prepared by: Submitted to:


Ankit yadav Dr Sunil Sikka
Mca – II
A505145022018
INDEX

S.no Practicals Date Signature


1. Write a program to check whether 8-puzzle problem is solvable or
not.

#include <iostream>
using namespace std;

int getInvCount(int arr[])


{
int inv_count = 0;
for (int i = 0; i < 9 - 1; i++)
for (int j = i+1; j < 9; j++)
if (arr[j] && arr[i] && arr[i] > arr[j])
inv_count++;
return inv_count;
}

bool isSolvable(int puzzle[3][3])


{
int invCount = getInvCount((int *)puzzle);

return (invCount%2 == 0);


}

int main()
{
int puzzle[3][3] = {{1, 7, 2},
{0, 4, 3},
{8, 6, 5}};

isSolvable(puzzle)? cout << "Solvable":


cout << "Not Solvable";
return 0;
}
2.Write a program to find out the heuristic value of a given state of 8-
puzzle game .

#include <bits/stdc++.h>
using namespace std;

int distancesum(int x[], int y[], int n)


{
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
sum += (abs(x[i] - x[j]) + abs(y[i] - y[j]));
return sum;
}

int main()
{
int x[] = { 1, 3, 2 ,5 , 6 , 7 ,0 , 8 , 5 };
int y[] = { 1, 2, 3, 4 , 5 ,6 ,7 ,8 ,0};
int n = sizeof(x) / sizeof(x[0]);
cout << distancesum(x, y, n) << endl;
return 0;
}
3.Write a program in prolog to implement any recommendation system.

suggest(F) :- write('Feeling hungry ?: '),read(P),write('Are you tired too?: '),read(M), fruit(F,M,P).

fruit('grapes',M,P):- M = yes ,(P= (yes)),!.

fruit('bananas',M,P):- M = no ,(P= (yes)),!.

fruit('Have some rest',M,P) :- M = yes, (P=(no)),!.

fruit('Have a nice day',M,P) :- M =no , (P=(no)),!.

You might also like