資源簡介
采用回溯法,當有解時輸出解,程序結束否則no solution

代碼片段和文件信息
#?include?
#?include?
#?include?
using?namespace?std;
typedef?struct
{
????long?x;
????long?y;
}Step;
static?long?tx[]={-2-1?1?2?2?1-1-2?}; //8個方向
static?long?ty[]={?1?2?2?1-1-2-2-1?};
?long?CurStep?=?1; //當前步
int?size;
int?TotalStep;
int?**?Board;
Step?*step;
??
void?Init(?) //初始化變量
{
cout<<“Please?input?size?of?your?chessboard?:“;
cin>>size;
//size=8;
TotalStep?=?size*size;
step?=?new?Step[TotalStep+1];
size++;
//cout<
Board?=?new?int*[size];
for(int?i?=?0;i? Board[i]?=?new?int[size];
for(int?i?=?0;i? for(int?j?=?0;j? Board[i][j]=0;
int?sxsy;
cout<<“input?start?position(x):“;
cin>>sx;
cout<<“input?start?position(y):“;
cin>>sy;
step[CurStep].x?=?sx;
step[CurStep].y?=?sy; //騎士初始位置
????Board[step[CurStep].x][step[CurStep].y]?=?CurStep; //騎士初始位置
}
void?Output(?) //輸出結果?
{
????long?dx?dy;
????dx?=?step[CurStep].x?-?step[1].x; //判斷最后是否回到起點
????dy?=?step[CurStep].y?-?step[1].y;
????if(dx?*?dx?+?dy?*?dy?==?5)??
????{??
???????cout<<“Hamilton?!!“< ????}
ofstream?fout(“output.txt“);
if(!fout)
{
cerr<<“File?cannot?open!“< }
if(CurStep?==?TotalStep)
{
for(int?i?=?1;i<=?TotalStep;++i)
{
fout<<““<“;
if(i?!=?TotalStep)
{
fout<<“?→?“;
}
if(i?%?5?==?0)
{
fout< }
}//end?file-for
for(int?i?=?1;?i? {??
for(int?j?=?1;?j? {??
???cout< }??
printf(“\n“);
}//end?Screen?print-for
}
else
{
cout<<“No?Solution“< fout<<“No?Solution“;
}
fout.clear();
fout.close();
for(int?i=0;i? delete?[]?Board[i];
delete?[]?Board;
delete?[]?step;
}??
void?knightTour(?)??
{??
????long?nextx?nexty;
????int?i;
????for(i?=?0;?i?8;?++i)?//8個方向上嘗試,??
????{??
????????nextx?=?step[CurStep].x?+?tx[i];
????????nexty?=?step[CurStep].y?+?ty[i];
????????if(?nextx?>?0?&&?nextx??0?&&?nexty?????????{
????????????++CurStep;
????????????Board[nextx][nexty]?=?CurStep; //騎士移動到下一步
????????????
step[CurStep].x?=?nextx;
????????????step[CurStep].y?=?nexty; //記錄走過的步
knightTour(); //做下一次嘗試
if(?CurStep?==?TotalStep) //如果步數達到最大步數(棋盤全滿),返回
{
return;
}
????????}
????}
if(i?==?8?&&?CurStep?!=?TotalStep)
{
Board[step[CurStep].x][step[CurStep].y]?=?0; //如果8個方向都不滿足,取消本次移動
--CurStep; //并且倒退1步
}
}
int?main(?)
{
????Init();??
????knightTour(); //開始遍歷
Output();
system(“pause“);
????return?0;??
}?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????41984??2011-04-21?02:19??Knight\Debug\Knight.exe
?????文件?????412656??2011-04-21?02:19??Knight\Debug\Knight.ilk
?????文件?????617472??2011-04-21?02:19??Knight\Debug\Knight.pdb
?????文件???????6702??2011-04-21?02:19??Knight\Knight\Debug\BuildLog.htm
?????文件????????663??2011-04-20?17:46??Knight\Knight\Debug\Knight.exe.em
?????文件????????728??2011-04-20?17:46??Knight\Knight\Debug\Knight.exe.em
?????文件????????621??2011-04-21?02:19??Knight\Knight\Debug\Knight.exe.intermediate.manifest
?????文件??????51590??2011-04-21?02:19??Knight\Knight\Debug\Knight.obj
?????文件?????????67??2011-04-21?02:19??Knight\Knight\Debug\mt.dep
?????文件?????175104??2011-04-21?02:19??Knight\Knight\Debug\vc90.idb
?????文件?????225280??2011-04-21?02:19??Knight\Knight\Debug\vc90.pdb
?????文件???????3014??2011-04-21?02:19??Knight\Knight\Knight.cpp
?????文件???????3934??2011-04-20?17:46??Knight\Knight\Knight.vcproj
?????文件???????1409??2011-04-21?02:20??Knight\Knight\Knight.vcproj.Lee-PC.Lee.user
?????文件????????852??2011-04-21?02:19??Knight\Knight\output.txt
?????文件????2247680??2011-04-21?02:20??Knight\Knight.ncb
?????文件????????884??2011-04-20?17:34??Knight\Knight.sln
????..A..H.??????9216??2011-04-21?02:20??Knight\Knight.suo
?????目錄??????????0??2011-04-21?02:19??Knight\Knight\Debug
?????目錄??????????0??2011-04-21?02:19??Knight\Debug
?????目錄??????????0??2011-04-21?02:19??Knight\Knight
?????目錄??????????0??2011-04-20?23:21??Knight
-----------?---------??----------?-----??----
??????????????3799856????????????????????22
- 上一篇:雪花圣誕禮物
- 下一篇:Vrml 地月模型 小球運動
評論
共有 條評論