my ai file
DESCRIPTION
nsit ai fileTRANSCRIPT
![Page 1: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/1.jpg)
Program No. – 1
Problem – Tower of Hanoi
Analysis of problem on basis of seven characteristics of AI problems :
The Tower of Hanoi problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps cannot be ignored but can be undone.
The universe is predictable. Every time we make a move we do know exactly what will happen.
Solution is absolute.
The solution is a state.
The role of knowledge is not much. Control strategy is required.
No interaction with a person is required to solve the problem.
So, on the basis of the analysis of the problem based on these seven characteristics control strategy is
made.
CODE
#include<bits/stdc++.h>
using namespace std;
struct node
{
int from, to, aux;
};
int main()
{
int n, m = 1;
cout << "Enter the number of disks: ";
cin >> n;
m = (m << n);
m--;
vector<node> a(m);
int k = m;
m = (m - 1) >> 1;
a[m].from = 1;
a[m].aux = 2;
![Page 2: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/2.jpg)
a[m].to = 3;
for(int j = 0; j < n-1; j++)
{
int p = 1, r = (m - 1) >> 1, q = m;
p = (p << j);
for(int i = 0; i < p; i++)
{
a[r].from = a[q].from;
a[r].to = a[q].aux;
a[r].aux = a[q].to;
r = r + m + 1;
a[r].from = a[q].aux;
a[r].to = a[q].to;
a[r].aux = a[q].from;
r = r + m + 1;
q = q + 2*(m + 1);
}
m = (m - 1) / 2;
}
for(int i = 0; i < k; i++)
cout << "From peg " << a[i].from << " To peg " << a[i].to << endl;
}
OUTPUT :
Enter the number of disks: 3
From peg 1 To peg 3
From peg 1 To peg 2
From peg 3 To peg 2
From peg 1 To peg 3
From peg 2 To peg 1
From peg 2 To peg 3
From peg 1 To peg 3
![Page 3: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/3.jpg)
Program No. – 2
Problem – Water jug
Analysis of problem on basis of seven characteristics of AI problems :
The Water jug problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps can be ignored but cannot be undone. We can always reach to the initial state by pouring all water on the ground.
The universe is predictable. Every time we make a move we do know exactly what will happen.
Solution is absolute.
The solution is a state where required amount of water is generated.
The role of knowledge is extensive. Complex control strategy is to be applied to find next move.
No interaction with a person is required to solve the problem.
CODE
#include<bits/stdc++.h>
using namespace std;
int main()
{
int big, small, z, x = 0, y = 0;
cout << "Enter size of the bigger jug : ";
cin >> big;
cout << "Enter size of smaller jug : ";
cin >> small;
cout << "Enter the amount to be generated : ";
cin >> z;
int g = __gcd(small, big);
if(z % g != 0)
{
cout << "No possible solution\n";
return 0;
}
while(y != z)
{
![Page 4: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/4.jpg)
if(y == 0)
{
y = big;
cout << "\n\nFill the bigger jug full";
cout << "\n BIG SMALL";
cout << "\n " << y << " " << x;
}
else
{
if(y < small-x)
{
x = x + y;
y = 0;
cout << "\n\nPour all water from bigger jug to smaller jug";
cout << "\n BIG SMALL";
cout << "\n " << y << " " << x;
}
else
{
y = y - (small - x);
x = 0;
cout << "\n\nFill smaller jug full with bigger jug and spill it";
cout << "\n BIG SMALL";
cout << "\n " << y << " " << x;
}
}
}
cout<<"\nThis is the final solution";
return 0;
}
![Page 5: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/5.jpg)
OUTPUT :
Enter size of the bigger jug : 5
Enter size of smaller jug : 3
Enter the amount to be generated : 4
Fill the bigger jug full
BIG SMALL
5 0
Fill smaller jug full with bigger jug and spill it
BIG SMALL
2 0
Pour all water from bigger jug to smaller jug
BIG SMALL
0 2
Fill the bigger jug full
BIG SMALL
5 2
Fill smaller jug full with bigger jug and spill it
BIG SMALL
4 0
This is the final solution.
![Page 6: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/6.jpg)
Program No. – 3
Problem – Monkey banana
Analysis of problem on basis of seven characteristics of AI problems :
The Monkey banana problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps can be ignored as well as undone.
The universe is predictable. Every time we make a move we do know exactly what will happen.
Solution is absolute.
The solution is a state where the money has the banana.
The role of knowledge is much. Control strategy is required.
No interaction with a person is required to solve the problem.
CODE
#include<bits/stdc++.h>
using namespace std;
struct status
{
int chair, stick, onchair, gotbanana;
};
int main()
{
status s;
cout << "The solution for monkey banana problem :\n";
s.chair = 0;
s.stick = 0;
s.onchair = 0;
s.gotbanana = 0;
if(s.chair == 0)
cout << "Put chair under banana\n";
s.chair = 1;
![Page 7: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/7.jpg)
if(s.stick == 0)
cout << "Grab the stick\n";
s.stick = 1;
if(s.onchair == 0)
cout << "Stand on the chair\n";
s.onchair = 1;
if(s.gotbanana == 0)
cout << "Get banana with stick\n";
s.gotbanana = 1;
return 0;
}
OUTPUT :
The solution for monkey banana problem :
Put chair under banana
Grab the stick
Stand on the chair
Get banana with stick
![Page 8: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/8.jpg)
Program No. – 4
Problem – Eight puzzle
Analysis of problem on basis of seven characteristics of AI problems.
The Eight puzzle problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps cannot be ignored but can be undone. To undone a streak of moves we have to store it somewhere.
The universe is predictable. Every time we make a move we know exactly what will happen.
As we have to find shortest sequence of moves for it, solution is relative not absolute.
The solution here is a sequence of moves. So the solution is a path rather than a state.
The role of knowledge is not much. Simple mechanism will do the trick.
No interaction with a person is required to solve the problem.
CODE #include<bits/stdc++.h> using namespace std; struct status { int state, a[3][3], curx, cury; string sol; status() { curx = cury = 2; a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[1][0] = 4; a[1][1] = 5; a[1][2] = 6; a[2][0] = 7; a[2][1] = 8; a[2][2] = 0; state = 0; } bool operator ==(status s1) { bool flag = 1; for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) if(s1.a[i][j] != a[i][j]) flag = 0; return flag; } }; status solve(status s) { queue<status> q; status s1, s2, final; q.push(s); while( !q.empty() )
![Page 9: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/9.jpg)
{ s1 = q.front(); q.pop(); if(s1 == final) return s1; else { s2 = s1; if(s2.state != 2) { s2.state = 1; s2.sol += "s"; s2.curx++; if(s2.curx >=0 && s2.curx < 3 && s2.cury >= 0 && s2.cury < 3) { s2.a[s2.curx-1][s2.cury] = s2.a[s2.curx][s2.cury]; s2.a[s2.curx][s2.cury] = 0; q.push(s2); } } s2 = s1; if(s2.state != 1) { s2.state=2; s2.sol += "n"; s2.curx--; if(s2.curx >= 0 && s2.curx < 3 && s2.cury >= 0 && s2.cury<3) { s2.a[s2.curx + 1][s2.cury] = s2.a[s2.curx][s2.cury]; s2.a[s2.curx][s2.cury] = 0; q.push(s2); } } s2 = s1; if(s2.state != 4) { s2.state = 3; s2.sol += "e"; s2.cury++; if(s2.curx >= 0 && s2.curx < 3 && s2.cury >= 0 && s2.cury < 3) { s2.a[s2.curx][s2.cury - 1] = s2.a[s2.curx][s2.cury]; s2.a[s2.curx][s2.cury] = 0; q.push(s2); } } s2=s1; if(s2.state != 3) { s2.state = 4; s2.sol += "w"; s2.cury--; if(s2.curx >= 0 && s2.curx < 3 && s2.cury >= 0 && s2.cury < 3)
![Page 10: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/10.jpg)
{ s2.a[s2.curx][s2.cury + 1] = s2.a[s2.curx][s2.cury]; s2.a[s2.curx][s2.cury] = 0; q.push(s2); } } } } } int main() { status s, s1; cout << "Enter the problem matrix :\n"; for(int i = 0 ; i < 3; i++) for(int j = 0; j < 3; j++) { cin >> s.a[i][j]; if(s.a[i][j] == 0) { s.curx = i; s.cury = j; } } s.sol = ""; s.state = 0; s1 = solve(s); for(int i = 0; i < s1.sol.length(); i++) { switch(s1.sol[i]) { case 'e': s.cury++; s.a[s.curx][s.cury - 1] = s.a[s.curx][s.cury]; s.a[s.curx][s.cury] = 0; break; case 'w' : s.cury--; s.a[s.curx][s.cury + 1] = s.a[s.curx][s.cury]; s.a[s.curx][s.cury] = 0; break; case 'n' : s.curx--; s.a[s.curx + 1][s.cury] = s.a[s.curx][s.cury]; s.a[s.curx][s.cury] = 0; break; case 's' : s.curx++; s.a[s.curx - 1][s.cury] = s.a[s.curx][s.cury]; s.a[s.curx][s.cury] = 0; break; default : cout << "Error";
![Page 11: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/11.jpg)
} cout << "\nNext State :\n"; for(int k = 0; k < 3; k++) { for(int j = 0; j < 3; j++) cout << " " << s.a[k][j]; cout<<"\n"; } } return 0; }
OUTPUT : Enter the problem matrix : 1 2 3 0 4 5 7 8 6 Next State : 1 2 3 4 0 5 7 8 6 Next State : 1 2 3 4 5 0 7 8 6 Next State : 1 2 3 4 5 6 7 8 0
![Page 12: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/12.jpg)
Program No. – 5
Problem – TIC TAC TOE
Analysis of problem on basis of seven characteristics of AI problems.
The Tic Tac Toe problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps can neither be ignored nor be undone. A move once made is final.
The universe is not predictable. Every time we make a move we do not know exactly what will happen.
Solution is absolute. It is a won game or drawn.
The solution is a state where win or draw is achieved.
The role of knowledge is extensive. Complex heuristic function is to be applied to find next move.
Interaction with a person is required to play the game.
CODE
#include<bits/stdc++.h>
using namespace std;
void draw(int board[][3])
{
char piece;
cout << "\n\n\n";
for(int i = 2; i > -1; i--)
{
cout<<"\t\t\t\t";
for (int j = 0; j < 3; j++)
{
if( !board[i][j] )
piece = (char)(i * 3 + j) + 49;
else if(board[i][j] == 2)
piece = 'X';
else
piece = 'O';
if(j != 0)
![Page 13: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/13.jpg)
cout << char(179) << " ";
cout<< piece << " ";
}
if(i != 0)
{
cout << "\n\t\t\t\t" << char(196) << char(196) << char(196) << char(196) << char(196);
cout<< char(196) << char(196) << char(196) << char(196);
cout << "\n";
}
}
}
int heuristic(int board[][3], int *x, int *y)
{
int score;
int scores[9] = {2, 0, -1, 3, 10, 2, 51, 0, 0};
score = scores[ board[*x][0] + board[*x][1] + board[*x][2] ];
score += scores[ board[0][*y] + board[1][*y] + board[2][*y] ];
if(*x == *y)
score += scores[ board[0][0] + board[1][1] + board[2][2] ];
if(*x + *y == 2)
score += scores[ board[2][0] + board[1][1] + board[0][2] ];
return score;
}
int finish(int board[][3], int player)
{
char again;
char name[3][30] = { "ARTIFICIAL INTELLIGENCE", "NO ONE", "PLAYER" };
draw(board);
cout << "\n" << name[player % 3] << " WON\nWANNA PLAY AGAIN? (Y/N): ";
cin >> again;
if(again == 'Y' || again == 'y')
![Page 14: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/14.jpg)
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
board[i][j] = 0;
return 0;
}
else
return 1;
}
int main()
{
enum player { computer = 3, human = 2, tie = 1 };
int board[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
int game = 0, turn = 0;
while(1)
{
int choice, row, column, howMany, newScore;
int biggestScore[9][3];
for(int i = 0; i < 9; i++)
for (int j = 0; j < 3; j++)
biggestScore[i][j] = 0;
if(turn == 9)
if( finish(board, tie) )
break;
else
{
game++;
turn = 0;
continue;
}
else if((game + turn) % 2 == 0)
![Page 15: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/15.jpg)
{
draw(board);
cout << "\n\nSelect position to make move (1-9): ";
cin >> choice;
if(choice >= 1 && choice <= 9)
{
row = (int) (choice - 1) / 3;
column = (choice - 1) % 3;
if(board[row][column])
{
cout << "Space taken, select again.\n";
continue;
}
else
{
board[row][column] = human;
turn++;
}
if(heuristic(board, &row, &column) > 50)
if(finish(board, human)) break;
else
{
game++;
turn = 0;
continue;
}
}
}
else
{
for(int x = 0; x < 3; x++)
![Page 16: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/16.jpg)
for(int y = 0; y < 3; y++)
if( !board[x][y] )
{
newScore = heuristic(board, &x, &y);
if(newScore > biggestScore[0][0])
howMany = 0;
if(newScore >= biggestScore[0][0])
{
biggestScore[howMany][0] = newScore;
biggestScore[howMany][1] = x;
biggestScore[howMany++][2] = y;
}
}
int random = rand() % howMany;
row = biggestScore[random][1];
column = biggestScore[random][2];
if(biggestScore[0][0] > 50)
{
board[row][column] = computer;
if(finish(board, computer)) break;
else
{
game++;
turn = 0;
continue;
}
}
else
{
board[row][column] = computer;
turn++;
![Page 17: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/17.jpg)
}
}
}
return 0;
}
OUTPUT :
7 │ 8 │ 9
─────────
4 │ 5 │ 6
─────────
1 │ 2 │ 3
Select position to make move (1-9): 5
7 │ 8 │ 9
─────────
4 │ X │ 6
─────────
1 │ 2 │ O
Select position to make move (1-9): 4
7 │ 8 │ 9
─────────
X │ X │ O
─────────
1 │ 2 │ O
Select position to make move (1-9): 9
![Page 18: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/18.jpg)
7 │ 8 │ X
─────────
X │ X │ O
─────────
O │ 2 │ O
Select position to make move (1-9): 2
7 │ O │ X
─────────
X │ X │ O
─────────
O │ X │ O
Select position to make move (1-9): 7
X │ O │ X
─────────
X │ X │ O
─────────
O │ X │ O
NO ONE WON
WANNA PLAY AGAIN? (Y/N):
![Page 19: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/19.jpg)
Program No. – 6
Problem – Missionary cannibal
Analysis of problem on basis of seven characteristics of AI problems.
The Missionary cannibal problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps cannot be ignored but can be undone. To undone a streak of moves we have to store it somewhere.
The universe is predictable. Every time we make a move we know exactly what will happen.
As we have to find shortest sequence of moves for it, solution is relative not absolute.
The solution here is a sequence of moves. So the solution is a path rather than a state.
The role of knowledge is not much. Simple mechanism will do the trick.
No interaction with a person is required to solve the problem.
CODE #include<bits/stdc++.h>
using namespace std;
struct status
{
int state,boat;
int m1, c1, m2, c2;
string sol;
bool operator ==(status s1)
{
bool flag;
flag = (m1 == s1.m1) && (m2 == s1.m2) && (c1 == s1.c1) && (c2 == s1.c2);
return flag;
}
};
void solve(status s)
{
queue<status> q;
![Page 20: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/20.jpg)
status s1, s2, final;
final.c1 = final.m1 = 0;
final.c2 = final.m2 = 3;
q.push(s);
while( !q.empty() )
{
s1 = q.front();
q.pop();
if(s1 == final)
{
cout << s1.sol;
return ;
}
else
{
s2 = s1;
if(s2.state != 1)
{
s2.state=1;
s2.sol += "1c ";
if(s2.boat == 1)
{
s2.c1--; s2.c2++;
s2.boat = 2;
}
else
{
s2.c1++; s2.c2--;
s2.boat = 1;
}
if( ( (s2.m1 == 0) || (s2.c1 <= s2.m1) ) && ( (s2.m2 == 0) || (s2.c2 <= s2.m2) ) )
![Page 21: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/21.jpg)
q.push(s2);
}
s2 = s1;
if(s2.state != 2)
{
s2.state = 2;
s2.sol += "1m ";
if(s2.boat == 1)
{
s2.m1--; s2.m2++;
s2.boat = 2;
}
else
{
s2.m1++; s2.m2--;
s2.boat = 1;
}
if( ( (s2.m1 == 0) || (s2.c1 <= s2.m1) ) && ( (s2.m2 == 0) || (s2.c2 <= s2.m2) ) )
q.push(s2);
}
s2 = s1;
if(s2.state != 3)
{
s2.state = 3;
s2.sol += "2c ";
if(s2.boat == 1)
{
s2.c1 -= 2; s2.c2 += 2;
s2.boat = 2;
}
else
![Page 22: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/22.jpg)
{
s2.c1 += 2; s2.c2 -= 2;
s2.boat = 1;
}
if( ( (s2.m1 == 0) || (s2.c1 <= s2.m1) ) && ( (s2.m2 == 0) || (s2.c2 <= s2.m2) ) )
q.push(s2);
}
s2 = s1;
if(s2.state != 4)
{
s2.state = 4;
s2.sol += "2m ";
if(s2.boat == 1)
{
s2.m1 -= 2; s2.m2 += 2;
s2.boat = 2;
}
else
{
s2.m1 += 2; s2.m2 -= 2;
s2.boat = 1;
}
if( ( (s2.m1 == 0) || (s2.c1 <= s2.m1) ) && ( (s2.m2 == 0) || (s2.c2 <= s2.m2) ) )
q.push(s2);
}
s2 = s1;
if(s2.state != 5)
{
s2.state = 5;
s2.sol += "1c1m ";
if(s2.boat == 1)
![Page 23: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/23.jpg)
{
s2.c1--; s2.c2++; s2.m1--; s2.m2++;
s2.boat = 2;
}
else
{
s2.c1++; s2.c2--; s2.m1++; s2.m2--;
s2.boat = 1;
}
if( ( (s2.m1 == 0) || (s2.c1 <= s2.m1) ) && ( (s2.m2 == 0) || (s2.c2 <= s2.m2) ) )
q.push(s2);
}
}
}
}
int main()
{
status s;
cout << "The solution for missionary cannibal problem :\n";
s.m1 = 3; s.m2 = 0; s.c1 = 3; s.c2 = 0; s.boat = 1; s.state = 0; s.sol = "";
solve(s);
return 0;
}
OUTPUT :
The solution for missionary cannibal problem :
2c 1c 2c 1c 2m 1c1m 2m 1c 2c 1c 2c
![Page 24: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/24.jpg)
Program No. – 7
Problem – Traveling salesman
Analysis of problem on basis of seven characteristics of AI problems.
The Traveling salesman problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps can be ignored as well as undone.
The universe is predictable. Every time we make a move we know exactly what will happen.
As we have to find shortest distance traveled for it, solution is relative not absolute.
The solution here is a sequence of cities en route. So the solution is a path rather than a state.
The role of knowledge is not much. Simple mechanism will do the trick.
No interaction with a person is required to solve the problem.
CODE #include<bits/stdc++.h>
using namespace std;
int main()
{
int n, d;
cout << "Enter the no. of cities: ";
cin >> n;
vector< vector<int> > q(n, vector<int>(n)), s(n, vector<int>(n));
vector<int> t(n), dist(n);
for(int i = 0; i < n-1; i++)
for(int j = i+1; j < n; j++)
{
cout << "Enter ditance between city " << i+1 << " and city " << j+1 << ": ";
cin >> q[i][j];
q[j][i] = q[i][j];
}
for(int i=0;i<n;i++)
{
d = 0;
for(int j = 0; j < n; j++)
![Page 25: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/25.jpg)
t[j] = 0;
t[i] = 1;
int min, r = i, k = i;
for(int j = 0; j < n-1; j++)
{
min = -1;
for(int p = 0; p < n; p++)
{
if(t[p] == 1) continue;
if(min < 0)
{
min = q[r][p];
k = p;
continue;
}
if(min > q[r][p])
{
min = q[r][p];
k = p;
}
}
t[k] = 1;
r = k;
d += min;
s[i][j] = k+1;
}
s[i][n-1] = i+1;
dist[i] = d + q[i][r];
}
int k = 0;
for(int i = 0; i < n; i++)
![Page 26: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/26.jpg)
if(dist[k] > dist[i])
k = i;
cout << "Distance is: " << dist[k];
cout << "\nRoute is: " << k+1;
for(int i = 0; i < n; i++)
cout << " " << s[k][i];
return 0;
}
OUTPUT :
Enter the no. of cities: 4
Enter ditance between city 1 and city 2: 46
Enter ditance between city 1 and city 3: 29
Enter ditance between city 1 and city 4: 73
Enter ditance between city 2 and city 3: 92
Enter ditance between city 2 and city 4: 143
Enter ditance between city 3 and city 4: 56
Distance is: 274
Route is: 1 3 4 2 1
![Page 27: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/27.jpg)
Program No. – 8
Problem – Chess 8-Queen
Analysis of problem on basis of seven characteristics of AI problems.
The 8-Queen problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps can be ignored as well as can be undone. We have to remember the sequence of moves made up till now to undone the move.
The universe is predictable. Every time we make a move we do know exactly what will happen.
Solution is absolute.
The solution is a state where all queens are placed without attacking each other.
The role of knowledge is not too much. Simple backtracking is sufficient.
No interaction with a person is required to solve the problem.
CODE
#include <bits/stdc++.h>
using namespace std;
#define TRUE 1
#define FALSE 0
static short int board[8][8];
int good()
{
for(int i = 0; i < 8; i++)
{
int count1 = 0, count2 = 0;
for(int j = 0; j < 8; j++)
{
if(board[i][j] == TRUE)
count1++;
if(board[j][i] == TRUE)
count2++;
if(count1 > 1 || count2 > 1)
![Page 28: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/28.jpg)
return FALSE;
}
}
for(int i = 0; i < 8; i++)
{
int count1 = 0, count2 = 0, j = 0, k = i;
while(j < 8 && k < 8)
{
if(board[k][j] == TRUE)
count1++;
if(board[j][k] == TRUE)
count2++;
if(count1 > 1 || count2 > 1)
return FALSE;
j++;
k++;
}
}
for(int i = 0; i < 8; i++)
{
int count = 0, j = 0, k = i;
while(j < 8 && k >= 0)
{
if(board[k][j] == TRUE)
count++;
if(count > 1)
return FALSE;
j++;
k--;
![Page 29: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/29.jpg)
}
}
for(int i = 0; i < 8; i++)
{
int count = 0, j = 7, k = i;
while(j >= 0 && k < 8)
{
if(board[j][k] == TRUE)
count++;
if(count > 1)
return FALSE;
j--;
k++;
}
}
return TRUE;
}
int check(int n)
{
int i;
for(i = 0; i < 8; i++)
{
board[n][i] = TRUE;
if((n == 7) && (good() == TRUE))
return TRUE;
if((n < 7) && (good() == TRUE) && (check(n+1) == TRUE))
return TRUE;
board[n][i] = FALSE;
}
![Page 30: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/30.jpg)
return FALSE;
}
void drawboard()
{
printf("\nSolution:\n\n");
for(int y = 0; y < 8; y++)
{
printf("\n-------------------------\n");
for (int x = 0; x < 8; x++)
{
if(board[y][x] == TRUE)
cout << "|Q ";
else
cout << "| ";
}
cout << "|";
}
printf("\n-------------------------\n");
}
int main()
{
int i, j;
for(i = 0; i < 8; i++)
for(j = 0; j < 8; j++)
board[i][j] = FALSE;
if(check(0) == TRUE)
drawboard();
}
![Page 31: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/31.jpg)
OUTPUT :
-------------------------
|Q | | | | | | | |
-------------------------
| | | | |Q | | | |
-------------------------
| | | | | | | |Q |
-------------------------
| | | | | |Q | | |
-------------------------
| | |Q | | | | | |
-------------------------
| | | | | | |Q | |
-------------------------
| |Q | | | | | | |
-------------------------
| | | |Q | | | | |
-------------------------
![Page 32: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/32.jpg)
Program No. – 9
Problem – Cryptarithmetic
Analysis of problem on basis of seven characteristics of AI problems.
The Cryptarithmetic problem is not decomposable into a set of nearly independent smaller or easier sub problems. The entire problem has to be solved as a whole unit.
The solution steps cannot be ignored but can be undone.
The universe is predictable. Every time we make a move we do know exactly what will happen.
Solution is absolute. It is the value of all characters.
The solution is a state.
The role of knowledge is not. Control strategy is required.
No interaction with a person is required to solve the problem.
CODE #include<bits/stdc++.h> using namespace std; string s1, s2, s3; vector<int> use(10); struct node { char c; int v; }; int check(node* arr, const int count) { int v1 = 0, v2 = 0, v3 = 0, m = 1, j; for(int i = s1.length()-1; i >= 0; i--) { char ch = s1[i]; for(j = 0; j < count; j++) if(arr[j].c == ch) break; v1 += m*arr[j].v; m *= 10; } m = 1; for(int i = s2.length()-1; i >=0; i--) { char ch = s2[i]; for(j = 0; j < count; j++) if(arr[j].c == ch) break; v2 += m*arr[j].v; m *=10; } m = 1; for(int i = s3.length()-1; i >= 0; i--)
![Page 33: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/33.jpg)
{ char ch = s3[i]; for(j = 0; j < count; j++) if(arr[j].c == ch) break; v3 += m*arr[j].v; m *= 10; } if(v3 == v1 + v2) return 1; else return 0; } void perm(const int count, node* arr, int n) { if(n == count - 1){ for(int i = 0; i < 10; i++){ if(use[i] == 0){ arr[n].v = i; if(check(arr, count) == 1){ cout << "\nSolution found :"; for(int j = 0; j < count; j++) cout << "\n" << arr[j].c << " = " << arr[j].v; } } } return; } for(int i = 0; i < 10; i++) if(use[i] == 0) { arr[n].v = i; use[i] = 1; perm(count, arr, n+1); use[i] = 0; } } int main() { cout << "Enter first string: "; cin >> s1; cout << "Enter second string: "; cin >> s2; cout << "Enter third string: "; cin >> s3; int l1, l2, l3, count = 0; l1 = s1.length(); l2 = s2.length(); l3 = s3.length(); vector<int> a(26); for(int i = 0; i < l1; i++) ++a[ s1[i] - 'a' ]; for(int i = 0; i < l2; i++)
![Page 34: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/34.jpg)
++a[ s2[i] - 'a' ]; for(int i = 0; i < l3; i++) ++a[ s3[i] - 'a' ]; for(int i = 0; i < 26; i++) if(a[i] > 0) count++; if(count > 10) { cout << "Invalid strings"; return 0; } node *b; b = (node*)malloc(count * sizeof(node)); for(int i = 0, j = 0; i < 26; i++) if(a[i] > 0) { b[j].c = char(i + 'a'); j++; } perm(count, b, 0); return 0; }
OUTPUT : Enter first string: send Enter second string: more Enter third string: money Solution found : d = 1 e = 5 m = 0 n = 3 o = 8 r = 2 s = 7 y = 6 Solution found : d = 1 e = 7 m = 0 n = 3 o = 6 r = 4 s = 5 y = 8
![Page 35: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/35.jpg)
Program No. – 10
Problem – Breadth First Search
CODE
#include<bits/stdc++.h>
using namespace std;
struct box
{
int add;
box *next;
};
struct node
{
int key;
box *link;
};
int main()
{
int n;
cout << "Enter the no. of nodes: ";
cin >> n;
vector<node> a(n);
vector<int> col(n);
for(int i = 0; i < n; i++)
{
cout << "Enter the key for node " << i+1 << ": ";
cin >> a[i].key;
a[i].link = NULL;
}
for(int i = 0; i < n; i++)
![Page 36: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/36.jpg)
{
box *k,*p;
k = NULL;
int choice;
cout << "Enter node connected to node " << i+1 << "\n";
cout << "Enter 0 if no more: ";
cin >> choice;
while(choice != 0)
{
k = new box;
k->add = choice - 1;
k->next = NULL;
if(a[i].link == NULL)
a[i].link = k;
else
{
for(p = a[i].link; p->next != NULL; p = p->next);
p->next = k;
}
}
cout << "Enter node connected to node " << i+1 << "\n";
cout << "Enter 0 if no more: ";
cin >> choice;
}
queue <int> q;
for(int i = 0; i < n; i++)
{
if(col[i] != 2)
{
q.push(i);
col[i] = 1;
![Page 37: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/37.jpg)
}
while( !q.empty() )
{
int d = q.front();
q.pop();
cout << "\nVisited node is " << d+1 << " with key " << a[d].key;
col[d] = 2;
box *p;
for(p = a[d].link; p != NULL; p = p->next)
{
if(col[p->add] == 0)
{
col[p->add] = 1;
q.push(p->add);
}
}
}
}
return 0;
}
OUTPUT :
Enter the no. of nodes: 4
Enter the key for node 1: 9
Enter the key for node 1: 2
Enter the key for node 1: 4
Enter the key for node 1: 7
Enter node connected to node 1
Enter 0 if no more: 2
Enter node connected to node 1
Enter 0 if no more: 3
![Page 38: My AI File](https://reader034.vdocuments.net/reader034/viewer/2022042718/5695d0f31a28ab9b029488f2/html5/thumbnails/38.jpg)
Enter node connected to node 1
Enter 0 if no more: 0
Enter node connected to node 2
Enter 0 if no more: 4
Enter node connected to node 2
Enter 0 if no more: 1
Enter node connected to node 2
Enter 0 if no more: 0
Enter node connected to node 3
Enter 0 if no more: 4
Enter node connected to node 3
Enter 0 if no more: 0
Enter node connected to node 4
Enter 0 if no more: 1
Enter node connected to node 4
Enter 0 if no more: 3
Enter node connected to node 4
Enter 0 if no more: 0
Visited node is 1 with key 9
Visited node is 1 with key 2
Visited node is 1 with key 4
Visited node is 1 with key 7