- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
nei.cpp
资源名称:knight.rar [点击查看]
上传用户:cctqzzy
上传日期:2020-11-17
资源大小:1k
文件大小:2k
源码类别:
压缩解压
开发平台:
Visual C++
- #include<iostream>
- using namespace std;
- int row[8]={-1,-2,-2,-1,1,2,2,1,}; /*8个方向上的x增量*/
- int col[8]={2,1,-1,-2,-2,-1,1,2}; /*8个方向上的y增量*/
- int h[8][8]; /*记录走的路径*/
- int num; /*记录方案个数*/
- void coutSolution();
- void try1(int x,int y,int i);
- int main()
- {
- int row,col;
- num=0;
- for(row=0;row<=7;row++)
- for(col=0;col<=7;col++)
- h[row][col]=0;
- int m,n;
- cout<<"输入骑士最开始的位置行:列:";
- cin>>m;
- cin>>n;
- h[m-1][n-1]=1;
- //h[0][0]=1;
- try1(m-1,n-1,2);
- /*输出总方案数*/
- cout<<"总方案数为%d"<<num;
- //system("pause");
- return 0;
- }
- /*从(x,y)出发,为第i步找合适位置*/
- void try1(int x,int y,int i)
- {
- int dir,u,v;
- if (i>64) /*已经走完64步,则统计方案个数,打印方案,跳出递归*/
- {
- num++;
- if(num==1)
- coutSolution();
- }
- else
- for(dir=0;dir<=7;dir++) /*依次试遍8个方向*/
- {
- u=x+row[dir]; /*得到的新坐标*/
- v=y+col[dir];
- if ((u>=0) && (u<=7) && (v>=0) && (v<=7) && (h[u][v]==0))/*如果新坐标在棋盘上,并且这一格可以走*/
- {
- h[u][v]=i;/*占据位置[u,v]*/
- try1(u,v,i+1);
- h[u][v]=0;
- }
- }
- }
- /*打印方案*/
- void coutSolution()
- {
- int row,col;
- for(row=0;row<=7;row++)
- {
- for(col=0;col<=7;col++)
- cout<<h[row][col]<<' ';
- cout<<"n";
- }
- cout<<"nn";
- }