'첫 스킨 십' - 데이터 구조, 미궁의 생 성, 소통 (비 귀속)


            무슨 말 인지 모 르 겠 어 요.코드 를 직접 보 세 요. 전에 대학교 2 학년 때 했 던 데이터 구조의 과정 디자인 은 선생님 이 주신 대로 했 습 니 다.하 이 라 이 트 는 미로 가 자동 으로 생 성 되 고 노선 표시, 출력 절차 입 니 다.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
",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
",(*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
",(*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
"); 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
"); 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
"); 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
"); 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("
find way print screen with picture
"); 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("
"); } printf("
"); while(n) { printf("x = %2d | y = %d
", (*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("
"); } } 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); }

좋은 웹페이지 즐겨찾기