📄 马的遍历.cpp
字号:
#include"stdio.h"
#include"stdlib.h"
//*******************************************************//
typedef struct node{
int row,col,post;
}node;
//**********************************************************//
typedef struct sp{
node data[1000];
int top;
}seq;
int m[12][12],pos,k,i,j,x0,y0;
int x1[8]={ 1,2,2,1,-1,-2,-2,-1 },y1[8]={ 2,1,-1,-2,-2,-1,1,2 };
node dx;
seq sq;
//************************************************************//
void push(int x,int y,int z)
{ if(sq.top<999){ sq.top++;
sq.data[sq.top].row=x;
sq.data[sq.top].col=y;
sq.data[sq.top].post=z; }
else { printf("no enough space."); return ; }
}
//*************************************************************//
node pop()
{ node dd;
if(sq.top>-1){ dd=sq.data[sq.top];sq.top--;return dd; }
else{ printf("stack is empty.");exit(0); }
}
//***************************************************************//
main()
{ for(i=0;i<12;i++)
for(j=0;j<12;j++)
m[i][j]=1;
for(i=2;i<10;i++)
for(j=2;j<10;j++)
m[i][j]=0;
for(i=0;i<12;i++)
for(j=0;j<12;j++)
{
printf("%5d",m[i][j]);
if(j==11) printf("\n");
}
scanf("%d,%d",&x0,&y0);
k=1;
sq.top=-1;
m[x0][y0]=k;
pos=0;
while(k<64)
{ while((pos<=7)&&(m[x0+x1[pos]][y0+y1[pos]]!=0))
pos++;
if(pos>7){ m[x0][y0]=0;
dx=pop();
x0=dx.row;
y0=dx.col;
pos=dx.post;
//printf("%d %d ",m[x0][y0],pos);
pos++;
k--;
}
else{ m[x0+x1[pos]][y0+y1[pos]]=++k;
push(x0,y0,pos);
x0=x0+x1[pos];
y0=y0+y1[pos];
//printf("%d %d ",m[x0][y0],pos);
pos=0;
}
}
printf("\n");
for(i=2;i<10;i++)
for(j=2;j<10;j++)
{
printf("%5d",m[i][j]);
if(j==9) printf("\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -