原程序如下,我想用栈来做,我现在想让方格的序号依次入栈,用printf()函数将它们显示出来,可能是递归回搠时出了问题。谢谢帮我一下。#include "stdio.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; /*用来表示走过的方格数*/
forward(struct SqStack *S);
void back(struct SqStack *S);InitStack(struct SqStack *S){ /*构造一个空栈*/
S->base=malloc(400);
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);
}
} }
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);/*无路 返回*/
}
}
}
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();
}
#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; /*用来表示走过的方格数*/
forward(struct SqStack *S);
void back(struct SqStack *S);InitStack(struct SqStack *S){ /*构造一个空栈*/
S->base=malloc(400);
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);
}
} }
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);/*无路 返回*/
}
}
}
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();
}
解决方案 »
- GDI+ 的Matrix::TransformPoints的作用?
- FindText返回0是什么意思?
- 风吹鸡弹壳,分散人安乐
- 请问,能否将AbiWord的源代码转换成标准组件
- 在一个窗口的子窗口类的函数中弹出的另一窗口(非子窗口),在按确定或取消或关闭另一窗口时为什么会出错?
- 请问GetClientRect()和AfxGetApp()是什么作用??????
- 关于文件读写的疑问!
- 该如何用MFC去学习制作小游戏d ne ?
- CMFCToolBar 中频繁调用AdjustLayout()函数存在严重内存泄露,求大神帮助!
- 请教Tab Control控件的问题
- 100分请教
- 以对话框作为主窗口--失败了,请教
数组ch还可以用来标识马是否能到达
所以完全可以不用栈来做
本来递归时系统就自动生成了栈
所以就不必再生成栈