王朝百科
分享
 
 
 

马踏棋盘

王朝百科·作者佚名  2010-06-18  
宽屏版  字体: |||超大  

1.内容:设计一个国际象棋的马踏遍棋盘的演示程序

2.需求分析

将马随机放在国际象棋的8×8棋盘Board[0~7][0~7]的某个方格中,马按走棋规则进行移动。要求每个方格只进入一次,走遍棋盘上全部64个方格。编制非递归程序,求出马的行走路线,并按求出的行走路线,将数字1,2,…,64依次填入一个8×8的方阵,输出之

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

#define OVERFLOW -2

#define OK 1

#include<malloc.h>

#include<stdio.h>

#include<stdlib.h>

int Board[8][8]={0};

int HTry1[8]={2,-1,1,-2,2,1,-1,-2};

int HTry2[8]={1,2,2,1,-1,-2,-2,-1};

typedef struct{

int i;

int j;

}PosType;

typedef struct{

int ord;

PosType seat;

int di;

}SElemType;

typedef struct{

SElemType *base;

SElemType *top;

int stacksize;

}SqStack;

int InitStack(SqStack *s1){

(*s1).base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if(!(*s1).base) exit(OVERFLOW);

(*s1).top=(*s1).base;

(*s1).stacksize=STACK_INIT_SIZE;

return(OK);

}

SElemType Pop(SqStack *s,SElemType e){

e=*--(*s).top;

return e;

}

int Push(SqStack *s1,SElemType e){

if((*s1).top-(*s1).base>=(*s1).stacksize){

(*s1).base=(SElemType *)realloc((*s1).base,

((*s1).stacksize+STACKINCREMENT)*sizeof

(SElemType));

if(!(*s1).base) exit(OVERFLOW);

(*s1).top=(*s1).base+(*s1).stacksize;

(*s1).stacksize+=STACKINCREMENT;

}

*(*s1).top++=e;

return OK;

}

int StackEmpty(SqStack *s){

if((*s).base==(*s).top)

return(1);

else

return(0);

}

int curstep=0;

int Pass(PosType s){

if((Board[s.i][s.j]==0)&&(s.i<=7)&&(s.i>=0)&&(s.j<=7)&&(s.j>=0))

return(1);

else

return(0);

}

PosType NextPos(PosType s,int i){

s.i=s.i+HTry1[i-1];

s.j=s.j+HTry2[i-1];

return(s);

}

void horsesteps(int Board[8][8],PosType start){

int k,j;

SqStack S;

SElemType e;

PosType curpos=start;

InitStack(&S);

do{

if(Pass(curpos)){

curstep++;

Board[curpos.i][curpos.j]=curstep;

e.seat=curpos;

e.ord=curstep;

e.di=1;

Push(&S,e);

if(curstep==64)

break;

else

curpos=NextPos(curpos,1);

}//if

else{

if(!StackEmpty(&S)){

Pop(&S,e);

if(e.di==8) Board[e.seat.i][e.seat.j]=0;

while(e.di==8&&!StackEmpty(&S)){

e=Pop(&S,e);

if(e.di==8) Board[e.seat.i][e.seat.j]=0;

curstep=e.ord;

}//while

if(e.di<8){

e.di++;

Push(&S,e);

curpos=NextPos(e.seat,e.di);

}//if

}//if

}//else

}while(!StackEmpty(&S));

if(StackEmpty(&S)){

printf("马儿从这个初始位置不能踏遍棋盘

");

printf("请按任意键推出本程序

");

getchar();

exit(OVERFLOW);

}

for(j=0;j<8;j++){

printf("

");

for(k=0;k<8;k++)

printf("%3d",Board[j][k]);

}// for

}//函数结束

void main(){

int k,j;

PosType t;

char a,b;

printf("请输入马儿的初始位置

");

fflush(stdin);

scanf("%c%d,%d%c",&a,&k,&j,&b);

t.i=k;

t.j=j;

printf("马儿走的路线为

");

horsesteps(Board,t);

}

小弟只能写出自己的回溯法的算法,效率很低,有时要21分钟出结果 ,但思路是对的。

【原创】我给出我写的试探法代码

#include <iostream>

using namespace std;

typedef struct _point

{

int x;

int y;

}point;

class Horse

{

public:

Horse(int n);

~Horse();

void MoveNext(int hang,int lie);

void Print();

public:

int shu;

int qipan[20+1][20+1];

point pt[400+1];

int ci;

};

Horse::Horse(int n)

{

ci=0;

shu=n;

for(int i=1;i<=shu;i++)

for(int j=1;j<=shu;j++)

qipan[i][j]=0;

}

Horse::~Horse()

{

}

void Horse::Print()

{

if(pt[0].x==shu*shu)

{

ci++;

cout<<ci<<" Yes!"<<endl;

for(int i=1;i<=shu;i++)

for(int j=1;j<=shu;j++)

{

cout<<qipan[i][j]<<'';

if(j==shu)

cout<<endl;

}

}

}

void Horse::MoveNext(int hang,int lie)

{

if(hang==1&&lie==1&&qipan[hang][lie]==0)

{

pt[0].x=1;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

}

if(hang-1>0&&lie-2>0&&qipan[hang-1][lie-2]==0)

{

pt[0].x++;

hang=hang-1;

lie=lie-2;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie); //返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

if(hang+1<=shu&&lie-2>0&&qipan[hang+1][lie-2]==0)

{

pt[0].x++;

hang=hang+1;

lie=lie-2;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie);//返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

if(hang-2>0&&lie-1>0&&qipan[hang-2][lie-1]==0)

{

pt[0].x++;

hang=hang-2;

lie=lie-1;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie);//返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

if(hang+2<=shu&&lie-1>0&&qipan[hang+2][lie-1]==0)

{

pt[0].x++;

hang=hang+2;

lie=lie-1;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie);//返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

if(hang-2>0&&lie+1<=shu&&qipan[hang-2][lie+1]==0)

{

pt[0].x++;

hang=hang-2;

lie=lie+1;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie);//返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

if(hang+2<=shu&&lie+1<=shu&&qipan[hang+2][lie+1]==0)

{

pt[0].x++;

hang=hang+2;

lie=lie+1;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie);//返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

if(hang-1>0&&lie+2<=shu&&qipan[hang-1][lie+2]==0)

{

pt[0].x++;

hang=hang-1;

lie=lie+2;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie);//返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

if(hang+1<=shu&&lie+2<=shu&&qipan[hang+1][lie+2]==0)

{

pt[0].x++;

hang=hang+1;

lie=lie+2;

pt[pt[0].x].x=hang;

pt[pt[0].x].y=lie;

qipan[hang][lie]=pt[0].x;

MoveNext(hang,lie);//返回点

}

hang=pt[pt[0].x].x;

lie=pt[pt[0].x].y;

Print();

{

qipan[hang][lie]=0;

pt[0].x--;

}

}

int main()

{

printf("Input the num:");

int n;

scanf("%d",&n);

Horse h(n);

h.MoveNext(1,1);

return 0;

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
感谢员工的付出和激励的话怎么说?
 干货   2023-06-18
 
>>返回首页<<
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有