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

📄 keti.c

📁 是一个迷宫程序
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

void delay();
void print(char tile[12][12]);

/***************create data of node*****************/
typedef struct data
{
	int i;
	int j;
	int k;
}DATA;
/*********************creat node********************/
typedef struct node
{
	DATA *data;
	struct node *next;
}LINKLISTSTACK;
void Push(DATA *d,LINKLISTSTACK *p)
{
	LINKLISTSTACK *t;
	t=(LINKLISTSTACK *)malloc(sizeof(LINKLISTSTACK)); //create node
	t->data=d;           //input data to node
	t->next=p->next;     // insert node
	p->next=t;           // insert node
}
/****************delete after position p*************/
int Pop(LINKLISTSTACK *p)
{
	LINKLISTSTACK *t;
	if((p->next)!=NULL) 
	{
		t=p->next;
		p->next=t->next;
		free(t); 
	}
	else return 0; //if no node in the list.return 0.
	return 1;
}
int PrintNode(LINKLISTSTACK *p)
{
	if(p==NULL) return 0; //no node in the list.
	printf("i = %d\n",p->data->i);
	printf("j = %d\n",p->data->j);
	printf("k = %d\n",p->data->k);
	printf("\n");
	return 1;             //print success.
}
/******************print list*************************/
void PrintList(LINKLISTSTACK *head)
{
	int index=0;
	LINKLISTSTACK *p;
	p=head->next ;   //from head
	while(PrintNode(p))
	{
		printf("*****************************\n");
		printf("[%d] step:\n",index++);
	//	if(!PrintNode(p)) break;
		p=p->next;
	}
	printf("print list over.\n");
}
DATA *Input(int i, int j, int k)
{
	DATA *d;
	d=(DATA*)malloc(sizeof(DATA));
	d->i =i;
	d->j=j;
	d->k=k;
	return d;
}


LINKLISTSTACK *travel(char tile[12][12]) //display travel
{
	int i,j,k=3,temp=1;//temp=1 is going on 0, temp=0 is changed way from X to 0
	LINKLISTSTACK *head,*p; //create list
    DATA *d;  //create data point 
	p=(LINKLISTSTACK *)malloc(sizeof(LINKLISTSTACK));
	head=p;   //start list head and current position p. 
	p->next=NULL;    //end of list point to NULL.
	//start position is S,next is X
	for(i=0; i<12; i++)
	{ 
		if (tile[i][0]=='0')
		{  
			tile[i][0]='S'; 
		    tile[i][1]='X';
			j=1;
			break;
		}
	}	
	d=Input(2,0,0);
	Push(d,p);
	p=p->next ;
	//go is 0 and 0 change to X, back is 1, and 1 change to #
	while(1)
	{
		delay(); //time delay(automatically run)
	//	getch(); //hand contral
		system("cls");//clear screen
		//end is E
		if(j==10 && tile[i][j+1]=='0')
		{  
			tile[i][j+1]='E';
	        
			d=Input(i,j,3);
			Push(d,p);
			p=p->next ;
			d=Input(i,j+1,0);
			Push(d,p);
			p=p->next ;
			break;
		}
		
		//right is 0, change to X
        else if(tile[i][j+1]=='0' )
		{
               tile[i][j+1]='X';
			   k=3;//right
			   d=Input(i,j,k);
			   j=j+1;
			   
			  	if(temp)
				{
					Push(d,p);
					p=p->next;
				}
				temp=1;
		}
    	//bottom is 0, chang to X
        else if(tile[i+1][j]=='0')
		{
			tile[i+1][j]='X';
			k=2;//bottom
			d=Input(i,j,k);
			i=i+1;
			if(temp)
			{
				Push(d,p);
				p=p->next;
			}
			temp=1;
		}
		//if left is 0, change to X
		else if(tile[i][j-1]=='0')
		{
				tile[i][j-1]='X';
				k=1;  //left
				d=Input(i,j,k);
				j=j-1;
				if(temp)
				{
					Push(d,p);
					p=p->next;
				
				}
				temp=1;
		}   
 
    	//top is 0, change to X
        else if(tile[i-1][j]=='0')
		{
			tile[i-1][j]='X';
			k=4;//top
			d=Input(i,j,k);
			i=i-1;
		
			if(temp)
			{
				Push(d,p);
				p=p->next;
			}
			temp=1;
		}	
	//if there are no way to go, go back and X change to # 
		else
		{
			temp=0;
			tile[i][j]='#';
			Pop(p);
			if(tile[i+1][j]=='X')
			{
				i=i+1;
			}
			else if(tile[i][j+1]=='X' )
			{
               j=j+1;
			}
	        else if(tile[i][j-1]=='X')
			{
				j=j-1;
			}
            else if(tile[i-1][j]=='X')
			{
			   i=i-1;
			}	
			else
			{
				i=0;
				j=0;
				k=0;
				printf("There is no way to go\n");
				break;
			}
		}	
	
		print(tile);//print array
		printf("I'm moving to [%d][%d]",i,j);//print i and j 
		printf("\n\n\n\n");
	}
	print(tile);
	printf("Congratulations!!!\n\n\n");
	return head;
}


void delay() //time 
{
	int i,j,k;
	for (i=0; i<500; i++)
		for(j=0;j<500;j++)
			for(k=0;k<1000;k++)
		;

}
void print(char tile[12][12])//print array function
{
	int i, j;
	for(i=0;i<12;i++)
	{
		for(j=0;j<12;j++)
		{
			printf("%c",tile[i][j]);
		}
		printf("\n");
	}
}

int main()
{
	int i, j, k=0;
    LINKLISTSTACK *head;

   //init maze array tile[][] 
   char	tile[12][12]={'1','1','1','1','1','1','1','1','1','1','1','1', 
		              '1','0','0','0','0','1','0','0','0','0','1','1',
                      '0','0','1','0','1','0','1','1','0','1','0','1',
                      '1','1','1','0','1','0','0','0','0','1','0','1',
                      '1','0','0','0','0','1','1','1','0','1','0','0',
                      '1','1','0','1','0','1','0','0','0','1','0','1',
                      '1','0','0','1','0','1','0','1','0','1','0','1',
                      '1','1','0','1','0','1','0','1','1','1','0','1',
                      '1','0','0','0','0','0','0','0','0','1','0','1',
                      '1','0','1','1','1','1','0','1','1','1','0','1',
                      '1','0','0','0','0','0','0','0','0','0','0','1',
                      '1','1','1','1','1','1','1','1','1','1','1','1'};
          	printf("\n");
	//printf array
	for(i=0; i<12; ++i) 
	{
        for(j=0; j<12; ++j) 
		{
			printf("%c", tile[i][j]);
		}
		printf("\n");
	}  
	printf("The maze start with tile[2][0]\nEnd to tile[11][4]\n");
    printf("1 is wall, 0 is load\n");
	printf("\nplease enter any key to start \n");
	getch();
	head=travel(tile);
	PrintList(head);
	return 0;
}

⌨️ 快捷键说明

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