栈遍历式迷宫

#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");


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值