미로 찾기 코드

3132 단어 데이터 구조
#include 
#include 

#define M 6		//      
#define N 8		//      
#define MAXSIZE 64	//   

typedef struct
{
	int x;
	int y;
}item_t;

typedef struct
{
	int x;		//      
	int y;		
	int z;		//    
}coord_t;

typedef struct
{
	int x[MAXSIZE-1];
	int y[MAXSIZE-1];
	int z[MAXSIZE-1];
	
	int top ;
}stack_t;

//    
int maze[M+2][N+2] =
{//	  0   1   2   3   4   5   6   7   8   9		
	{ 1,  1,  1,  1,  1,  1,  1,  1,  1,  1 },//	0
	{ 1,  0,  1,  1,  1,  0,  1,  1,  1,  1 },//	1		
	{ 1,  1,  0,  1,  0,  1,  1,  1,  1,  1 },//	2
	{ 1,  0,  1,  0,  0,  0,  0,  0,  1,  1 },//	3
	{ 1,  0,  1,  1,  1,  0,  1,  1,  1,  1 },//	4
	{ 1,  1,  0,  0,  1,  1,  0,  0,  0,  1 },//	5
	{ 1,  0,  1,  1,  0,  0,  1,  1,  0,  1 },//	6
	{ 1,  1,  1,  1,  1,  1,  1,  1,  1,  1 } //	7
};

//    
item_t move[8] = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};

stack_t* init_stack(void)
{
	stack_t* st = (stack_t *)malloc(sizeof(stack_t));
	if(st == NULL)
	{
		printf("malloc error
"); return NULL; } st->top = -1; return st; } int empty_stack(stack_t* st) { if(st->top == -1) return 1; else return 0; } int full_stack(stack_t* st) { if(st->top > MAXSIZE -1) return 1; else return 0; } int push_stack(stack_t* st, coord_t cor) { if(full_stack(st)) { printf("stack full.
"); return 0; } st->top ++ ; st->x[st->top] = cor.x ; st->y[st->top] = cor.y ; st->z[st->top] = cor.z ; return 1; } int pop_stack(stack_t* st, coord_t* cor) { if(empty_stack(st)) { printf("stack empty.
"); return 0; } cor->x = st->x[st->top] ; cor->y = st->y[st->top] ; cor->z = st->z[st->top] ; st->top-- ; return 1; } void loop_stack(stack_t* st) { coord_t cor; printf(" :
"); while(!empty_stack(st)) { pop_stack(st, &cor); printf("x=%d y=%d z=%d
",cor.x, cor.y, cor.z); } } int main(void) { int x,y,z,i,j; coord_t cor; stack_t* st = init_stack() ; cor.x = 1; cor.y = 1; cor.z = -1; // push_stack(st, cor); // printf(" ...
"); while(!empty_stack(st)) // { pop_stack(st, &cor); // x = cor.x; y = cor.y; z = cor.z + 1; // while(z<8) { i = x + move[z].x; // j = y + move[z].y; if(maze[i][j] == 0) // { cor.x = x; // cor.y = y; cor.z = z; push_stack(st, cor); x = i; // y = j; maze[i][j] = -1; // if((x==M) && (y==N)) // { cor.x = x; // cor.y = y; cor.z = z; push_stack(st, cor); goto loop; } else z = 0; } else
			{
			 	z++ ;
			}
		}
	}
	
loop:	
	loop_stack(st);
	
	free(st);
	return 0;
}

좋은 웹페이지 즐겨찾기