“第一次亲密接触”--数据结构,迷宫的生成、走通(非递归)

 

            也不知道说什么?直接看代码吧,之前上大二的时候做的一个数据结构的课程设计,按照老师给的做的。亮点就是迷宫自动生成,路线标注、输出步骤。在redhat中测试没有问题。

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define AMAX	200
#define MAZE	2
#define WAY    1

//maze's size
int mazeX,mazeY;
//1 is wall 0 is way
int maze[99][99] = {1};

//0 no errro 1 exit 2 overflow bound 3 malloc fault
int merror = 0 ;



typedef struct mazeList
{
	int x,y,dir;
	struct mazeList * child ;
	struct mazeList * parent ;
}mazeList,*mazeNode ;




// use print message
void Print(char *p)
{
	printf("%s\n",p);
}


// string convert to int
//argurment c is convert stirng
//reslut -1 not a number other is a number 
int TestNum(char *str)
{
	int result = 0 ;
	while( '\0' != (*str) )
	{
		if('0' <= (*str) && '9' >= (*str))
		{
			result = result * 10 + ( (*str) - '0');
		}
		else
		{
			result = - 1;
			break;
		}
		str ++;if(-1 == mazeX )
		{
			Print("Input maze's is not a number");
		}
	} 
	return result;
}


int Find(mazeList *head ,int x,int y)
{
	while( head)
	{
		if(x == (*head).x  && y == (*head).y )
			return 1 ;
		head = (*head).child ;
	}
	return 0 ;
}


void GetMaze()
{
	
	char x[AMAX],y[AMAX];
	char bl = 1 ;
	while(bl)
	{
		Print("Input two number to maze's x and y, split with blank space ");
		scanf("%s %s",x,y);
		if(2 < strlen(x))
		{

			Print("Input maze's x is overflow bound");
			continue;
		}
		if(2 < strlen(y))
		{
			Print("Input maze's x is overflow bound");
			continue ;
		}
		mazeX = TestNum(x) ;
		if(-1 == mazeX )
		{
			Print("Input maze's x is not a number");
			exit(0);
		}
		mazeY = TestNum(y) ;
		if(-1 == mazeX )
		{
			Print("Input maze's y is not a number");
			exit(0);
		}
		bl = 0;
	}
}


int sr(int x,int y)
{
   static int d[4][2]={0,1,1,0,0,-1,-1,0};
    int zx=x*2,zy=y*2,nx,tn=rand()%2? 1:3,i;
    maze[zx][zy]=1;
    for (i=0,nx=rand()%4;i<4;i++,nx=(nx+tn)%4)
        if (maze[zx+2*d[nx][0]][zy+2*d[nx][1]]==0)
            maze[zx+d[nx][0]][zy+d[nx][1]]=1,sr(x+d[nx][0],y+d[nx][1]);
    return 0;
}


void InitMaze()
{
	int z1,z2;
	int x,y;
	x = mazeX;
	y = mazeY;
    for (z1=0,z2=2*y+2;z1<=2*x+2;z1++)
		maze[z1][0]=1,maze[z1][z2]=1;
    for (z1=0,z2=2*x+2;z1<=2*y+2;z1++)
		maze[0][z1]=1,maze[z2][z1]=1;
    maze[1][2]=1;
	 maze[2*x+1][2*y]=1;
    srand((unsigned)time(NULL));
    sr(rand()%x+1,rand()%y+1);

	/*char value[81] = "0010001000100010000011000111000000010000010001000111011010000000";
	int loc,j = 0 ,i = 0;
	mazeX = 8 ;
	mazeY = 8 ;
	
	while(mazeX > i )
	{
		j = 0;
		while(mazeY > j)
		{
			maze[i][j] = value[loc] - '0';
			loc++;
			j++;
		}
		i++;
	}*/
	
}
int aa = 0;
void SearchMaze(mazeList *h)
{
	int i = 1 ;
	mazeNode item = h,newnode;
	int boundx = mazeX*2+1;
	int boundy = mazeY*2+1;
	for( i = 1 ; i< boundy;i++)
	{
		if(WAY == maze[1][i])
		{
			(*item).x = 1;;
			(*item).y = i;
			//printf("%d %d %d\n",(*item).x,(*item).y,(*item).dir);
			break;
		}
	}
	while( i)
	{
		if(boundx == (*item).x  && WAY == maze[(*item).x][(*item).y])
		{
			break;
		}
		aa++;
		if(330 < aa)
		{break;}
		//printf("%d %d %d\n",(*item).x,(*item).y,(*item).dir);
		if( 1 < (*item).x  && 0 ==  (*item).dir)
		{
			if(WAY == maze[((*item).x)-1][(*item).y] )
			{
				newnode = (mazeNode) malloc(sizeof(maze));
				if(0 == newnode)
				{
					Print("malloc error in SearchMaze\n");
					exit(0);
				}
				(*item).dir ++;
				if(1 != Find(h, ((*item).x-1),(*item).y))
				{
					(*newnode).x = ((*item).x )- 1;
					(*newnode).y = (*item).y;
					(*newnode).child = 0 ;
					(*newnode).dir = 0 ;
					(*newnode).parent = item ;
					(*item).child = newnode ;
					item = newnode ;
					continue;
				}
			}

		}
		if(0 == (*item).dir) (*item).dir ++;
		if(boundx > (*item).x  && 1 == (*item).dir)
		{
			if(WAY == maze[((*item).x)+1][(*item).y] )
			{
				newnode = (mazeNode) malloc(sizeof(maze));
				if(0 == newnode)
				{
					Print("malloc error in SearchMaze\n");
					exit(0);
				}
				(*item).dir ++;
				if(1 != Find(h, ((*item).x)+1,(*item).y))
				{
					(*newnode).dir = 0 ;
					(*newnode).x = ((*item).x) + 1;
					(*newnode).y = (*item).y;
					(*newnode).child = 0 ;
				
					(*newnode).parent = item ;
					(*item).child = newnode ;
					item = newnode ;
					continue;
				}
			}
			
		}
		if(1 == (*item).dir) (*item).dir ++;
		if( 1 < (*item).y && 2 == (*item).dir )
		{
			if(WAY == maze[(*item).x][((*item).y)-1]  )
			{
				newnode = (mazeNode) malloc(sizeof(maze));
				if(0 == newnode)
				{
					Print("malloc error in SearchMaze\n");
					exit(0);
				}
				(*item).dir ++;
				if(1 != Find(h, (*item).x,((*item).y)-1))
				{
					(*newnode).x = (*item).x ;
					(*newnode).y = ((*item).y) - 1;
					(*newnode).child = 0 ;
					(*newnode).dir = 0 ;
					(*newnode).parent = item ;
					(*item).child = newnode ;
					item = newnode ;
					continue ;
				}
			}
			
		}
		if(2 ==(*item).dir ) (*item).dir ++;
		if(boundx > (*item).y && 3 == (*item).dir)
		{
			if(WAY == maze[(*item).x][((*item).y)+1]  )
			{
				newnode = (mazeNode) malloc(sizeof(maze));
				if(0 == newnode)
				{
					Print("malloc error in SearchMaze\n");
					exit(0);
				}
				(*item).dir ++;
				if(1 != Find(h, (*item).x,((*item).y)+1))
				{
					(*newnode).x = (*item).x ;
					(*newnode).y = ((*item).y) + 1 ;
					(*newnode).child = 0 ;
					(*newnode).dir = 0 ;
					(*newnode).parent = item ;
					(*item).child = newnode ;
					item = newnode ;
					continue ;
				}
			}
			
		}
		if( (*item).dir == 3) (*item).dir ++;
		
		if(!(*item).parent)
		{
			Print("no way");
			exit(0);
		}
		newnode = item ;
		item = (*item).parent ;
		(*item).child = 0;
		free(newnode);
	}
}


void mazePrint(mazeList head)
{
	mazeNode fn,n = &head ;
	int x,y,z1,z2;   
  
	x = mazeX;
	y = mazeY;
	Print("\n find way print screen with picture\n");
	for (z2=1;z2<=y*2+1;z2++)
    	{
      for (z1=1;z1<=x*2+1;z1++)
		{
			if(Find(n, z1,z2)	)
			{
				printf("-");
			}
			else
			{
				printf(maze[z1][z2]?"0":"1");
			}
		}
      printf("\n");
    	}
	printf("\n");

	while(n)
	{
		printf("x = %2d | y = %d \n", (*n).x,(*n).y);
		fn = n ;
		n = (*n).child;
		//(*n).parent = 0;
		//free(fn);
	}
}
 
void p()
{
	
	int x,y,z1,z2;
	x = mazeX;
	y = mazeY;
	Print("maze is ");
	for (z2=1;z2<=y*2+1;z2++)
    	{
      for (z1=1;z1<=x*2+1;z1++)
		{

			printf(maze[z1][z2]?"0":"1");
		}
      printf("\n");
    	}

}


int main()
{
	mazeList mhead;
	mhead.x = 0;
	mhead.y = 0;
	mhead.dir = 0 ;
	mhead.child = 0 ;
	mhead.parent = 0 ;
	mazeX = 6 ;
	mazeY = 4 ;
	//GetMaze();
	InitMaze();
	p();
	SearchMaze(&mhead);
	mazePrint(mhead);
}

你可能感兴趣的:(数据结构,struct,String,redhat,input)