Sunteți pe pagina 1din 46

Program:-1 Program to implement Tower of Hanoi #include<stdio.h> #include<conio.h> void TOH int n!char "!char #!

char $%& void main % ' int n& printf (n)nter num*er of plates:(%& scanf (+d(!,n%& TOH n-1!-.-!-/-!-0-%& getch %& 1 void TOH int n!char "!char #!char $% ' if n>2% ' TOH n-1!"!$!#%& printf (n+c -> +c(!"!#%& TOH n-1!$!#!"%& 131

Program:-4 Program to implement /56

#include<stdio.h> #include<conio.h>

char que[20]; int front=0, rear=0, n; char arr[20]; int bfs(int ); char ajMat[20][20]; char b[20]; void display(); int p=0;

int main() { char v; printf("Enter the number of nodes in a graph");

scanf("%d",&n); printf("Enter the value of node of graph"); for(int i=0; i<n; i++) { scanf("%s",&b[i]); }

printf("Enter the value in adjancency matrix in from of 'y' or 'n'\n"); printf("If there exits an edge between two vertices than 'y' otherwise 'n'\n"); for(int i=0; i<n; i++) printf(" %c ",b[i]); for(int i=0;i<n; i++) { printf("\n%c ",b[i]); for(int j=0; j<n; j++) { printf("%c ",v=getch());

ajMat[i][j]=v; } printf("\n\n"); } for(int i=0;i<n;i++) bfs(i);

display(); getch(); }

void display() { printf("BFS of Graph : "); for(int i=0; i<n; i++) printf("%c ",arr[i]); }

void insert(char val) { que[front]=val; front++; }

char del() { rear=rear+1; return que[rear-1]; }

bool unVisit(char val) { for(int i=0; i<front; i++) { if(val==que[i]) return false;

} return true; }

int bfs(int i) { char m; if(front==0) { insert(b[i]); } for(int j=0; j<n; j++) { if(ajMat[i][j]=='y') { if(unVisit(b[j])) { insert(b[j]);

} } } m=del(); arr[p]=m; p++; return 0;

Program:-3

Program to implement DFs

#include<stdio.h> #include<conio.h>

char stack[20]; int top=-1, n; char arr[20]; char dfs(int ); char ajMat[20][20]; char b[20]; void display(); int p=0;

int main() { char v; int l=0; printf("Enter the number of nodes in a graph");

scanf("%d",&n); printf("Enter the value of node of graph"); for(int i=0; i<n; i++) { scanf("%s",&b[i]); } char k=b[0]; printf("Enter the value in adjancency matrix in from of 'Y' or 'N'\n"); printf("\nIf there is an edge between the two vertices then enter 'Y' or 'N'\n"); for(int i=0; i<n; i++) printf(" %c ",b[i]); for(int i=0;i<n; i++) { printf("\n%c ",b[i]); for(int j=0; j<n; j++) { printf("%c ",v=getch());

ajMat[i][j]=v; } printf("\n\n"); } for(int i=0;i<n;i++) { l=0; while(k!=b[l]) l++; k=dfs(l); } display(); getch(); }

void display() { printf(" DFS of Graph : ");

for(int i=0; i<n; i++) printf("%c ",arr[i]); } void push(char val) { top=top+1; stack[top]=val; } char pop() { return stack[top]; }

bool unVisit(char val) { for(int i=0; i<p; i++) if(val==arr[i]) return false;

for(int i=0; i<=top; i++) if(val==stack[top]) return false; return true; }

char dfs(int i) { int k; char m; if(top==-1) { push(b[i]); } m=pop(); top--; arr[p]=m; p++;

for(int j=0; j<n; j++) { if(ajMat[i][j]=='y') { if(unVisit(b[j])) { push(b[j]); } } } return stack[top];

Program:-4

Program to implement iterative deepening search

// class Node.cs class Node { public int depth; public int State; public int Cost; public Node Parent; // Parent Node which has depth =0 and parent = null; public Node (int State) { this.State = State; this.Parent = null; this.depth = 0; } // another form of Node Constructor which accepts only the State; public Node(int State) { this.State = State; } // this form of Generalization of Node Constructor which accepts // any node root or ordinary node; public Node(int State,Node Parent) { this.State = State; this.Parent = Parent; if (Parent == null) this.depth = 0; else this.depth = Parent.depth + }

// this form of Generalization of Node Constructor which accept // any node root or ordinary node and accept the cost of each node; public Node(int State, Node Parent, int Cost) { this.State = State; this.Parent = Parent; this.Cost = Cost; if (Parent == null) this.depth = 0; else this.depth = Parent.depth + ; } class !etSucc { //if search go forward from 0 to num er n public "rra#$ist !etSussessor(int State) { "rra#$ist %esult = ne& "rra#$ist(); %esult."dd(' ( State + ); %esult."dd(' ( State + '); return %esult; }

//if search go ac!ward from n to num er 0 public "rra#$ist !etSussessor)%e*erse(int State) { "rra#$ist %esult = ne& "rra#$ist(); if (State + ' == 0) { int P = State , ' - ; %esult."dd(P); } else { int Sib = State + ; %esult."dd(Sib , ' - ); } return %esult; } //if the cost of each node must e determined public "rra#$ist !etSussessor(int State,Node Parent) { "rra#$ist %esult = ne& "rra#$ist(); %ando. n = ne& %ando.(); /est s = ne& /est(); %esult."dd(ne& Node('( State + ,Parent,n.Ne0t( , 00)+Parent.Cost)); %esult."dd(ne& Node('( State + ', Parent,n.Ne0t( , 00) + Parent.Cost)); %esult.Sort(s); return %esult; } }//end class //implement the interface "Comparer to compare o #ects in $rray%ist public class /est 1 2Co.parer { public int Co.pare(ob3ect 0, ob3ect #) { int *al = ((Node)0).Cost; int *al' = ((Node)#).Cost; if (*al 4= *al') return ; else return 0; }

1//end class Test


public static *oid 2terati*e)5eepenin6)Search(Node Start, Node !oal) { bool Cutt)off = false; int depth = 0; &hile(Cutt)off == false) { Console.7rite$ine(8Search !oal at 5epth {0}8,depth); 5epth)$i.ited)Search(Start, !oal, depth,ref Cutt)off); Console.7rite$ine(8-----------------------------8); depth++; } }//end method public static *oid 5epth)$i.ited)Search(Node Start, Node !oal,

int depth)$i.ite,ref bool Cut)off) { !etSucc 0 = ne& !etSucc(); "rra#$ist children = ne& "rra#$ist(); Stac9 :rin6e = ne& Stac9(); :rin6e.Push(Start); &hile (:rin6e.Count ;= 0) { Node Parent = (Node):rin6e.Pop(); Console.7rite$ine(8Node {0} <isited 8, Parent.State); // Console.&ead'ey(); if (Parent.State == !oal.State) { Console.7rite$ine(); Console.7rite$ine(8:ind !oal 8 + Parent.State); Cut)off = true; brea9; }//end if if (Parent.depth == depth)$i.ite) { continue; } else { children = 0.!etSussessor(Parent.State); for (int i = 0; i 4 children.Count; i++) { int State = (int)children=i>; Node /e. = ne& Node(State, Parent); :rin6e.Push(/e.); }//end for }//end else }//end while

1//end method

Program 5:Program to implement depth limited search


// class Node.cs class Node { public int depth; public int State; public int Cost; public Node Parent; // Parent Node which has depth =0 and parent = null; public Node (int State) { this.State = State; this.Parent = null; this.depth = 0; } // another form of Node Constructor which accepts only the State; public Node(int State) { this.State = State; } // this form of Generalization of Node Constructor which accepts // any node root or ordinary node; public Node(int State,Node Parent) { this.State = State; this.Parent = Parent; if (Parent == null) this.depth = 0; else this.depth = Parent.depth + }

// this form of Generalization of Node Constructor which accept // any node root or ordinary node and accept the cost of each node; public Node(int State, Node Parent, int Cost) { this.State = State; this.Parent = Parent; this.Cost = Cost; if (Parent == null) this.depth = 0; else this.depth = Parent.depth + ; }

1
class !etSucc { //if search go forward from 0 to num er n public "rra#$ist !etSussessor(int State) { "rra#$ist %esult = ne& "rra#$ist(); %esult."dd(' ( State + ); %esult."dd(' ( State + ');

return %esult; } //if search go ac!ward from n to num er 0 public "rra#$ist !etSussessor)%e*erse(int State) { "rra#$ist %esult = ne& "rra#$ist(); if (State + ' == 0) { int P = State , ' - ; %esult."dd(P); } else { int Sib = State + ; %esult."dd(Sib , ' - ); } return %esult; } //if the cost of each node must e determined public "rra#$ist !etSussessor(int State,Node Parent) { "rra#$ist %esult = ne& "rra#$ist(); %ando. n = ne& %ando.(); /est s = ne& /est(); %esult."dd(ne& Node('( State + ,Parent,n.Ne0t( , 00)+Parent.Cost)); %esult."dd(ne& Node('( State + ', Parent,n.Ne0t( , 00) + Parent.Cost)); %esult.Sort(s); return %esult; } }//end class //implement the interface "Comparer to compare o #ects in $rray%ist public class /est 1 2Co.parer { public int Co.pare(ob3ect 0, ob3ect #) { int *al = ((Node)0).Cost; int *al' = ((Node)#).Cost; if (*al 4= *al') return ; else return 0; }

1//end class Test


public static *oid 5epth)$i.ited)Search(Node Start, Node !oal, int depth)$i.ite) { !etSucc 0 = ne& !etSucc(); "rra#$ist children = ne& "rra#$ist(); Stac9 :rin6e = ne& Stac9(); :rin6e.Push(Start); &hile (:rin6e.Count ;= 0) { Node Parent = (Node):rin6e.Pop(); Console.7rite$ine(8Node {0} <isited 8, Parent.State); // Console.&ead'ey();

if (Parent.State == !oal.State) { Console.7rite$ine(); Console.7rite$ine(8:ind !oal 8 + Parent.State); brea9; }//end if if (Parent.depth == depth)$i.ite) { continue; } else { children = 0.!etSussessor(Parent.State); for (int i = 0; i 4 children.Count; i++) { int State = (int)children=i>; Node /e. = ne& Node(State, Parent); :rin6e.Push(/e.); }//end for }//end else }//end while

1//end method

Program 6:Program to implement tac toe problem

?include 4stdio.h@ int .ain (*oid) { int pla#er = 0; int &inner = 0; int choice = 0; int ro& = 0; int colu.n = 0; int line = 0; char board =A>=A> = { {B B,B'B,BAB}, {BCB,BDB,BEB}, {BFB,BGB,BHB}

};

for (int i = 0; i4H II &inner==0; { printf(8JnJn8); printf(8 +c K +c K +cJn8, board=0>='>); printf(8---K---K---Jn8); printf(8 +c K +c K +cJn8, board= >='>); printf(8---K---K---Jn8); printf(8 +c K +c K +cJn8, board='>='>); pla#er = i+' + do { the sLuare 8 ;

i++) board=0>=0>, board=0>= >, board= >=0>, board= >= >, board='>=0>, board='>= >,

printf(8JnPla#er +d, please enter the nu.ber of 8&here #ou &ant to place #our +c1 8, pla#er,(pla#er== )MBNB1BOB); scanf(8+d8, Ichoice); ro& = --choice,A; colu.n = choice+A; }&hile(choice40 KK choice@H KK board =ro&>=colu.n>@BHB); board=ro&>=colu.n> = (pla#er == ) M BNB 1 BOB;

='>) KK =0>))

if((board=0>=0>==board= >= > II board=0>=0>==board='> (board=0>='>==board= >= > II board=0>='>==board='> &inner = pla#er; else for(line = 0; line 4='; line++)

if((board=line>=0>==board=line>= > II board=line>=0>==board=line>='>)KK (board=0>=line>==board= >=line> II board=0>=line>==board='>=line>)) &inner = pla#er; } printf(8JnJn8); printf(8 +c K +c K +cJn8, board=0>=0>, board=0>= >, printf(8---K---K---Jn8); printf(8 +c K +c K +cJn8, board= >=0>, board= >= >, board= >='>); board='>='>); printf(8---K---K---Jn8); printf(8 +c K +c K +cJn8, board='>=0>, board='>= >,

board=0>='>);

if(&inner==0) printf(8/he 6a.e is a dra&Jn8); else printf(8Pla#er +d has &onJn8, &inner); return 0; }

Program:-7

Program to implement the decision tree Pac7age aima.util.learning.learners& 8mport 9ava.util.iterator& 8mport 9ava.util.list& 8mport aima.learner.framewor7.dataset& 8mport aima.learner.framewor7.e"ample& 8mport aima.learner.framewor7.learner& 8mport aima.learner.framewor7.constantdecisiontree& 8mport aima.learner.framewor7.decisiontree& 8mport aima.util.util& Pu*lic class decisiontreelearner implements learner ' Private decisiontree tree& Private string defaultvalue& Pu*lic decision treelearner % ' This.defaultvalue:;una*le to classif#;& 1 Pu*lic decisiontreelearner decisiontree tree!string defaultvalue% ' This.tree:tree& This.defaultvalue:defaultvalue& 1 Pu*lic void train dataset ds% ' <ist<string>attri*utes:ds.getnodetargetattri*utes %& This.tree:decisiontreelearning ds!attri*utes! new constantdecisiontree default value%%& 1 Pu*lic string predict e"ample e% ' =eturn string% tree.predict e%& 1 Pu*lic int>? test dataset ds% ' 8nt>? results:new int>? '2!21& 5or e"ample e:ds.e"amples% ' 8f e.targetvalue %.e@uals tree.predicts e%%% ' =esults>2?:result>2?A1& 1

)lse ' =esult>1?:results>1?A1B 1 1 =eturn results& 1 Private decisiontree decisiontreelearning dataset ds! list<string>attri*utesnames! constantdecisiontree defaulttree% ' 8f ds.si$e %::2% ' =eturn defaulttree& 1 8f alle"ampleshavesameclassification ds%% ' =eturn new constantdecisiontree ds.gete"ample 2%.targetvalue %%& 1 8f attri*utename.si$e %::2% ' =eturn ma9orit#value ds%& 1 6tring chosenattri*ute:chooseattri*utes ds!attri*utename%& Cecisiontree tree:new decisiontree chosenattri*utes%& 0onstantdecisiontree m:ma9orit#value ds%& <ist<string>values:ds.getpossi*leattri*utesvalues chosenattri*utes%& 5or string v:values% ' Cataset filtered:ds.matchingdataset chosenattri*utes!v%& <ist<string>newattri*s:util.removefrom%attri*utesname!chosenattri*utes%& Cecisiontree su*tree:decisiontreelearning filtered!newattri*s!m%& Tree.addnode v!su*tree%& 1 =eturn tree& 1 Private constantdecisiontree ma9orit#value dataset ds% ' <earner learner:new ma9orit#learner %& <eraner.trains ds%& =eturn new constantdecisontree learner.predict ds.gete"ample 2%%%& 1 Private string choosenattri*utes dataset ds!list<string>attri*utesnames% ' Cou*le greatestgain:2.2&

6tring attri*uteswithgreatesgain:attri*utesnames.get 2%& 5or string attr:attri*utenames% ' Cou*le gain:ds.calculategainfor attr%& 8f gain>greatesgain% ' Dreatestgain:gain& .ttri*uteswithgreatestgain:attr& 1 1 =eturn attri*utewithgreatestgain& 1 Private /oolean aale"ampleshavesameclassification dataset ds% ' 6tring classification:ds.gete"ample 2%.targetvalue %& 8terator<e"ample>iter:ds.iterator %& Ehile iter.hasne"t %% ' )"ample element:iter.ne"t %& 8f F element.targetvalue %.e@uals classification%%% ' =eturn false& 11 =eturn true& 1 Pu*lic decisiontree getdecisiontree % ' =eturn tree& 1 1

Program:-8 Program to implement hill climbing 8mport 9ava.io./uffer=eader& 8mport 9ava.io.8O)"ception& 8mport 9ava.io.8nput6tream=eader& 8mport 9ava.util.stac7& 0lass flightinfo ' 6tring from& 6tring to& 8nt distance& /oolean s7ip& 5lightinfo string f! string t! int d% ' 5rom : f& To:t& Cistance:d& 67ip:false& 11 Pu*lic class hill ' 5inal int G.H:122& 5lightinfo flight>?:new flightinfo>G.H?& 8nt numflight:2& 6tac7 *tstac7:new stac7 %& Pu*lic static void main 6tring args>?% ' 6tring to! from& Hill o*:new hill %& /ufferreader *r:new /ufferreader new 8nput6tream=eader s#stem.in%%& O*.setup %& Tr# ' 6#stem.out.print I5rom;%& 5rom : *r.readline %& 6#stem.out.print ITo;%& To:*r.readline %& O*.isflight from!to%& 8f o*.*tstac7.si$e %F:2% O*.route to%& 1 catch 8O)"ception e"e% ' 6#stem.out.println I)rror on input;%& 1 1 Joid setup % '

.ddflight .ddflight .ddflight .ddflight .ddflight .ddflight .ddflight .ddflight .ddflight .ddflight 1

IKew Lor7;! I0hicago;! M22%& I0hicago;! ICenver;! 1222%& IKew Lor7;! IToronto;! N22%& IKew Lor7;! ICenver;! 1O22%& IToronto;! I0algar#;! 1P22%& IToronto;! I<os .ngeles;! 4N22%& IToronto;! I0hicago;! N22%& ICenver;! IQr*ana;! 1222%& ICenver;! IHouston;! 1222%& IHouston;! I<os .ngeles;! 1N22%&

Joid addflight 6tring from! 6tring to! int dist% ' 8f numflight<G.H% ' 5light>num5light?:new flightinfo from! to! dist% KumflightAA& 1 )lse 6#stem.out.println flight data*ase full;%& 1 Joid route string to% ' 6tac7 rev:new stac7 %& 8nt dist:2& 5lightinfo f& 8nt num:*tstac7.si$e %& 5or int i:2&i<num&iAA% =ev.push *tstac7.pop %%& 5or int i:2&i<num&iAA% ' 5: flightinfo%rev.pop %& 6#stem.out.print f.fromA;to;%& CistA:f.distance& 1 6#stem.out.println to%& 6#stem.out.println Idistance is IAdist%& 1 8nt match string from! string to% ' 5or int i:numflight-1&i>-1&i--% ' 8f flight>i?.from.e@uals from% ,, flight>i?.to.e@uals to% ,, Fflight>i?.s7ip% ' 5light>i?.s7ip:true& =eturn flight>i?.distance& 1 1

5lightinfo find string from% ' 8nt pos:-1& 8nt dist:2& 5or int i:2&i<numflight&iAA% ' 8f flight>i?.from.e@ual from% ,, Fflight>i?.s7ip% ' 8f flight>i?.distance>dist% ' Pos:8& Cist:flight>i?.distance& 1 1 1 8f posF:-1% ' 5light>pos?.s7ip:true& 5lightinfo f:new flightinfo flight>pos?.from! flight>pos?.to!flight>pos?.distance%& =eturn f& 1 =eturn null& 1 Joid isflight string from! string to% ' 8nt dist& 5lightinfo f:null& Cist:match from!to%& 8f distF:2% ' /tstac7.push new flightinfo from!to!dist%%& =eturn& 1 8f fF:null% ' /tstac7.push new flightinfo from!to!f.distance%%& 8sflight f.to!to%& 1 else if *tstac7.si$e %>2% ' 5: flightinfo% *tstac7.pop %& 8sflight f.from!f.to%& 1 1

Program:-9 Program to implement A Algorithm

#include<iostream.h> #include<conio.h> #include<alloc.h> 6truct @ueue' 8nt arr>R?>R?& 8nt fn& Su>N2?& 1 T#pedef struct @ueue @ueue& 8nt front:-1& 8nt rear:-1& 8nt level:2& 8nt heuristic int s>R?>R?! int g>R?>R?%& Joid insert3node int node>R?>R?%& Joid e"plore3child @ueue s! int g>R?>R?%& Joid fn=C @ueue s! int g>R?>R?%& Joid fnQ< @ueue s! int g>R?>R?%& Joid fnQ= @ueue s! int g>R?>R?%& Joid fn<C @ueue s! int g>R?>R?%& Joid fnQ=< @ueue s! int g>R?>R?%& Joid fn=<C @ueue s! int g>R?>R?%& Joid fnQ=C @ueue s! int g>R?>R?%& Joid fnQ<C @ueue s! int g>R?>R?%& Joid fnQ=<C @ueue s! int g>R?>R?%& Joid main % ' 8nt state>R?>R?:'4!O!R!1!T!U!P!2!N1!goal>R?>R?:'1!4!R!2!O!U!P!T!N1& 8nt 8!9!h& 0lrscr %& 8nsert3node state%& H:heuristic state!goal%& 8f h::2% 0out<<;found goal;& )lse ' Su>font?.fn:hAlevel& <evelAA& )"plore3child @u>font?!goal%& 1 getch %& 1 8nt heuristic int s>R?>R?!int g>R?>R?% '

8nt 7:2& 5or int i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' 8f s>i?>9?::g>i?>9?% 0ontinue& )lse V:7A1& 1 =eturn 7%& 1 Joid insert3node int node>R?>R?% ' 8nt 8!9& 8f font::-1%,, rear::-1% ' 5rontAA& =earAA& 1 )lse 5rontAA& 5or i:2&i<R&iAA% 5or 9:2&9<R&9AA% ' Su>front?.arr>i?>9?:node>i?>9?& 1 0out<<;Wn;& 5or i:2&i<R&iAA% ' 5or 9:2&9<R&9AA% ' 0out<<@u>front?.arr>i?>9?& 1 0out<<;Wn;& 11 Joid e"plore3child @ueue s!int g>R?>R?% ' 8nt m!n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' 8f s.arr>i?>9?::2% ' G:8& K:9&

1 1 8f m::2 ,, n::2% ' fn=C s!g%& 1 8f m::2 ,, n::4% ' fn<C s!g%& 1 8f m::1 ,, n::2% ' fnQ=C s!g%& 1 8f m::1 ,, n::1% ' fnQ=<C s!g%& 1 8f m::1 ,, n::4% ' fnQ<C s!g%& 1 8f m::4 ,, n::2% ' fnQ= s!g%& 1 8f m::4 ,, n::1% ' fnQ=< s!g%& 1 8f m::4 ,, n::4% ' fnQ< s!g%& 11 Joid fn=C @ueue s!int g>R?>R?% ' 8nt child=>R?>R?!child>R?>R?!ch>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' child=>i?>9?:s.arr>i?>9?& childC>i?>9?:a.arr>i?>9?& 1

child=>2?>2?:s.arr>2?>1?& child=>2?>1?:s.arr>2?>2?& childC>2?>2?:s.arr>1?>2?& childC>1?>2?:s.arr>2?>2?& int 71!74& 71:heuristic child=!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child!g%& 8f 74::2% 0out<<;Wn found goal node at level;<<level& )lse ' 8f 71<74% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node child=%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node childC%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 111 Joid fnQ< @ueue s! int g>R?>R?% ' 8nt childQ>R?>R?! child<>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' childQ>i?>9?:s.arr>i?>9?& child<>i?>9?:a.arr>i?>9?& 1

childQ>4?>4?:s.arr>1?>4?& childQ>1?>4?:s.arr>4?>4?& child<>4?>4?:s.arr>4?>1?& child<>4?>1?:s.arr>4?>4?& int 71!74& 71:heuristic childQ!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child<!g%& 8f 74::2% 0out<<;Wn found goal node at level;<<level& )lse ' 8f 71<74%XX 71::74% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node childQ%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node child=%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 111

Joid fnQ= @ueue s! int g>R?>R?% ' 8nt child=>R?>R?! childQ>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' childQ>i?>9?:s.arr>i?>9?&

child=>i?>9?:a.arr>i?>9?& 1 childQ>4?>2?:s.arr>1?>2?& childQ>1?>2?:s.arr>4?>2?& child=>4?>2?:s.arr>4?>1?& child=>4?>1?:s.arr>4?>2?& int 71!74& 71:heuristic childQ!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child=!g%& 8f 74::2% 0out<<;Wn node selected with heuristic value:;<<71& )lse ' 8f 71<74%XX 71::74% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node childQ%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node child<%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 111 Joid fn<C @ueue s! int g>R?>R?% ' 8nt child<>R?>R?! childC>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' child<>i?>9?:s.arr>i?>9?&

childC>i?>9?:a.arr>i?>9?& 1 child<>2?>4?:s.arr>2?>1?& child<>2?>1?:s.arr>2?>4?& childC>2?>4?:s.arr>1?>4?& childC>1?>4?:s.arr>2?>4?& int 71!74& 71:heuristic child<!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic childC!g%& 8f 74::2% 0out<<;Wn node selected with heuristic value:;<<71& )lse ' 8f 71<74%XX 71::74% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node child<%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node childC%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 111 Joid fnQ<C @ueue s! int g>R?>R?% ' 8nt childQ>R?>R?! child<>R?>R?! childC>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' childQ>i?>9?:s.arr>i?>9?&

child<>i?>9?:a.arr>i?>9?& childC>i?>9?:a.arr>i?>9?& 1 childQ>1?>4?:s.arr>2?>4?& childQ>2?>4?:s.arr>1?>4?& child<>1?>4?:s.arr>1?>1?& child<>1?>1?:s.arr>1?>4?& childC>1?>4?:s.arr>4?>4?& childC>4?>4?:s.arr>1?>4?& int 71!74!7R& 71:heuristic childQ!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child<!g%& 8f 74::2% 0out<<;Wn found goal node at level;<<level& )lse ' VR:heuristic childC!g%& 8f 7R::2% 0out<<;Wn found goal node at level;<<level& )lse 8f 71<74%XX 71::74%%,, 71<7R%XX 71::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node childQ%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse if 74<71%,, 74<7R%XX 74::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node child<%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<7R& 8nsert3node childC%&

Su>front?.fn:7RAlevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 1111

Joid fn=<C @ueue s! int g>R?>R?% ' 8nt child=>R?>R?! child<>R?>R?! childC>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' child=>i?>9?:s.arr>i?>9?& child<>i?>9?:a.arr>i?>9?& childC>i?>9?:a.arr>i?>9?& 1 child=>2?>1?:s.arr>2?>4?& child=>2?>4?:s.arr>2?>1?& child<>2?>1?:s.arr>2?>2?& child<>2?>2?:s.arr>2?>1?& childC>2?>1?:s.arr>1?>1?& childC>1?>1?:s.arr>2?>1?& int 71!74!7R& 71:heuristic child=!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child<!g%& 8f 74::2% 0out<<;Wn found goal node at level;<<level& )lse ' VR:heuristic childC!g%& 8f 7R::2%

0out<<;Wn found goal node at level;<<level& )lse 8f 71<74%XX 71::74%%,, 71<7R%XX 71::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node child=%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse if 74<71%,, 74<7R%XX 74::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node child<%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<7R& 8nsert3node childC%& Su>front?.fn:7RAlevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 1111 Joid fnQ=C @ueue s! int g>R?>R?% ' 8nt childQ>R?>R?! child=>R?>R?! childC>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' childQ>i?>9?:s.arr>i?>9?& child=>i?>9?:a.arr>i?>9?& childC>i?>9?:a.arr>i?>9?& 1 childQ>1?>2?:s.arr>2?>2?& childQ>2?>2?:s.arr>1?>2?&

child=>1?>2?:s.arr>1?>1?& child=>1?>1?:s.arr>1?>2?& childC>1?>2?:s.arr>4?>2?& childC>4?>2?:s.arr>1?>2?& int 71!74!7R& 71:heuristic childQ!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child=!g%& 8f 74::2% 0out<<;Wn found goal node at level;<<level& )lse ' VR:heuristic childC!g%& 8f 7R::2% 0out<<;Wn found goal node at level;<<level& )lse 8f 71<74%XX 71::74%%,, 71<7R%XX 71::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node childQ%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse if 74<71%,, 74<7R%XX 74::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node child=%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<7R& 8nsert3node childC%& Su>front?.fn:7RAlevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA&

)"plore3child n!g%& 1111 Joid fnQ=< @ueue s! int g>R?>R?% ' 8nt childQ>R?>R?! child=>R?>R?! child<>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' childQ>i?>9?:s.arr>i?>9?& child=>i?>9?:a.arr>i?>9?& child<>i?>9?:a.arr>i?>9?& 1 childQ>4?>1?:s.arr>1?>1?& childQ>1?>1?:s.arr>4?>1?& child=>4?>1?:s.arr>4?>4?& child=>4?>4?:s.arr>4?>1?& child<>4?>1?:s.arr>4?>2?& child<>4?>2?:s.arr>4?>1?& int 71!74!7R& 71:heuristic childQ!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child=!g%& 8f 74::2% 0out<<;Wn found goal node at level;<<level& )lse ' VR:heuristic child<!g%& 8f 7R::2% 0out<<;Wn found goal node at level;<<level& )lse 8f 71<74%XX 71::74%%,, 71<7R%XX 71::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node childQ%& Su>front?.fn:71Alevel&

0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse if 74<71%,, 74<7R%XX 74::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node child=%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<7R& 8nsert3node child<%& Su>front?.fn:7RAlevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 1111 Joid fnQ=<C @ueue s! int g>R?>R?% ' 8nt childQ>R?>R?! child=>R?>R?! child<>R?>R?! childC>R?>R?& Sueue n& 5or i:2&i<R&iAA% 5or int 9:2&9<R&9AA% ' ' childQ>i?>9?:s.arr>i?>9?& child=>i?>9?:a.arr>i?>9?& child<>i?>9?:a.arr>i?>9?& childC>i?>9?:a.arr>i?>9?& 1 childQ>1?>1?:s.arr>2?>1?& childQ>2?>1?:s.arr>1?>1?& child=>1?>1?:s.arr>1?>4?& child=>1?>4?:s.arr>1?>1?& child<>1?>1?:s.arr>1?>2?& child<>1?>2?:s.arr>1?>1?& childC>1?>1?:s.arr>4?>1?& childC>4?>1?:s.arr>1?>1?&

int 71!74!7R!7U& 71:heuristic childQ!g%& if 71::2% cout<<;Wn found goal node at level;<<level& else ' V4:heuristic child=!g%& 8f 74::2% 0out<<;Wn found goal node at level;<<level& )lse ' VR:heuristic child<!g%& 8f 7R::2% 0out<<;Wn found goal node at level;<<level& )lse ' VU:heuristic childC!g%& 8f 7U::2% 0out<<;Wn found goal node at level;<<level& )lse 8f 71<74%XX 71::74%%,, 71<7R%XX 71::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<71& 8nsert3node childQ%& Su>front?.fn:71Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse if 74<71%,, 74<7R%XX 74::7R%%% ' 0out<<;Wn node selected with heuristic value:;<<74& 8nsert3node child=%& Su>front?.fn:74Alevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse if 7R<71%,, 7R<74% ' 0out<<;Wn node selected with heuristic value:;<<7R& 8nsert3node child<%& Su>front?.fn:7RAlevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 )lse ' 0out<<;Wn node selected with heuristic value:;<<7R&

8nsert3node childC%& Su>front?.fn:7UAlevel& 0out<<;Wn and fn value:;<<@u>front?.fn& 1 K:@u>front?& <evelAA& )"plore3child n!g%& 11 Sueue compare % ' 8nt temp:front& Sueue node& Ehile tempF:-1% ' 8f @u>temp?.fn<@u>--temp?.fn% Kode:@u>temp?& 1 0out<<;Wn selected node:Wn;& 5or int i:2&i<R&iAA% ' 5or int 9:2&9<R&9AA% ' 0out<<node.arr>i?>9?& 0out<<;Wn its heuristics value is:;<<node.fn& 1 0out<<;Wn;& 1 )mpt#3@ueue %& =eturn node%& 1

Program:-!" Program to implement 8 P#$$le Problem


?ifndef S/"/)P)2NC$Q5R5 ?define S/"/)P)2NC$Q5R5 ?include 4iostrea.@ struct Stat { int Soard=A>=A>; int depth; int R.pt#='>; int R*aluation; int operator- (Stat) const; ,,O*erloadin6 operator- to return 8Pa..in6 distance8 *oid operator= (Stat); bool operator== (Stat) const; bool operator4 (Stat) const; }; ,,Qsed as a co.pare class for the set container class Co.paror ,,Class to Co.pare bet&een Stats(5efault Si66er) { bool Si66er; public1 Co.paror(const boolI S.aller= ) { Si66er=S.allerM01 ; } bool operator() (const StatI lhs, const StatI rhs) const { if (Si66er) return (lhs4rhsM01 ); else return (lhs4rhs); } }; std11istrea.I operator@@ (std11istrea.I,StatI); ,,/o input the H-cells as one std11ostrea.I operator44 (std11ostrea.I,StatI); ,,/o output the H-cells as one ?endif ,, S/"/)P)2NC$Q5R5

?include ?include ?include ?include ?include

4*ector@ 4set@ 4al6orith.@ 4fstrea.@ 8Stat.h8

usin6 na.espace std; ,,!lobal <ariables Stat Start,!oal,te.p;

int Steps=0; set4Stat,Co.paror@ Open; ,,Qsed so that the Stats &ill be arran6ed -b# heuristic *alue- and uniLue *ector4Stat@ Closed; ,,:or&ard 5eclaration int Sol*er(); inline int !enerate)To*e(char); int .ain() { ifstrea. cin(8input.t0t8); cin@@Start@@!oal; Start.depth=0; Start.R*aluation=Start-!oal; Open.insert(Start); Sol*er(); cout44te.p44endl; cout44endl448Setps to reach the 6oal = 844Steps44endl; return 0;

int Sol*er() { set4Stat,Co.paror@11iterator it=Open.be6in(); te.p=(it; if(te.p==!oal) return Steps; cout44te.p44endl; Closed.push)bac9(te.p); Open.erase(it); Start=te.p; if(te.p.R.pt#=0>4') ,,Qp 5irection !enerate)To*e(BQB); if(te.p.R.pt#=0>@0) ,,5o&n 5irection !enerate)To*e(B5B); if(te.p.R.pt#= >4') ,,%i6ht 5irection !enerate)To*e(B%B); if(te.p.R.pt#= >@0) ,,$eft 5irection !enerate)To*e(B$B); Steps++; Sol*er();

inline int !enerate)To*e(char 5irection) { int 2nde0,2n*erse,%o&,Colou.; int R0=te.p.R.pt#=0>,R =te.p.R.pt#= >; if(5irection == BQB){2nde0= ;2n*erse=0;} else if(5irection == B5B){2nde0=- ;2n*erse=0;} else if(5irection == B%B){2nde0=0;2n*erse= ;} else if(5irection == B$B){2nde0=0;2n*erse=- ;}

%o&=R0+2nde0; Colou.=R +2n*erse; s&ap(te.p.Soard=R0>=R >,te.p.Soard=%o&>=Colou.>); ,,S&appin6 the e.pt# cell &ith an ad3acent cell if(find(Closed.be6in(),Closed.end(),te.p);=Closed.end()) { te.p=Start; return 0; } ,,Chan6in6 the place of e.pt# cell to the ne& place te.p.R.pt#=0>=%o&; te.p.R.pt#= >=Colou.; te.p.depth++; ,,2ncreasin6 the depth of the stat ,,Settin6 the heuristic *alue of the stat te.p.R*aluation=!oal-te.p; te.p.R*aluation+=te.p.depth; Open.insert(te.p); te.p=Start; return 0; }

S-ar putea să vă placă și