问题描述:
在中国象棋的半个棋盘中(楚河汉界的一边),马从左下角跳到右上角的所有正确的跳法有哪些?(只能往右边跳,包括右上和右下,不能后退)
这个问题是我们的技术总监告诉我们的,他说这是他上高中时参加的计算机程序设计比赛中的一道题(高中时就能做这样的题,真是羡慕),如果没学数据结构的话,写起来就不太好下手。正好我最近在学习数据结构(学校里学的都忘的差不多了),所以就用递归写了一下,没想到会有37种方法,没想到呀。呵呵!大家提提意见。
源程序如下:
#include <stdio.h>
#define CHESSBDROW 5 /*棋盘的行数*/
#define CHESSBDCOL 9 /*棋盘的列数*/
#define MAXPOS 20 /*到达目的地的最大步数*/
/*棋子的位置结构*/
typedef struct {
int row;
int col;
} POS;
POS path[MAXPOS]; /*用来存储正确的路径*/
POS startPos; /*棋子所在的起始位置*/
POS destPos; /*目的地址*/
void InitChessboard();
void FindPath(POS pos, int direction, int count);
int OutRange(POS step);
void display();
int equal(POS pos1, POS pos2);
POS NextPos(POS pos, int n);
int IsDestnation(POS pos);
int main(void)
{
InitChessboard();
FindPath(startPos, 0, 0);
exit(0);
}
/*初始化棋盘数据*/
void InitChessboard()
{
destPos.row = 0;
destPos.col = 8;
startPos.row = 4;
startPos.col = 0;
}
/*递归搜索函数*/
void FindPath(POS currentPos, int direction, int count)
{
POS next;
int i;
next = NextPos(currentPos, direction);
if (OutRange(next)) {
return;
}
else {
path[count] = next;
currentPos = next;
count++;
}
if ( IsDestination(currentPos) ) {
display(path, count);
return;
}
else {
/*依次查找4个方向*/
for (i = 1; i <= 4; i++) {
FindPath(currentPos, i, count);
}
}
}
/* 返回pos位置的第n个方向所能到达的位置*/
POS NextPos(POS pos, int n)
{
POS next;
switch(n) {
case 0:
next = pos;
break;
case 1:
next.row = pos.row - 1;
next.col = pos.col + 2;
break;
case 2:
next.row = pos.row - 2;
next.col = pos.col + 1;
break;
case 3:
next.row = pos.row + 1;
next.col = pos.col + 2;
break;
case 4:
next.row = pos.row + 2;
next.col = pos.col + 1;
break;
default:
break;
}
return next;
}
/*测试是否越界*/
int OutRange(POS pos)
{
return ((pos.row < 0) || (pos.col > (CHESSBDCOL-1)) || (pos.row > (CHESSBDROW-1)) || (pos.col < 0));
}
/*测试两个点是否相同*/
int equal(POS pos1, POS pos2)
{
return ((pos1.row == pos2.row ) && (pos1.col == pos2.col));
}
/*是否到达目的地址*/
int IsDestination(POS pos)
{
return equal(pos, destPos);
}
/*显示正确的路径*/
void display(POS path[], int n)
{
int i;
for(i = 0; i < n; i++) {
printf("%d, %d/t", path[i].row, path[i].col);
}
printf("/n");
}
在中国象棋的半个棋盘中(楚河汉界的一边),马从左下角跳到右上角的所有正确的跳法有哪些?(只能往右边跳,包括右上和右下,不能后退)
这个问题是我们的技术总监告诉我们的,他说这是他上高中时参加的计算机程序设计比赛中的一道题(高中时就能做这样的题,真是羡慕),如果没学数据结构的话,写起来就不太好下手。正好我最近在学习数据结构(学校里学的都忘的差不多了),所以就用递归写了一下,没想到会有37种方法,没想到呀。呵呵!大家提提意见。
源程序如下:
#include <stdio.h>
#define CHESSBDROW 5 /*棋盘的行数*/
#define CHESSBDCOL 9 /*棋盘的列数*/
#define MAXPOS 20 /*到达目的地的最大步数*/
/*棋子的位置结构*/
typedef struct {
int row;
int col;
} POS;
POS path[MAXPOS]; /*用来存储正确的路径*/
POS startPos; /*棋子所在的起始位置*/
POS destPos; /*目的地址*/
void InitChessboard();
void FindPath(POS pos, int direction, int count);
int OutRange(POS step);
void display();
int equal(POS pos1, POS pos2);
POS NextPos(POS pos, int n);
int IsDestnation(POS pos);
int main(void)
{
InitChessboard();
FindPath(startPos, 0, 0);
exit(0);
}
/*初始化棋盘数据*/
void InitChessboard()
{
destPos.row = 0;
destPos.col = 8;
startPos.row = 4;
startPos.col = 0;
}
/*递归搜索函数*/
void FindPath(POS currentPos, int direction, int count)
{
POS next;
int i;
next = NextPos(currentPos, direction);
if (OutRange(next)) {
return;
}
else {
path[count] = next;
currentPos = next;
count++;
}
if ( IsDestination(currentPos) ) {
display(path, count);
return;
}
else {
/*依次查找4个方向*/
for (i = 1; i <= 4; i++) {
FindPath(currentPos, i, count);
}
}
}
/* 返回pos位置的第n个方向所能到达的位置*/
POS NextPos(POS pos, int n)
{
POS next;
switch(n) {
case 0:
next = pos;
break;
case 1:
next.row = pos.row - 1;
next.col = pos.col + 2;
break;
case 2:
next.row = pos.row - 2;
next.col = pos.col + 1;
break;
case 3:
next.row = pos.row + 1;
next.col = pos.col + 2;
break;
case 4:
next.row = pos.row + 2;
next.col = pos.col + 1;
break;
default:
break;
}
return next;
}
/*测试是否越界*/
int OutRange(POS pos)
{
return ((pos.row < 0) || (pos.col > (CHESSBDCOL-1)) || (pos.row > (CHESSBDROW-1)) || (pos.col < 0));
}
/*测试两个点是否相同*/
int equal(POS pos1, POS pos2)
{
return ((pos1.row == pos2.row ) && (pos1.col == pos2.col));
}
/*是否到达目的地址*/
int IsDestination(POS pos)
{
return equal(pos, destPos);
}
/*显示正确的路径*/
void display(POS path[], int n)
{
int i;
for(i = 0; i < n; i++) {
printf("%d, %d/t", path[i].row, path[i].col);
}
printf("/n");
}