怎样解决中国象棋中的马不重复的走遍棋盘上的每个格子,我的代码错在了哪里?(100分)

  • 主题发起人 GuangQingYang
  • 开始时间
G

GuangQingYang

Unregistered / Unconfirmed
GUEST, unregistred user!
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>

#define DIRECTIONERROR -1
#define ROWERROR -2
#define COLERROR -3

char chessboard[9][10];


char getnext(char pos,char direction)
{
//direction的各值代表的意思如下:
//□⑦□⑤□
//⑥□□□④
//□□马□□
//②□□□○
//□③□①□

//马:表示马当前的位置
char x,y,signx=1,signy=1,dx=1;
x= pos/10;
y= pos%10;

if ((direction<0) || (direction>7)) return DIRECTIONERROR;
if (4==(direction&4)) signx=-1;
if (2==(direction&2)) signy=-1;
if (1==(direction&1)) dx=2;
(1==signx)?x+=dx:x-=dx;
(1==signy)?y=y+3-dx:y=y+dx-3;
if ((x<0) || (x>89)) return ROWERROR;
if ((y<0) || (y>89)) return COLERROR;
return x*10+y;

}
void output(char aa[9][10],char *filename)
{
FILE *f;
char vi,vj,v;
f=fopen(filename,"ab+");
fseek(f,0,SEEK_END);
char str[4];
memset(str,0,3);
str[0]=0x20;
for (vi=0;vi<9;vi++)
{
for (vj=0;vj<10;vj++)
{
v=aa[vi][vj];
itoa(v,str+1,10);
if ((v>=0) && (v<10))
{
str[2]=str[1];str[1]=0x20;
}
fwrite(str,1,strlen(str),f);
}
fputc(0x0d,f);
fputc(0x0a,f);
}
fputc(0x0d,f);
fputc(0x0a,f);
fputc(0x0d,f);
fputc(0x0a,f);
fclose(f);
}


void tm(char bb[9][10],char pos,char direction,char deep)
{
char nextpos;
char aa[9][10];
memcpy(aa,bb,90);
if ((pos>0) && (pos<90) && (0==aa[pos/10][pos%10]))
{
aa[pos/10][pos%10]=deep;
nextpos=getnext(pos,direction);

if ((nextpos>0) && (nextpos<90) && (0==aa[nextpos/10][nextpos%10]))
{
for (char i=0;i<8;i++)
tm(aa,nextpos,i,deep+1);
}
else

{
if (deep=90)
{
output(aa,"yy.txt");
}
}
}
}
void tmroot(char aa[9][10],char pos)
{
for (char i=0;i<8;i++)
tm(aa,pos,i,1);
}

void main(char argn,char * argv[])
{

memset(chessboard,0,90);
char mypos;
for (mypos=0;mypos<90;mypos++)
{
tmroot(chessboard,mypos);
}

}
 
走过的做个记号就行了:)
 
同意上楼,要一步一个脚印,否则很难确保你走的路不会重复,除非你已经有一个证明有效的方法。
 
我已经更正了所有错误,现在的程序应该很好了,不过昨晚执行了3个小时,最终结果还没有出来,不知谁还能替我改进改进,这个程序当前的源码如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#define DIRECTIONERROR -1
#define ROWERROR -2
#define COLERROR -3
#define BOARDCOLS 10
#define BOARDROWS 9
#define POSNUM 90
char chessboard[BOARDROWS][BOARDCOLS];
char getnext(char pos,short direction)
{
//direction的各值代表的意思如下:
//□⑦□⑤□
//⑥□□□④
//□□马□□
//②□□□○
//□③□①□
//马:表示马当前的位置
char x,y,signx=1,signy=1,dx=1;
x= pos/BOARDCOLS;
y= pos%BOARDCOLS;

if ((direction<0) || (direction>7)) return DIRECTIONERROR;
if (4==(direction&4)) signx=-1;
if (2==(direction&2)) signy=-1;
if (1==(direction&1)) dx=2;
(1==signx)?x+=dx:x-=dx;
(1==signy)?y=y+3-dx:y=y+dx-3;
if ((x<0) || (x>=BOARDROWS)) return ROWERROR;
if ((y<0) || (y>=BOARDCOLS)) return COLERROR;
return x*BOARDCOLS+y;

}
void output(char aa[BOARDROWS][BOARDCOLS],char *filename)
{
FILE *f;
short vi,vj,v;
f=fopen(filename,"a+");
fseek(f,0,SEEK_END);
char str[4];
memset(str,0,4);
str[0]=0x20;
for (vi=0;vi<BOARDROWS;vi++)
{
for (vj=0;vj<BOARDCOLS;vj++)
{
v=aa[vi][vj];
itoa(v,str+1,10);
if ((v>=0) && (v<10))
{
str[2]=str[1];str[1]=0x20;
}
fwrite(str,1,strlen(str),f);
}
fputc(0x0a,f);
}
fputc(0x0a,f);
fclose(f);
}
void tm(char bb[BOARDROWS][BOARDCOLS],char pos,short direction,char deep)
{
char nextpos;
char aa[BOARDROWS][BOARDCOLS];
memcpy(aa,bb,POSNUM);
printf("pos=%d,direction=%d,deep=%d/n",pos,direction,deep);
if ((pos>0) && (pos<POSNUM) && (0==aa[pos/BOARDCOLS][pos%BOARDCOLS]))
{
aa[pos/BOARDCOLS][pos%BOARDCOLS]=deep;
if (POSNUM==deep)
{
output(aa,"yy.txt");
}
else
{
nextpos=getnext(pos,direction);
if ((nextpos>0) && (nextpos<POSNUM) && (0==aa[nextpos/BOARDCOLS][nextpos%BOARDCOLS]))
{
for (short i=0;i<8;i++)
tm(aa,nextpos,i,deep+1);
}
}
}
}
void tmroot(char aa[BOARDROWS][BOARDCOLS],char pos)
{
for (short i=0;i<8;i++)
tm(aa,pos,i,1);
}
void main(char argn,char * argv[])
{
memset(chessboard,0,POSNUM);
char mypos;
for (mypos=0;mypos<POSNUM;mypos++)
tmroot(chessboard,mypos);
}
 
可能有些人没明白我的意思,我是用一个9行10列的数组来表示棋盘,数组中第i行j列的元代表棋盘的i行j列,用1至90这90个数字(1表示马起步的位置,2表示马走过的第2个位置,3表示马走过的第3个位置,以此类推)填入数组.
以上函数GetNext是已知马当前的位置pos,根据提供的方向参数direction,获取马的下一个位置.
Output是将结果存放在一个文本文件中,我的程序存放在了yy.txt文件中.
tm是我的主程序,使用了递归,只要当前不是第90个位置,就继续调用tm,否则打印结果.
 
顶部