人工智能實(shí)驗(yàn)報(bào)告完整版八數(shù)碼+驗(yàn)證(共36頁)_第1頁
人工智能實(shí)驗(yàn)報(bào)告完整版八數(shù)碼+驗(yàn)證(共36頁)_第2頁
人工智能實(shí)驗(yàn)報(bào)告完整版八數(shù)碼+驗(yàn)證(共36頁)_第3頁
人工智能實(shí)驗(yàn)報(bào)告完整版八數(shù)碼+驗(yàn)證(共36頁)_第4頁
人工智能實(shí)驗(yàn)報(bào)告完整版八數(shù)碼+驗(yàn)證(共36頁)_第5頁
已閱讀5頁,還剩33頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、把數(shù)碼問題(wnt)就是把一串?dāng)?shù)字變成下面這個(gè)樣子:1 2 38 0 4 7 6 5實(shí)現(xiàn)(shxin)方法1.過程(guchng)表示源代碼:#includestatic int style9=1,2,3,6,7,4,5,8,0;/輸入的數(shù)碼/2,5,4,3,0,7,1,8,6 3,2,1,8,0,4,7,6,5 1,0,4,2,7,3,8,5,6 1,0,3,8,2,4,7,6,5static int arrayStep416=5,4,3,6,7,8;/第四步和第六步共用的數(shù)組,所以設(shè)為全局量static int arrayStep714=3,6,7,4;static int local;/

2、空格的位置int i,j;/所用到的變量int number=0;/記錄移動(dòng)步數(shù)void print();void step1();void step2();void step3();void step4();void step5();void step6();void step7();void step8();void step9();void exchange(int x,int y);void judge();void judge()/判斷空格位置number = 0;for(i=0;i9;i+)if(stylei=0)local=i;return;void exchange(int x

3、,int y)/交換(jiohun)兩個(gè)數(shù)int temp;print();temp=stylex;stylex=styley;styley=temp;local=y;number+;void step1()int arrayStep115=3,0,1,2,5;int arrayStep126=6,7,8,5,2,1;if(style2!=0)&style2!=1)return;elseif(local=2)if(style1=1)exchange(2,5);elseexchange(2,1);return;elseif(local=4)exchange(4,1);i=2;while(loca

4、l!=5)exchange(arrayStep11i,arrayStep11i+1);i+;return;for(i=0;i3;i+)if(arrayStep11i=local)while(local!=5)exchange(arrayStep11i,arrayStep11i+1);i+;return;for(i=0;i4;i+)if(arrayStep12i=local)while(local!=1)exchange(arrayStep12i,arrayStep12i+1);i+;return;return;void step2()int arrayStep218=0,3,6,7,8,5,4

5、,1;for(i=0;i8;i+)if(arrayStep21i=local)while(style0!=1)exchange(arrayStep21i%8,arrayStep21(i+1)%8);i+;break;void step3()int arrayStep318=2,1,4,3,6,7,8,5;for(i=0;i8;i+)if(arrayStep31i=local)while(style1!=2)exchange(arrayStep31i%8,arrayStep31(i+1)%8);i+;break;void step4()for(i=0;i6;i+)if(arrayStep41i=

6、local)while(style4!=3)exchange(arrayStep41i%6,arrayStep41(i+1)%6);i=(i+1)%6;while(local!=3)exchange(arrayStep41i%6,arrayStep41(i+5)%6);i=(i+5)%6;break;void step5()int arrayStep519=3,0,1,4,5,2,1,0,3;i=0;doexchange(arrayStep51i,arrayStep51i+1);i+;while(local!=3);void step6()for(i=0;i6;i+)if(arrayStep4

7、1i=local)while(style5!=4)exchange(arrayStep41i%6,arrayStep41(i+1)%6);i+;if(local=8)exchange(8,7);break;return;void step7()for(i=0;i4;i+)if(arrayStep71i=local)while(style4!=5)exchange(arrayStep71i%4,arrayStep71(i+1)%4);i=(i+1)%4;while(local!=3)exchange(arrayStep71i%4,arrayStep71(i+3)%4);i=(i+3)%4;bre

8、ak;void step8()int arrayStep8113=3,0,1,2,5,4,7,8,5,2,1,0,3;i=0;doexchange(arrayStep81i,arrayStep81i+1);i+;while(local!=3);void step9()for(i=0;i4;i+)if(arrayStep71i=local)while(style7!=6)exchange(arrayStep71i%4,arrayStep71(i+1)%4);i=(i+1)%4;while(local!=4)exchange(arrayStep71i%4,arrayStep71(i+3)%4);i

9、=(i+3)%4;break;void print()for(j=0;j9;j+)if(stylej=0)printf( t);elseprintf(%dt,stylej);if(j+1)%3=0)printf(n);printf(* %d *n,number);void loop()printf(請(qǐng)輸入(shr)數(shù)碼:n);for(i=0;i9;i+)scanf(%d,&stylei);judge();step1();step2();step3();if(style2!=3)step4();step5();step6();if(style8!=5)step7();step8();step9(

10、);print();if(!(style3=8)&(style6=7)printf(用書上所給算法(sun f)來看此數(shù)碼錯(cuò)誤!n);void main()while(1)loop();深度優(yōu)先(yuxin)實(shí)現(xiàn)/*說明(shumng)* 用寬度優(yōu)先搜索算法實(shí)現(xiàn)八數(shù)碼問題 */#include#include#include#include #include string.h #include assert.h #include windows.h using namespace std;int wholeStyle9 = 2,8,3,1,6,4,7,0,5;int standard19 =

11、1,2,3,8,0,4,7,6,5;int local,i,j;int startKey = 0,endKey = 0,equalKey = 1,tempSpace;struct node *openHead,*open; /open表struct node *closedHead,*closed; /closed表struct node *tempNode; /臨時(shí)(ln sh)節(jié)點(diǎn)struct node *answer; /找到的路徑(ljng)int num = 0;struct nodeint style9;struct node *next;struct node *father;v

12、oid updateData() /更新(gngxn)要判斷數(shù)據(jù)int i;printf(請(qǐng)輸入八數(shù)碼原始狀態(tài):n);for(i = 0;i 9;i+)scanf(%d,&wholeStylei);printf(請(qǐng)輸入八數(shù)碼最終狀態(tài):n);for(i = 0;i 9;i+)scanf(%d,&standard1i);void judge1(struct node *head)/判斷空格位置for(i = 0;i stylei = 0)local = i;return;int judge2(struct node *head) /判斷是否與標(biāo)準(zhǔn)八數(shù)碼相等,不相等返回值為0for(i = 0;i

13、stylei != standard1i)if(i = 3)&(head-style3 = standard16);else if(i = 6)&(head-style6 = standard13);else return 0;return 1;void judge3() /判斷新生成的八數(shù)碼是否就是最終狀態(tài)(zhungti)或者在open、closed表中出現(xiàn)if(judge2(tempNode)endKey = 1;elsewhile(openHead-next-next-style0 != 9)for(i = 0;i next-next-stylei != tempNode-stylei

14、)equalKey = 1;break;elseequalKey = 0;if(equalKey)/不相等(xingdng)openHead = openHead-next;elsebreak;openHead = open-next;if(equalKey)/不相等(xingdng)while(closedHead-next-style0 != 9)for(i = 0;i next-stylei != tempNode-stylei)equalKey = 1;break;elseequalKey = 0;if(!equalKey)/相等(xingdng)break;elseclosedHea

15、d = closedHead-next;closedHead = closed-next;if(equalKey)/不相等(xingdng)open-next = tempNode;tempNode-next = openHead;tempNode-father = openHead-next;open = open-next;void print(struct node *temp) /輸出(shch)八數(shù)碼表for(j = 0;j stylej = 0)printf( t);elseprintf(%dt,temp-stylej);if(j + 1) % 3 = 0)printf(n);vo

16、id write2txt()ofstream out(F:out_details.txt,ios:app);if( out.fail() )cerr未找到文件endl;out 第+num步n;outnext-style0 != 9)for(i = 0;i 9;i +)outnext-styleit;if(i+1) % 3 = 0)outn;outnext;openHead = openHead-next;out*n;outnext-style0 != 9)for(i = 0;i 9;i +)outnext-styleit;if(i + 1) % 3 =0)outn;outnext;closed

17、Head = closedHead-next;out-n;out.close();void main()/updateData();/輸入(shr)八數(shù)碼數(shù)據(jù)for(i = 0;i style0 = 9;openHead-next = open;for(i = 0;i stylei = wholeStylei;open-next = openHead;open-father = openHead;closedHead = new node();closed = new node();closedHead-style0 = 9;closedHead-next = closedHead;close

18、d = closedHead;while(open-style0 != 9)/當(dāng)open表不為空時(shí)一直(yzh)循環(huán)judge1(openHead-next);if(local % 3 0)/右移equalKey = 1;tempNode = new node();for(i = 0;i stylei = openHead-next-stylei;tempSpace = tempNode-stylelocal - 1;tempNode-stylelocal - 1 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if

19、(endKey)break;if(local 2)/下移equalKey = 1;tempNode = new node();for(i = 0;i stylei = openHead-next-stylei;tempSpace = tempNode-stylelocal - 3;tempNode-stylelocal - 3 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if(endKey)break;if(local % 3 2)/左移equalKey = 1;tempNode = new node();for

20、(i = 0;i stylei = openHead-next-stylei;tempSpace = tempNode-stylelocal + 1;tempNode-stylelocal + 1 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if(endKey)break;if(local 6)/上移equalKey = 1;tempNode = new node();/tempNode = malloc(sizeof(struct node);for(i = 0;i stylei = openHead-next

21、-stylei;tempSpace = tempNode-stylelocal + 3;tempNode-stylelocal + 3 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if(endKey)break;closed-next = openHead-next;/把open的標(biāo)頭添加(tin ji)到closed表中openHead-next = openHead-next-next; closed = closed-next;closed-next = closedHead;write2txt();ope

22、n-next = tempNode;/把找到的新節(jié)點(diǎn)(ji din)添加到open表中tempNode-next = openHead;tempNode-father = openHead-next;open = open-next;closed-next = openHead-next;/把open的標(biāo)頭添加(tin ji)到closed表中openHead-next = openHead-next-next; closed = closed-next;closed-next = closedHead;write2txt();answer = new node();tempNode = ne

23、w node();tempNode = open;while(tempNode-style0 != 9)/將結(jié)果路徑存于answeranswer = tempNode;tempNode = tempNode-father;tempNode-next = answer;num = 0;while(answer-next-style0 != 9)/輸出answerprintf(*第%d步*n,num+);print(answer);answer = answer-next;printf(n);printf(*第%d步*n,num+);print(answer);if(answer-style3 !

24、= standard13)printf(n!輸入的八數(shù)碼不合法,不能從初始狀態(tài)到最終狀態(tài)nnnn);return;寬度(kund)優(yōu)先:/*說明(shumng)* 用寬度(kund)優(yōu)先搜索算法實(shí)現(xiàn)八數(shù)碼問題 */#include#include#include#include #include string.h #include assert.h #include windows.h using namespace std;int wholeStyle9 = 2,8,3,1,6,4,7,0,5;int standard19 = 1,2,3,8,0,4,7,6,5;int local,i,j;

25、int startKey = 0,endKey = 0,equalKey = 1,tempSpace;struct node *openHead,*open; /open表struct node *closedHead,*closed; /closed表struct node *tempNode; /臨時(shí)節(jié)點(diǎn)struct node *answer; /找到的路徑int num = 0;struct nodeint style9;struct node *next;struct node *father;void updateData() /更新要判斷數(shù)據(jù)int i;printf(請(qǐng)輸入八數(shù)碼原

26、始狀態(tài):n);for(i = 0;i 9;i+)scanf(%d,&wholeStylei);printf(請(qǐng)輸入八數(shù)碼最終狀態(tài):n);for(i = 0;i 9;i+)scanf(%d,&standard1i);void judge1(struct node *head)/判斷(pndun)空格位置for(i = 0;i stylei = 0)local = i;return;int judge2(struct node *head) /判斷是否與標(biāo)準(zhǔn)(biozhn)八數(shù)碼相等,不相等返回值為0for(i = 0;i stylei != standard1i)if(i = 3)&(head-

27、style3 = standard16);else if(i = 6)&(head-style6 = standard13);else return 0;return 1;void judge3() /判斷新生成的八數(shù)碼是否(sh fu)就是最終狀態(tài)或者在open、closed表中出現(xiàn)if(judge2(tempNode)endKey = 1;elsewhile(openHead-next-next-style0 != 9)for(i = 0;i next-next-stylei != tempNode-stylei)equalKey = 1;break;elseequalKey = 0;if

28、(equalKey)/不相等(xingdng)openHead = openHead-next;elsebreak;openHead = open-next;if(equalKey)/不相等(xingdng)while(closedHead-next-style0 != 9)for(i = 0;i next-stylei != tempNode-stylei)equalKey = 1;break;elseequalKey = 0;if(!equalKey)/相等(xingdng)break;elseclosedHead = closedHead-next;closedHead = closed

29、-next;if(equalKey)/不相等open-next = tempNode;tempNode-next = openHead;tempNode-father = openHead-next;open = open-next;void print(struct node *temp) /輸出八數(shù)碼表for(j = 0;j stylej = 0)printf( t);elseprintf(%dt,temp-stylej);if(j + 1) % 3 = 0)printf(n);void write2txt()ofstream out(F:out_details.txt,ios:app);

30、if( out.fail() )cerr未找到文件(wnjin)endl;out 第+num步n;outnext-style0 != 9)for(i = 0;i 9;i +)outnext-styleit;if(i+1) % 3 = 0)outn;outnext;openHead = openHead-next;out*n;outnext-style0 != 9)for(i = 0;i 9;i +)outnext-styleit;if(i + 1) % 3 =0)outn;outnext;closedHead = closedHead-next;out-n;out.close();void m

31、ain()/updateData();/輸入八數(shù)碼(shm)數(shù)據(jù)for(i = 0;i style0 = 9;openHead-next = open;for(i = 0;i stylei = wholeStylei;open-next = openHead;open-father = openHead;closedHead = new node();closed = new node();closedHead-style0 = 9;closedHead-next = closedHead;closed = closedHead;while(open-style0 != 9)/當(dāng)open表不為

32、空時(shí)一直(yzh)循環(huán)judge1(openHead-next);if(local % 3 0)/右移(yu y)equalKey = 1;tempNode = new node();for(i = 0;i stylei = openHead-next-stylei;tempSpace = tempNode-stylelocal - 1;tempNode-stylelocal - 1 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if(endKey)break;if(local 2)/下移equalKey = 1;

33、tempNode = new node();for(i = 0;i stylei = openHead-next-stylei;tempSpace = tempNode-stylelocal - 3;tempNode-stylelocal - 3 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if(endKey)break;if(local % 3 2)/左移equalKey = 1;tempNode = new node();for(i = 0;i stylei = openHead-next-stylei;te

34、mpSpace = tempNode-stylelocal + 1;tempNode-stylelocal + 1 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if(endKey)break;if(local 6)/上移equalKey = 1;tempNode = new node();/tempNode = malloc(sizeof(struct node);for(i = 0;i stylei = openHead-next-stylei;tempSpace = tempNode-stylelocal +

35、 3;tempNode-stylelocal + 3 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;judge3();if(endKey)break;closed-next = openHead-next;/把open的標(biāo)頭添加(tin ji)到closed表中openHead-next = openHead-next-next; closed = closed-next;closed-next = closedHead;write2txt();open-next = tempNode;/把找到的新節(jié)點(diǎn)(ji din)添加到open

36、表中tempNode-next = openHead;tempNode-father = openHead-next;open = open-next;closed-next = openHead-next;/把open的標(biāo)頭添加(tin ji)到closed表中openHead-next = openHead-next-next; closed = closed-next;closed-next = closedHead;write2txt();answer = new node();tempNode = new node();tempNode = open;while(tempNode-s

37、tyle0 != 9)/將結(jié)果(ji gu)路徑存于answeranswer = tempNode;tempNode = tempNode-father;tempNode-next = answer;num = 0;while(answer-next-style0 != 9)/輸出(shch)answerprintf(*第%d步*n,num+);print(answer);answer = answer-next;printf(n);printf(*第%d步*n,num+);print(answer);if(answer-style3 != standard13)printf(n!輸入的八數(shù)碼

38、(shm)不合法,不能從初始狀態(tài)到最終狀態(tài)nnnn);return;A算法/*說明* * A算法實(shí)現(xiàn)八數(shù)碼問題 * */#include#include#includeusing namespace std;void print(struct node *temp);int wholeStyle9 = 2,8,3,1,6,4,7,0,5;int standard19 = 1,2,3,8,0,4,7,6,5;int local,i,j;int tempSpace;struct node *openHead,*open; /open表struct node *closedHead,*closed;

39、 /closed表struct node *tempNode; /臨時(shí)節(jié)點(diǎn)struct node *answer; /找到的路徑(ljng)int num = 0;bool endKey;struct nodeint depth;int judgement_based;int style9;struct node *next;struct node *father;void updateData() /更新(gngxn)要判斷數(shù)據(jù)int i;cout請(qǐng)輸入(shr)八數(shù)碼原始狀態(tài):endl;for(i = 0;i wholeStylei;cout請(qǐng)輸入八數(shù)碼最終狀態(tài):endl;for(i =

40、0;i standard1i;int judge1(struct node *head)/判斷空格位置for(i = 0;i stylei = 0)return i;int evaluation_function(struct node *head) /計(jì)算不在正確位置的點(diǎn)的個(gè)數(shù)int not_correct_position = 0;for(i = 0;i stylei != 0)&(head-stylei != standard1i)not_correct_position+;return not_correct_position;void print(struct node *temp)

41、 /輸出八數(shù)碼表for(j = 0;j stylej = 0)cout t;elsecoutstylejt;if(j + 1) % 3 = 0)coutendl;void write2txt() /將過程(guchng)記錄到文本文檔中ofstream out(F:out_details4.txt,ios:app);if( out.fail() )cerr未找到文件(wnjin)endl;out 第+num步n;outnext-style0 != 9)for(i = 0;i 9;i +)outnext-styleit;if(i+1) % 3 = 0)outn;out估值:;outnext-ju

42、dgement_basednext;openHead = openHead-next;out*n;outnext-style0 != 9)for(i = 0;i 9;i +)outnext-styleit;if(i + 1) % 3 =0)outn;out深度(shnd):next-depth 估值:next-judgement_basednext;closedHead = closedHead-next;outfather)/當(dāng)新生成節(jié)點(diǎn)的空格位置與closed父親(f qn)節(jié)點(diǎn)的空格位置不相等tempNode-father = closed; /新生成節(jié)點(diǎn)(ji din)的指向它的父親節(jié)

43、點(diǎn)tempNode-depth = closed-depth + 1;/深度加1tempNode-judgement_based = evaluation_function(tempNode) + tempNode-depth;/計(jì)算估值while(openHead-next-style0 != 9)if(tempNode-judgement_based openHead-next-judgement_based)openHead = openHead-next;elsetempNode-next = openHead-next;openHead-next = tempNode;openHea

44、d = open-next;return;/插入的值比最后一個(gè)還大 進(jìn)行下面的操作openHead = open-next;/先恢復(fù)表頭 否則Head指向?yàn)樽詈笠粋€(gè)open-next = tempNode;tempNode-next = openHead;open = open-next;return;void main()/updateData();/輸入八數(shù)碼數(shù)據(jù)openHead = new node(); /初始化open表open = new node();openHead-style0 = 9;openHead-next = open;for(i = 0;i stylei = who

45、leStylei;open-next = openHead;open-father = openHead;open-depth = 0;open-judgement_based = evaluation_function(open) + open-depth;/這行可以(ky)不寫closedHead = new node(); /初始化closed表closed = new node();closedHead-style0 = 9;closedHead-next = closedHead;closed = closedHead;int min_judgement1 = open-judgem

46、ent_based;while(open-style0 != 9)/當(dāng)open表不為空時(shí)一直(yzh)循環(huán)/updateData();/輸入八數(shù)碼(shm)數(shù)據(jù)write2txt();closed-next = openHead-next; /把open表中第一組八數(shù)碼移到closed表中openHead-next = openHead-next-next;closed = closed-next;closed-next = closedHead;if(openHead-next-style0 = 9)/如果是第一次 則open暫時(shí)指向openHeadopen = openHead;if(cl

47、osed-judgement_based = closed-depth)/判斷新移進(jìn)closed表中的八數(shù)碼的f值是否等于它的層數(shù)coutsucceed!style0 != 9) /將結(jié)果存于answer表中answer = closed;closed = closed-father;closed-next = answer;num = 0;while(answer-style0 != 9) /控制臺(tái)顯示步驟cout*第num+步*n;print(answer);coutnext;return;else /如果closed表末的八數(shù)碼是最終(zu zhn)狀態(tài) 則拓展新的八數(shù)碼local =

48、judge1(closed);if(local % 3 0)/右移(yu y)tempNode = new node();for(i = 0;i stylei = closed-stylei;tempSpace = tempNode-stylelocal - 1;tempNode-stylelocal - 1 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;basic_exchange_oper();if(local 6)/上移tempNode = new node();for(i = 0;i stylei = closed-stylei;tempSpace = tempNode-stylelocal + 3;tempNode-stylelocal + 3 = tempNode-stylelocal;tempNode-stylelocal = tempSpace;basic_exchange_oper();if(local 2)/下移tempNode = new node();for(i = 0;i style

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論