⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 骑士周游算法--用双向链表实现.cpp

📁 数据结构 之骑士周游算法
💻 CPP
字号:

#include<stdio.h>
#include <malloc.h>
#define LEN sizeof(struct stack)
struct stack
{int row;
 int col;
 int dir;
 struct stack *next;
 struct stack *prior;
};
struct  stack*head = (struct stack*)malloc(LEN);
struct stack*q=head;

void push(int i,int j,int v)
{
 struct stack *p=(struct stack*)malloc(LEN);
 p->row=i;
 p->col=j;
 p->dir=v;
 q->next=p;
 p->prior=q;
 q=p;
}

void pop()
{struct stack *temp;
 temp=q->prior;
 free(q);
 q=temp;

}
void start()
{   int y,z,v=0;
	int i,j;
	int move[8][2]={2,1,1,2,1,-2,2,-1,-2,1,-1,2,-1,-2,-2,-1};
	int c[6][6];
	for(i=0;i<6;i++)
	  {for(j=0;j<6;j++)
	   c[i][j]=0;
	  }
	 printf("input y:");
	 scanf("%d",&y);
	 printf("input z:");
	 scanf("%d",&z);
	 int account=0;
	 while(account<35)
	 {
		 while(v<8)
		  {
			  i=y+move[v][0];
			  j=z+move[v][1];
			  if(i>=0&&i<=5&&j>=0&&j<=5&&c[i][j]==0)
				{ push(y,z,v+1);
				  account++;
				  c[y][z]=account;
				  y=i;
				  z=j;
				  v=0;
			}
			  else v++;
			}
		   if(v==8&&account>0&&account!=35)
			{
			  y=q->row;
			  z=q->col;
			  v=q->dir;
			   pop();
			
			 c[y][z]=0;
			   account--;
			 }
	   }
	   c[y][z]=36;
	   for(i=0;i<6;i++)
		 {
		   for(j=0;j<6;j++)
			 {
				 printf("%4d",c[i][j]);
			 }
			printf("\n");
	   }
}

void main()
{printf("\n");
 start();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -