You are on page 1of 33

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 subproblems. 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<iostream> #include<conio.h> #include<queue> using namespace std; struct status{ int state; int a[3][3]; int 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()) { 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; //cout<<s2.state<<" "; 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); //cout<<"+"; } } 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)){ (s2.a)[s2.curx][s2.cury+1]=(s2.a)[s2.curx][s2.cury]; (s2.a)[s2.curx][s2.cury]=0; q.push(s2); //cout<<"+"; //cout<<"there"; } } } } } 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"; } cout<<"\n\n\nnext state :\n\n"; for(int k=0;k<3;k++){ for(int j=0;j<3;j++) cout<<" "<<s.a[k][j]; cout<<"\n"; } getch(); } getch(); return 0; }

OUTPUT

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 subproblems. 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.

So on the basis of the analysis of the problem based on these seven characteristics breadth first search technique is used.

CODE
#include<iostream> #include<conio.h> #include<queue> 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; 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))){ 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{ 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){ 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); getch(); return 0; }

OUTPUT

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 subproblems. 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.

So on the basis of the analysis of the problem based on these seven characteristics nearest neighbor heuristics technique is used.

CODE
#include<iostream> #include<conio.h> #include<queue> using namespace std; int main(){ int n,d; cout<<"Enter the no. of cities :"; cin>>n; vector < vector<int> > q(n,n),s(n,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++) t[j]=0; t[i]=1; int min,r,k;r=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++) 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]; getch(); return 0; }

OUTPUT

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 subproblems. 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.

So, on the basis of the analysis of the problem based on these seven characteristics heuristic function is designed and used.

CODE
#include <iostream> #include <conio.h> using namespace std; void draw(int board[][3]); int heuristic(int board[][3], int*, int*); int finish(int board[][3], int); int main(int argc, char *argv[]) { 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++) // Set biggestScore[9][3] to all zeros.

for (int j = 0; j < 3; j++) biggestScore[i][j] = 0; if(turn == 9) // No more sqares left if(finish(board, tie)) break; else { game++; turn = 0; continue; } else if((game + turn) % 2 == 0) { draw(board); cout << "\n\nSelect position to make move (1-9): "; cin >> choice; if(choice >= 1 && choice <= 9) { // Change user input to row/column for use in 2D board array. row = (int) (choice - 1) / 3; column = (choice - 1) % 3; // Check user's input 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++) for(int y = 0; y < 3; y++) if(!board[x][y]) // Square is empty { 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++; } } // End of if(choice > 0 .. } // End of while(!quit) { ... return 0; } // End of main() { ... 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) 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"; } } } // End of draw() ... int heuristic(int board[][3],int *x,int *y) { int score; int scores[9] = {/*No Pieces on line */ 2, /*Undefined*/ 0, /*One human piece*/ -1, /*One computer piece*/ 3, /*Two human pices*/ 10, /*One of each*/ 2, /*Two computer OR three human*/ 51, /*Two human and one computer */ 0, /*Two computer and one human */ 0 }; score = scores[(board[*x][0] + board[*x][1] + board[*x][2])]; // horizontal score score += scores[(board[0][*y] + board[1][*y] + board[2][*y])]; //vertical score: if(*x == *y) // diagonal score 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;

} // End of heuristic() ... 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') { for (int i = 0; i < 3; i++) //reset board for (int j = 0; j < 3; j++) board[i][j] = 0; return 0; } else return 1; }

OUTPUT

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 subproblems. 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<iostream> #include<conio.h> using namespace std; int gcd(int x,int y){ if(y%x == 0) return(x); return(gcd(y%x,x)); } 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<<"\n No possible solution"; getch(); return 0; } while(y!=z){ 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"; getch(); return 0; }

OUTPUT

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 subproblems. 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.

#include <iostream> #include <conio.h> using namespace std; #define TRUE 1 #define FALSE 0 static short int board[8][8]; int check(int); void drawboard(); 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) return (FALSE); } }

CODE

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--; } } 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 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(); getch(); return 0; } 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; } 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"); }

OUTPUT

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 subproblems. 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<iostream> #include<conio.h> using namespace std; struct status{ int chair,stick,onchair,gotbanana; }; int main() { status s; cout<<"the solution for monkey banana problem :\n\n"; s.chair=0;s.stick=0; s.onchair=0;s.gotbanana=0; if(s.chair==0) cout<<"\nput chair under banana"; s.chair=1; if(s.stick==0) cout<<"\ngrab the stick"; s.stick=1; if(s.onchair==0) cout<<"\nstand on the chair"; s.onchair=1; if(s.gotbanana==0) cout<<"\nget banana with stick"; s.gotbanana=1; getch(); return 0; }

OUTPUT

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 subproblems. 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<iostream> #include<conio.h> #include<vector> using namespace std; struct node{ int from,to,aux; }; int main(){ int n; cout<<"Enter the no. of disks"; cin>>n; int m=1; m = m << n; m--; vector <node> a(m); int k=m; m=(m-1)/2; a[m].from=1;a[m].aux=2;a[m].to=3; for(int j=0;j<n-1;j++){ int p=1; int r=(m-1)>>1; int 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<<"\nfrom peg "<<a[i].from<<" to peg "<<a[i].to; } getch(); return 0; }

OUTPUT

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 subproblems. 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<iostream> #include<conio.h> #include<vector> #include<alloc.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--){ 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++) ++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"; getch(); 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); getch(); return 0; }

OUTPUT

Program No. 10
Breadth First Search

CODE
#include<iostream> #include<conio.h> #include<queue> using namespace std; struct box; struct node{ int key; box *link; }; struct box{ int add; box *next; }; 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++){ 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; } while(!q.empty()){ int d=q.front(); q.pop(); cout<<"\n visited 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); } } } } getch(); return 0; }

OUTPUT

ARTIFICIAL INTELLIGENCE
Practical Work Record

Submitted byDivyanshu Rastogi 236/CO/09

TABLE OF CONTENTS
1. Tower Of Hanoi 2.Water Jug Problem 3.Monkey Banana Problem 4.Eight Puzzle 5.Tic Tac Toe 6.Missionaries cannibals 7.Travelling salesman 8.Chess 8-Queen

9.Cryptarithmetic. 10. Breadth First Search

You might also like