帮忙调试一下程序(马踏棋盘问题)

Angelqwb 2005-04-17 02:33:30
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"
#define STACK_INIT_SIZE 400;
struct SqStack{
int *base;
int *top;
int stacksize;
};
int HTry1[8]={-2,-1,1,2,2,1,-1,-2};
int HTry2[8]={1,2,2,1,-1,-2,-2,-1};
int ch[64]={0,0,0}; /*用来标识对应方格是否走过*/
int arr[8][8]; /*记录走到当前方格的k值*/
int i,j,k;
int l=0;
int n=0; /*用来表示走过的方格数*/
struct SqStack stack1;


void forward(struct SqStack *S);
void back(struct SqStack *S);

void InitStack(struct SqStack *S){ /*构造一个空栈*/
S->base=(int *)malloc(400*sizeof(int));
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}

void Push(struct SqStack *S,int e){
*S->top=e;
S->top++;
n++;
}

void Pop(struct SqStack *S,int *a){
S->top--;
*a=*(S->top);
n--;
}

void print(struct SqStack *S){
int e;
while (!(S->top==S->base)){
Pop(S,&e);
printf("%d\t",e);
}
}

void back(struct SqStack *S){
if(n<64){
int e;
Pop(S,&e);
ch[e]=0;
l=arr[i-1][j-1];
i=i-HTry1[l];
j=j-HTry2[l];
if(l==7) back(S);/*此回搠点也无路可走,继续回搠*/
if(l<7){
l++;
forward(S);
}
}

}
void forward(struct SqStack *S){
int x;
int y;
if(n<64){
for (k=l;k<8;k++){
x=i+HTry1[k];
y=j+HTry2[k];

if(x>0&&x<9&&y>0&&y<9&&ch[(x-1)*8+(y-1)]==0){/*向前一个点*/
i=x;
j=y;
Push(S,(x-1)*8+(y-1));
ch[(x-1)*8+(y-1)]=1;
arr[i-1][j-1]=k; /*保留k值,回搠时使用*/
l=0;
forward(S);
}

else if(k==7) back(S);/*无路 返回*/
}
}
}
void main(){
//struct SqStack stack1;
InitStack(&stack1);
printf("Please enter the row and column of the starting point.\n");
scanf("%d,%d",&i,&j);/*输入行数和列数*/
Push(&stack1,(i-1)*8+(j-1));
ch[(i-1)*8+(j-1)]=1;
forward(&stack1);
print(&stack1);
getch();
}

...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sTigerwsk 2005-04-17
  • 打赏
  • 举报
回复
编译不通过总有个错误提示吧
Angelqwb 2005-04-17
  • 打赏
  • 举报
回复
编译通不过
ysbcg 2005-04-17
  • 打赏
  • 举报
回复
先说说有什么问题哦
Angelqwb 2005-04-17
  • 打赏
  • 举报
回复
各位帮忙调试一下

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧