#include <malloc.h>
#define STACK_SIZE 100
#define STACK_SIZE_INC 10
enum result
{
OK,
ERR,
FAIL,
};
struct coordinate
{
int x;
int y;
};
typedef struct
{
struct coordinate *base;
struct coordinate *top;
int size;
}stack,*pstack;
enum result init_stack(pstack a)
{
a->base = (struct coordinate*)calloc(STACK_SIZE,sizeof(struct coordinate ));
a->top = a->base;
a->size = STACK_SIZE;
}
enum result push(pstack a,struct coordinate cur_coo)
{
if(a->top - a->base >= a->size)
{
realloc(a->base,(STACK_SIZE+STACK_SIZE_INC)*sizeof(struct coordinate));
a->size += STACK_SIZE_INC;
}
a->top->x = cur_coo.x;
a->top->y = cur_coo.y;
a->top++;
}
enum result pop(pstack a,struct coordinate *cur_coo)
{
if(a->base == a->top)
{
printf("the stack is empty yet");
return ERR;
}
a->top--;
cur_coo->x = a->top->x;
cur_coo->y = a->top->y;
}
int main()
{
stack maze;
init_stack(&maze);
int i,j;
int flag = 0;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
#if 0
for(i = 3; i >= 0; i--)
{
printf("%d,%d\n",dir[i][0],dir[i][1]);
}
sleep(10);
#endif
struct coordinate cur_coordinate;
char b[8][10] ;
char a[8][10] = {'#','#','#','#','#','#','#','#','#','#',
'#','*','#',' ',' ',' ',' ',' ',' ','#',
'#',' ','#',' ','#',' ','#','#','#','#',
'#',' ',' ',' ','#',' ',' ',' ',' ','#',
'#','#',' ','#','#','#','#','#','#','#',
'#',' ',' ','#',' ',' ',' ',' ',' ','#',
'#',' ',' ',' ',' ','#','#','#',' ','#',
'#','#','#','#','#','#','#','#','$','#'};
memcpy(b,a,80*sizeof(char));
for(i = 0; i < 8; i++)
{
for(j = 0; j < 10; j++)
{
printf("%c ",a[i][j]);
if('*' == b[i][j])
{
cur_coordinate.x = j;
cur_coordinate.y = i;
}
}
printf("\n");
}
//printf("the start add is %d,%d\n",cur_coordinate.x,cur_coordinate.y);
while('$' != b[cur_coordinate.x][cur_coordinate.y])
{
//printf("cur place is %d,%d\n",cur_coordinate.x,cur_coordinate.y);
sleep(1);
for(i = 3; i >= 0; i--)
{
//printf("the dir is %d,%d\n",dir[i][0],dir[i][1]);
if(' ' == b[cur_coordinate.x + dir[i][0]][cur_coordinate.y + dir[i][1]])
{
//printf("a space\n");
cur_coordinate.x += dir[i][0];
cur_coordinate.y += dir[i][1];
b[cur_coordinate.x][cur_coordinate.y] = '*';
for(i = 0; i < 8; i++)
{
for(j = 0; j < 10; j++)
{
printf("%c ",b[i][j]);
}
printf("\n");
}
flag = 1;
break;
}
else if('$' == b[cur_coordinate.x + dir[i][0]][cur_coordinate.y + dir[i][1]])
{
goto success;
}
}
if(flag)
{
push(&maze,cur_coordinate);
flag = 0;
}
else
{
pop(&maze,&cur_coordinate);
}
}
success:
printf("congratulation!!!!!!!!!!!!!!!!!!!\n");
}
#define STACK_SIZE 100
#define STACK_SIZE_INC 10
enum result
{
OK,
ERR,
FAIL,
};
struct coordinate
{
int x;
int y;
};
typedef struct
{
struct coordinate *base;
struct coordinate *top;
int size;
}stack,*pstack;
enum result init_stack(pstack a)
{
a->base = (struct coordinate*)calloc(STACK_SIZE,sizeof(struct coordinate ));
a->top = a->base;
a->size = STACK_SIZE;
}
enum result push(pstack a,struct coordinate cur_coo)
{
if(a->top - a->base >= a->size)
{
realloc(a->base,(STACK_SIZE+STACK_SIZE_INC)*sizeof(struct coordinate));
a->size += STACK_SIZE_INC;
}
a->top->x = cur_coo.x;
a->top->y = cur_coo.y;
a->top++;
}
enum result pop(pstack a,struct coordinate *cur_coo)
{
if(a->base == a->top)
{
printf("the stack is empty yet");
return ERR;
}
a->top--;
cur_coo->x = a->top->x;
cur_coo->y = a->top->y;
}
int main()
{
stack maze;
init_stack(&maze);
int i,j;
int flag = 0;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
#if 0
for(i = 3; i >= 0; i--)
{
printf("%d,%d\n",dir[i][0],dir[i][1]);
}
sleep(10);
#endif
struct coordinate cur_coordinate;
char b[8][10] ;
char a[8][10] = {'#','#','#','#','#','#','#','#','#','#',
'#','*','#',' ',' ',' ',' ',' ',' ','#',
'#',' ','#',' ','#',' ','#','#','#','#',
'#',' ',' ',' ','#',' ',' ',' ',' ','#',
'#','#',' ','#','#','#','#','#','#','#',
'#',' ',' ','#',' ',' ',' ',' ',' ','#',
'#',' ',' ',' ',' ','#','#','#',' ','#',
'#','#','#','#','#','#','#','#','$','#'};
memcpy(b,a,80*sizeof(char));
for(i = 0; i < 8; i++)
{
for(j = 0; j < 10; j++)
{
printf("%c ",a[i][j]);
if('*' == b[i][j])
{
cur_coordinate.x = j;
cur_coordinate.y = i;
}
}
printf("\n");
}
//printf("the start add is %d,%d\n",cur_coordinate.x,cur_coordinate.y);
while('$' != b[cur_coordinate.x][cur_coordinate.y])
{
//printf("cur place is %d,%d\n",cur_coordinate.x,cur_coordinate.y);
sleep(1);
for(i = 3; i >= 0; i--)
{
//printf("the dir is %d,%d\n",dir[i][0],dir[i][1]);
if(' ' == b[cur_coordinate.x + dir[i][0]][cur_coordinate.y + dir[i][1]])
{
//printf("a space\n");
cur_coordinate.x += dir[i][0];
cur_coordinate.y += dir[i][1];
b[cur_coordinate.x][cur_coordinate.y] = '*';
for(i = 0; i < 8; i++)
{
for(j = 0; j < 10; j++)
{
printf("%c ",b[i][j]);
}
printf("\n");
}
flag = 1;
break;
}
else if('$' == b[cur_coordinate.x + dir[i][0]][cur_coordinate.y + dir[i][1]])
{
goto success;
}
}
if(flag)
{
push(&maze,cur_coordinate);
flag = 0;
}
else
{
pop(&maze,&cur_coordinate);
}
}
success:
printf("congratulation!!!!!!!!!!!!!!!!!!!\n");
}