拼图算法分析...
From: http://blog.sina.com.cn/s/blog_6a4b57e30100mfch.html
一、題目說明:
(九宮問題)在一個3×3的九宮中有1-8這8個數(shù)及一個空格隨機的擺放在其中的格子里,如圖1-1所示。現(xiàn)在要求實現(xiàn)這個問題:將該九宮格調(diào)整為如圖1-1右圖所示的形式。調(diào)整的規(guī)則是:每次只能將與空格(上、下、或左、右)相鄰的一個數(shù)字平移到空格中。試編程實現(xiàn)這一問題的求解。
(圖1-1)
二、題目分析:
九宮問題是人工智能中的經(jīng)典難題之一,問題是在3×3方格棋盤中,放8格數(shù),剩下的沒有放到的為空,每次移動只能是和相鄰的空格交換數(shù)。程序自動產(chǎn)生問題的初始狀態(tài),通過一系列交換動作將其轉(zhuǎn)換成目標(biāo)排列(如下圖1-2到圖1-3的轉(zhuǎn)換)。
?
?
?
?
(圖1-2)????(圖1-3)
九宮問題中,程序產(chǎn)生的隨機排列轉(zhuǎn)換成目標(biāo)共有兩種可能,而且這兩種不可能同時成立,也就是奇數(shù)排列和偶數(shù)排列。我們可以把一個隨機排列的數(shù)組從左到右從上到下用一個一維數(shù)組表示,如上圖1-2我們就可以表示成{8,7,1,5,2,6,3,4,0}其中0代表空格。
在這個數(shù)組中我們首先計算它能夠重排列出來的結(jié)果,公式就是:
∑(F(X))=Y,其中F(X)
就是一個數(shù)他前面比這個數(shù)小的數(shù)的個數(shù),Y為奇數(shù)和偶數(shù)個有一種解法。那么上面的數(shù)組我們就可以解出它的結(jié)果。
F(8)=0;
F(7)=0;
F(1)=0;
F(5)=1;
F(2)=1;
F(6)=3;
F(3)=2;
F(4)=3;
Y=0+0+0+1+1+3+2+3=10
Y=10是偶數(shù),所以他的重排列就是如圖1-3的結(jié)果,如果加起來的結(jié)果是奇數(shù)重排的結(jié)果就是如圖1-1最右邊的排法。
三、算法分析
九宮問題的求解方法就是交換空格(0)位置,直至到達目標(biāo)位置為止。圖形表示就是:
(圖3-1)
要想得到最優(yōu)的就需要使用廣度優(yōu)先搜索,九宮的所以排列有9!種,也就是362880種排法,數(shù)據(jù)量是非常大的,我使用的廣度搜索,需要記住每一個結(jié)點的排列形式,要是用數(shù)組記錄的話會占用很多的內(nèi)存,我們把數(shù)據(jù)進行適當(dāng)?shù)膲嚎s。使用DWORD形式保存,壓縮形式是每個數(shù)字用3位表示,這樣就是3×9=27個字節(jié),由于8的二進制表示形式1000,不能用3位表示,我使用了一個小技巧就是將8表示位000,然后用多出來的5個字表示8所在的位置,就可以用DWORD表示了。用移位和或操作將數(shù)據(jù)逐個移入,比乘法速度要快點。定義了幾個結(jié)果來存儲遍歷到了結(jié)果和搜索完成后保存最優(yōu)路徑。
類結(jié)構(gòu)如下:
?
計算隨機隨機數(shù)組使用了vector模板用random_shuffle(,)函數(shù)來打亂數(shù)組數(shù)據(jù),并計算目標(biāo)結(jié)果是什么。代碼:
?
// NineGrid.cpp#include "NineGrid.h"void CNineGird::Reset() {if(m_bAutoRun) return;vector vs;int i;for (i = 1 ; i < 9 ; i ++)vs.push_back(i);vs.push_back(0);random_shuffle(vs.begin(), vs.end());random_shuffle(vs.begin(), vs.end());for ( i = 0 ; i < 9 ; i ++){m_iChess[i] = vs[i];}if (!EstimateUncoil(m_iChess)){unsigned char array[9] = {1,2,3,8,0,4,7,6,5};memcpy(m_iTargetChess , array , 9);}else{unsigned char array[9] = {1,2,3,4,5,6,7,8,0};memcpy(m_iTargetChess , array , 9);}m_iStepCount = 0; }// 數(shù)據(jù)壓縮函數(shù)實現(xiàn): inline void CNineGird::ArrayToDword(unsigned char *array , DWORD& data) {unsigned char night = 0;for ( int i = 0 ; i < 9 ; i ++){if (array[i] == 8){night = (unsigned char)i;break;}}array[night] = 0;data = 0;data = (DWORD)((DWORD)array[0] << 29 | (DWORD)array[1] << 26 |(DWORD)array[2] << 23 | (DWORD)array[3] << 20 |(DWORD)array[4] << 17 | (DWORD)array[5] << 14 |(DWORD)array[6] << 11 | (DWORD)array[7] << 8 |(DWORD)array[8] << 5 | night);array[night] = 8; } // 解壓縮時跟壓縮真好相反,解壓代碼: inline void CNineGird::DwordToArray(DWORD data , unsigned char *array) {unsigned char chtem;for ( int i = 0 ; i < 9 ; i ++){chtem = (unsigned char)(data >> (32 - (i + 1) * 3) & 0x00000007);array[i] = chtem;}chtem = (unsigned char)(data & 0x0000001F);array[chtem] = 8; } // 由于可擴展的數(shù)據(jù)量非常的大,加上我在保存的時候使用的是DWORD類型,將每一步數(shù)據(jù)都記錄在一個排序二叉樹中,按從小到大從左到有的排列,搜索的時候跟每次搜索將近萬次的形式比較快幾乎是N次方倍,把幾個在循環(huán)中用到的函數(shù)聲明為內(nèi)聯(lián)函數(shù),并在插入的時候同時搜索插入的數(shù)據(jù)會不會在樹中有重復(fù)來加快總體速度。二叉樹插入代碼: inline bool CNineGird::AddTree(DWORD place , PlaceList*& parent) {if (parent == NULL){parent = new PlaceList();parent->Left = parent->Right = NULL;parent->Place = place;return true;}if (parent->Place == place)return false;if (parent->Place > place){return AddTree(place , parent->Right);}return AddTree(place , parent->Left); } // 計算結(jié)果是奇數(shù)排列還是偶數(shù)排列的代碼: bool CNineGird::EstimateUncoil(unsigned char *array) {int sun = 0;for ( int i = 0 ; i < 8 ; i ++){for ( int j = 0 ; j < 9 ; j ++){if (array[j] != 0){if (array[j] == i +1 )break;if (array[j] < i + 1)sun++;}}}if (sun % 2 == 0)return true;elsereturn false; } // 移動到空格位的代碼比較簡單,只要計算是否會移動到框外面就可以了,并在移動的時候順便計算一下是不是已經(jīng)是目標(biāo)結(jié)果,這是用來給用戶手工移動是給與提示用的,代碼: inline bool CNineGird::MoveChess(unsigned char *array , int way) {int zero , chang;bool moveok = false;for ( zero = 0 ; zero < 9 ; zero ++){if (array[zero] == 0)break;}POINT pnt;pnt.x = zero % 3;pnt.y = int(zero / 3);switch(way){case 0 : //upif (pnt.y + 1 < 3){chang = (pnt.y + 1) * 3 + pnt.x ;array[zero] = array[chang];array[chang] = 0;moveok = true;}break;case 1 : //downif (pnt.y - 1 > -1){chang = (pnt.y - 1) * 3 + pnt.x ;array[zero] = array[chang];array[chang] = 0;moveok = true;}break;case 2 : //leftif (pnt.x + 1 < 3){chang = pnt.y * 3 + pnt.x + 1;array[zero] = array[chang];array[chang] = 0;moveok = true;}break;case 3 : //rightif (pnt.x - 1 > -1){chang = pnt.y * 3 + pnt.x - 1;array[zero] = array[chang];array[chang] = 0;moveok = true;}break;}if (moveok && !m_bAutoRun){m_iStepCount ++ ;DWORD temp1 ,temp2;ArrayToDword(array , temp1);ArrayToDword(m_iTargetChess , temp2);if (temp1 == temp2){MessageBox(NULL , "你真聰明這么快就搞定了!" , "^_^" , 0);}}return moveok; } // 我在進行廣度搜索時候,將父結(jié)點所在的數(shù)組索引記錄在子結(jié)點中了,所以得到目標(biāo)排列的時候,我們只要從子結(jié)點逆向搜索就可以得到最優(yōu)搜索路徑了。用變量m_iPathsize來記錄總步數(shù),具體函數(shù)代碼: void CNineGird::GetPath(UINT depth) {int now = 0 , maxpos = 100 ;UINT parentid;if (m_pPathList != NULL){delete[] m_pPathList;}m_pPathList = new PathList[maxpos];parentid = m_pScanbuf[depth].ScanID;DwordToArray(m_pScanbuf[depth].Place , m_pPathList[++now].Path);while(parentid != -1){if (now == maxpos){maxpos += 10;PathList * temlist = new PathList[maxpos];memcpy(temlist , m_pPathList , sizeof(PathList) * (maxpos - 10));delete[] m_pPathList;m_pPathList = temlist;}DwordToArray(m_pScanbuf[parentid].Place , m_pPathList[++now].Path);parentid = m_pScanbuf[parentid].ScanID;}m_iPathsize = now; }// 動態(tài)排列的演示函數(shù)最簡單了,為了讓主窗體有及時刷新的機會,啟動了一個線程在需要主窗體刷新的時候,用Slee(UINT)函數(shù)來暫停一下線程就可以了。代碼: unsigned __stdcall MoveChessThread(LPVOID pParam) {CNineGird * pGird = (CNineGird *)pParam;RECT rect;pGird->m_iStepCount = 0;::GetClientRect(pGird->m_hClientWin , &rect);for ( int i = pGird->m_iPathsize ; i > 0 ; i --){memcpy(pGird->m_iChess , pGird->m_pPathList[i].Path , 9);pGird->m_iStepCount ++;InvalidateRect( pGird->m_hClientWin , &rect , false);Sleep(300);}char msg[100];sprintf(msg , "^_^ ! 搞定了!\r\n計算步驟用時%d毫秒" , pGird->m_iTime);MessageBox(NULL , msg , "~_~" , 0);pGird->m_bAutoRun = false;return 0L; } // 最后介紹一下搜索函數(shù)的原理,首先得到源數(shù)組,將其轉(zhuǎn)換成DWORD型,與目標(biāo)比較,如果相同完成,不同就交換一下數(shù)據(jù)和空格位置,加入二叉樹,搜索下一個結(jié)果,直到?jīng)]有步可走了,在搜索剛剛搜索到的位置的子位置,這樣直到找到目標(biāo)結(jié)果為止,函數(shù): bool CNineGird::ComputeFeel() {unsigned char *array = m_iChess;UINT i;const int MAXSIZE = 362880;unsigned char temparray[9];DWORD target , fountain , parent , parentID = 0 , child = 1;ArrayToDword(m_iTargetChess , target);ArrayToDword(array , fountain);if (fountain == target){return false;}if (m_pScanbuf != NULL){delete[] m_pScanbuf;}m_pScanbuf = new Scanbuf[MAXSIZE];AddTree(fountain ,m_pPlaceList);m_pScanbuf[ 0 ].Place = fountain;m_pScanbuf[ 0 ].ScanID = -1;clock_t tim = clock();while(parentID < MAXSIZE && child < MAXSIZE){parent = m_pScanbuf[parentID].Place;for ( i = 0 ; i < 4 ; i ++) // 0 :UP , 1:Down ,2:Left,3:Right{DwordToArray(parent , temparray);if (MoveChess(temparray,i)) //是否移動成功{ArrayToDword(temparray , fountain);if (AddTree(fountain, m_pPlaceList)) //加入搜索數(shù){m_pScanbuf[ child ].Place = fountain;m_pScanbuf[ child ].ScanID = parentID;if (fountain == target) //是否找到結(jié)果{m_iTime = clock() - tim;GetPath(child);//計算路徑FreeTree(m_pPlaceList);delete[] m_pScanbuf;m_pScanbuf = NULL;return true;}child ++;}}} // for iparentID++;}m_iTime = clock() - tim;FreeTree(m_pPlaceList);delete[] m_pScanbuf;m_pScanbuf = NULL;return false; }
重要函數(shù)的介紹結(jié)束,下面是程序的運行結(jié)果和運算結(jié)果:
??轉(zhuǎn)自:http://www.qqgb.com/Program/VC/VCarithmetic/Program_55328_3.html
?
總結(jié)
- 上一篇: golang reflect
- 下一篇: 再别极域(亲测有效)