马的遍历问题
来源:互联网 发布:php curl 教程 编辑:程序博客网 时间:2024/04/27 13:39
#include <stdio.h>
#include <conio.h>
int deltai[]={2,1,-1,-2,-2,-1,1,2};
int deltaj[]={1,2,2,1,-1,-2,-2,-1};
int board[8][8];
/*求(i,j)的出口数,和各出口号于a[],s是顺序选择着法的开始序号*/
int exitn(int i,int j,int s,int a[])
{
int i1,j1,k,count=0;
for(k=0;k<8;k++)
{
i1=i+deltai[(s+k)%8];
j1=j+deltaj[(s+k)%8];
if(i1>=0 && i1<8 && j1>=0 && j1<8 && board[i1][j1]==0)
a[count++]=(s+k)%8;
}
return count;
}
/*选择下一出口*/
int next(int i,int j,int s)
{
int m,k,kk,min,a[8],b[8],temp;
m=exitn(i,j,s,a);
if(m==0) return -1;/*没有出口*/
min=9;
for(k=0;k<m;k++)
{
temp=exitn(i+deltai[a[k]],j+deltaj[a[k]],s,b);
if(temp<min) /*找出有最少出口数的出口*/
{
min=temp;
kk=a[k];
}
}
return kk;/*返回选中的着法*/
}
void main()
{
int sx,sy,i,j,step,no,start;/*start用于控制8种可能着法的选择顺序*/
for(sx=0;sx<8;sx++)
for(sy=0;sy<8;sy++)
{
start=0;/*从0号着法开始顺序执行*/
do
{
/*清棋盘*/
for(i=0;i<8;i++)
for(j=0;j<8;j++)
board[i][j]=0;
board[sx][sy]=1;//出发位置
i=sx;j=sy;
for(step=2;step<=64;step++)
{
if((no=next(i,j,start))==-1) break;
i+=deltai[no];
j+=deltaj[no];
board[i][j]=step;
}
if(step>64)
break;
start++;//前一号着法没找到解
}while(step<=64);
/*输出解*/
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%4d",board[i][j]);
printf("/n/n");
}
getch();
printf("/n/n");
}
}
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 马的遍历问题
- 中国象棋马的遍历问题
- 马的遍历问题-问题分析
- 【数据结构】马踏棋盘的遍历问题
- 【经典算法问题】马的遍历【回溯】
- cursor遍历的问题
- 树的遍历问题
- 【5003】马遍历问题
- 关于hashmap的遍历问题
- 关于hashmap的遍历问题
- jsp jstl 遍历的问题
- ArrayList的遍历删除问题
- struts2 iterator遍历的问题
- 关于树的数据结构初始化问题?
- delphi 中的Mapx 开发
- 通用图像识别的神经网络代码描述
- 图书资料
- 如何实现网间互访(没有网络服务器权限) 大家提提建议 共同探讨一下
- 马的遍历问题
- [转载]童眼看世界
- HTML标记总汇
- 2005-04-21
- 测试下效果
- 本人呕心沥血,收集的一些专业网址
- 求助
- Java的Package与Import机制之我的理解(初学者的心得)
- 杀毒msdirectx.sys和msdrv.exe