前端之家收集整理的这篇文章主要介绍了
【数据结构】马踏棋盘的遍历问题,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACK_ADD_SIZE 10 //存储空间分配增量
typedef struct adress
{
int x;
int y;
int di; //表示8个方向
}AD;
typedef struct
{
AD *base; //构造前和销毁后,base=NULL
AD *top; //栈顶指针
int stacksize; //当前已分配存储空间,元素为单位
}SqStack;
void InitStack(SqStack *s) //初始化栈
{
s->base = (AD*)malloc(STACK_INIT_SIZE*sizeof(AD));
if(!(s->base)) exit(1); //分配失败程序结束
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
}
AD GetTop(SqStack s) //取得栈顶元素
{
if(s.top == s.base) exit(1);//栈内无元素
return *(s.top-1);
}
void Push(SqStack *s,AD e) //入栈
{
if(s->top - s->base >= s->stacksize) //栈满追加存储空间
{
s->base = (AD*)realloc(s->base,(s->stacksize+STACK_ADD_SIZE)*sizeof(AD));
if(!s->base) exit(1);//分配失败
s->top = s->base + s->stacksize; //指向刚刚为满时
s->stacksize += STACK_ADD_SIZE;
}
*(s->top++) = e;
}
AD Pop(SqStack &s) //出栈
{
if(s.top == s.base) exit(1); //为空时退出
--s.top;
return *(s.top);
}
void Display(SqStack *s) //显示栈元素
{
AD *q;
q = s->top-1;
while(q>=s->base)
{
printf("(%d,%d)-",q->x,q->y); q--;
}
printf("\n");
}
/*void DestroyStack(SqStack *s) //销毁栈
{
free(s->base);
printf("Destroy ok!\n");
}*/
/*************************************************
**************************************************
*************************************************/
void initBoard(int Board[][8])
{
int i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
Board[i][j]=0;
// for(i=3;i<8;i++)
// for(j=0;j<8;j++)
// Board[i][j]=1;
}
//////////////////////////////////////////////////
void show(int Board[][8])
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
printf("%3d",Board[i][j]);
putchar('\n');
}
}
////////////////////////////////////////////////////
int stackempty(SqStack stack)
{
if(stack.top==stack.base)
return 1;
else
return 0;
}
/////////////////////////////////////////////////////
int pass(int board[][8],AD adress)
{
if(adress.x<8&&adress.x>=0&&adress.y<8&&adress.y>=0
&&board[adress.x][adress.y]==0)
return 1;
else
return 0;
}
//////////////////////////////////////////////////////
void Traverse(int board[][8],AD &adress)
{
int HTry1[8]={-2,-1,1,2,-2};
int HTry2[8]={1,-2,-1};
SqStack stack;
int count=0;
InitStack(&stack);
adress.di=0;//di=0~8 8表示没路
do
{
if(pass(board,adress)) //adress点可走
{
board[adress.x][adress.y]=++count;
Push(&stack,adress);
printf("加入栈顶的坐标为(%d,%d)\n%d",adress.x,adress.y,count);
if(count==64) exit(0);//遍历完成
else
{
adress.x+=HTry1[adress.di];
adress.y+=HTry2[adress.di];
adress.di=0;
}//else
}//if
else
{
if(!stackempty(stack))
{
adress=Pop(stack);
adress.di++;
while(adress.di==8&&!stackempty(stack))
{
board[adress.x][adress.y]=0;
adress=Pop(stack);
adress.di++;
count--;
}//while
if(adress.di<8)
{
Push(&stack,adress);
adress.x+=HTry1[adress.di];
adress.y+=HTry2[adress.di];
adress.di=0;
}//if
}//if
}//else
}while(count<64);//(!stackempty(stack));
}
////////////////////////////////////////////////////////////
void main()
{
AD initadress;
int Board[8][8];
int HTry1[8]={-2,-2};
int HTry2[8]={1,-1};
initBoard(Board);
printf("please inputs the initial position :\n");
do
{
scanf("%d%d",&initadress.x,&initadress.y);
if(initadress.x>=8||initadress.x<0||
initadress.y>=8||initadress.y<0)
printf("输入的范围不正确,请重新输入:\n");
}while(initadress.x>=8||initadress.x<0||
initadress.y>=8||initadress.y<0);
Traverse(Board,initadress);
printf("\n");
printf("\n遍历后的棋盘:\n");
show(Board);
system ("pause");
}
原文链接:https://www.f2er.com/datastructure/383136.html