(一)問題描述
在乙個3*3
的方棋盤上放置著
1,2,3,4,5,6,7,8
八個數碼
,每個數碼佔一格
,且有乙個空格。這些數碼可以在棋盤上移動,其移動規則是:與空格相鄰的數碼方格可以移入空格。現在的問題是:對於指定的初始棋局和目標棋局,給出數碼的移動序列。該問題稱八數碼難題或者重排九宮問題。
原始碼:#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
#include
#include
using namespace std;
const int n=3;//3*3棋盤
const int max_step=30;//最大搜尋深度
enum direction;//方向
struct chess//棋盤
;//列印棋盤
void printchess(struct chess *thechess)
printf("/n");
}printf("/t/t/t/t差距:%d/n",thechess->value);
}//移動棋盤
struct chess * movechess(struct chess * thechess,direction direct,bool createnewchess)
}if(hasgetblankcell)
break;
}//移動數字
int t_i=i,t_j=j;
bool ablemove=true;
switch(direct)
;if(!ablemove)//不可以移動則返回原節點
if(createnewchess)
}else
newchess=thechess;
newchess->cell[i][j]=newchess->cell[t_i][t_j];
newchess->cell[t_i][t_j]=0;
return newchess;
}//初始化乙個初始棋盤
struct chess * randomchess(const struct chess * thechess)
}thechess->value=value;
return value;
}//搜尋函式
struct chess * search(struct chess* begin,struct chess * target)
queue1.push(p2);//儲存節點到待處理佇列
if(p2->value==0)//為0則,搜尋完成
}else }}
step++;
if(step>max_step)
return null;
}while(p==null || queue1.size()<=0);
return p;
}main()
printf("搜尋結果:/n");
while(!stack1.empty())
printf("/n完成!");
}else
printf("搜尋不到結果.深度為%d/n",max_step);
scanf("%d",t);
}
八數碼問題 啟發式搜尋
我們在搜尋的時候往往會發現一些資料會導致我們的普通的深搜與廣搜都無法通過,那這個時候我們就需要讓起點與終點建立一些聯絡,今天我們講述的啟發式搜尋就是在廣度優先搜尋的基礎上加了這樣乙個優化,叫做估價函式。對於乙個狀態,在我們知道終點狀態的時候,我們可以設計乙個估價函式使我們對這個狀態距離終點的距離有個...
A 啟發式搜尋 八數碼問題
a 啟發式搜尋 include iostream include stdlib.h include conio.h include include define size 3 using namespace std 定義二維陣列來儲存資料表示某乙個特定狀態 typedef int status si...
八數碼問題 python實現 A 啟發式搜尋
class board object def init self self.groud 1,0,2,3,4,5,6,7,8 棋盤,0代表空 self.groud 7 2,4 5,0 6,8 3,1 移動的路徑 self.route def lt self,other return self.prio...