(10.1.1)--ch10-01theKnight'stourproblem-E.pdf
《(10.1.1)--ch10-01theKnight'stourproblem-E.pdf》由会员分享,可在线阅读,更多相关《(10.1.1)--ch10-01theKnight'stourproblem-E.pdf(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Data Structures and AlgorithmsThe Knights Tour ProblemData Structures and AlgorithmsData Structures and Algorithms2Problem Description:Experiment:The Knights Tour ProblemThe knight is placed on a square on the chessboard.It moves according to the rules of chess.Each square can only be entered once.G
2、o through all sixty-four squares on the chessboard.Write a non-recursive program to find out the route of the knight.According to the route,fill the chessboard with Numbers 1 to sixty-four,and output it.Data Structures and AlgorithmsData Structures and Algorithms3problem analysisExperiment:The Knigh
3、ts Tour ProblemBoard:int Board88=0;0:the position has not been passed i:164 the ith step has reached hereSet the starting position;Step=1;Set the chessboard;Step64Find the next step;Updata the Step;Updata the Board;output start over0123456701234567YNstep number:int Step;/he number of steps completed
4、Data Structures and AlgorithmsData Structures and Algorithms4problem analysisExperiment:The Knights Tour ProblemThe Knights move0123456701234567Position point type:typedef struct int x;/row number(07)int y;/column number(07)Spot;Define Variable:Spot Cur,Next;/Represents the current and next position
5、 points.Data Structures and AlgorithmsData Structures and Algorithmsdirection12345678Line changeColumn change5problem analysisExperiment:The Knights Tour Problem12345678defining array:int direction29=0,-2,-1,1,2,2,1,-1,-2 ,0,1,2,2,1,-1 -2,-2,-1 ;-21-1212212-11-2-1-2-2-1If the current position is Cur
6、,Go in the i-th direction,Then the next point is:Next.x=Cur.x+direction0 i;Next.y=Cur.y+direction1 i;0123456701234567Data Structures and AlgorithmsData Structures and Algorithmsint Feasible(int x,int y)if(0=x)&(x8)&(0=y)&(y8)&(Boardxy=0))return 1;else return 0;6Experiment:The Knights Tour Problempro
7、blem analysis1、The position must be within the checkboard:(0=x)&(x8)&(0=y)&(y8))The condition under which the position is feasible:2、The position has not been visited:Boardxy=0 int NextDirection(Spot Cur)int i,x,y;for(i=1;i=8;i+)x=Cur.x+Direction0i;y=Cur.y+Direction1i;if(Feasible(x,y)return i;return
8、 0;Data Structures and AlgorithmsData Structures and Algorithms7Experiment:The Knights Tour Problemproblem analysis495241301105053313811401348514229149254323722391274743281583243336214423617462734191625435204526518Conclusion1:When we cant find the next feasible position,that is to say,the return val
9、ue of the NextDirection is 0,we need to back off.Conclusion2:Positions passed by the knight should be stored in the stack.That is,when the NextDirection returns a non-zero value,the current position is pushed,and then go to the next step.Conclusion3:Do two things when back off The Cur position on th
10、e Board set 0,Step-;Pop a position,set as the Cur.0123456701234567123456783054Step:Cur.x:Cur.y:Data Structures and AlgorithmsData Structures and Algorithms8problem analysisExperiment:The Knights Tour Problem k=NextDirection(Cur);k!=0?Find the Next position;Push(Stack,Cur);Cur=Next;Step+;BoardCur.xCu
11、r.y=Step;BoardCur.xCur.y=0,Step-;Cur=Pop(Stack);设置起始点;step=1;设置棋盘;Step64 output start overCur.x=m;Cur.y=n;Step=1;BoardCur.xCur.y=Step;1212YNYNFind the next step;Updata the Step;Updata the Board;Data Structures and AlgorithmsData Structures and Algorithms9Experiment:The Knights Tour Problemproblem an
12、alysis4952413011050533138114013485142291492323722391274743281583243336214423617462734191625435204526518012345670123456712345678typedef struct int x;/line number(07)int y;/column number(07)int d;/Number of directions explored(08)Spot;Modify the definition of the Spot type:Conclusion4:After back off,w
13、e can only continue to detect from the direction of the last detection,but not from the beginning.1153Step:Cur.x:Cur.y:Data Structures and AlgorithmsData Structures and Algorithms10problem analysisExperiment:The Knights Tour Problemk=NextDirection(Cur);k!=0?Find the Next position;Push(Stack,Cur);Cur
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 10.1 ch10 01 theKnight stourproblem
限制150内