中国象棋中马的走法(回朔法)——算法分析与设计
实验五:
中国象棋中马的走法。(回溯法) 1.问题描述:
在4×5的棋盘上已知马的起始坐标(x,y),求马能够返回到起始位置的不重复的所有不同走法的总数。
问题分析:
(1)读入马的起始位置,进行合法性判断;
(2)从起始位置开始搜索,搜索
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
采用深搜,累计总数;
(3)输出结果。
#include
#include
int array[2][8] = {{-1, -1, -2, -2, 2, 2, 1, 1},{-2, 2, 1, -1, 1, -1, 2, -2}};//表示马跳的8个方向
int chess[4][5];//表示棋盘,1:表示已经在较早的时候走过,0:则没有
int total = 0;//统计走法的个数
int sx, sy;//(sx,sy)表示马的起始坐标
void find_way(int p1, int p2);//回溯的过程
void main()
{ int i, j;
for(i = 0; i < 4; i++)
for(j = 0; j < 5; j++)
chess[i][j] = 0;
printf("输入马的起始坐标:\n");
scanf("%d", &sx);
scanf("%d", &sy);
printf("sx = %d, sy = %d\n", sx, sy);
if ((sx < 0)||(sx >= 4)||(sy < 0)||(sy >= 5))
printf("ERROR\n");
else{
chess[sx][sy] = 1;
find_way(sx, sy);//从起始位置开始试探
printf("total = %d\n", total);
getchar();
}
}
void find_way(int p1, int p2) { int i, pi, pj;
for(i = 0; i < 8; i++){//向8个方向试探
pi = p1 + array[0][i];
pj = p2 + array[1][i];
if((sx == pi)&&(sy == pj))
total++;//找到一种走法,(sx,sy)表示起点
else if( (pi >= 0)&&(pi < 4)&&(pj >= 0)&&(pj < 5)&&(chess[pi][pj]
== 0)){//继续试探
chess[pi][pj] = 1;
find_way(pi, pj);
chess[pi][pj] = 0;
}
}//for
}
测试
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
1.输入马的起始(3.4)
2.运行结果