数据结构—迷宫(栈实现版)

    //
//  main.c
//  reo
//
//  Created by 郭瞾阳 on 14-8-13.
//  Copyright (c) 2014年 gzy. All rights reserved.
//

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

int a[10][10] = {
    {0,0,0,0,0,0,0,0,0,0},
    {0,1,1,0,1,1,1,0,1,0},
    {0,1,1,0,1,1,1,0,1,0},
    {0,1,1,1,1,0,0,1,1,0},            //0:边界  1:路   2:通路   -1:死路
    {0,1,0,0,0,1,1,1,1,0},
    {0,1,1,1,0,1,1,1,1,0},
    {0,1,0,1,1,1,0,1,1,0},
    {0,1,0,0,0,1,0,0,1,0},
    {0,0,1,1,1,1,1,1,1,0},
    {0,0,0,0,0,0,0,0,0,0}
};


typedef struct oo{
    int x;
    int y;
}seat;


typedef struct hee
{
    int i;
    int di;
    seat xy;
}DD;


typedef struct pp{
    DD t;
    struct pp*next;
}hehe;


void fist(hehe**fist)
{
    *fist  = (hehe*)malloc(sizeof(hehe));
    (*fist)->next = NULL;

}


void push(hehe*fist,DD d)
{
    hehe* p = (hehe*)malloc(sizeof(hehe));
    p->t = d;
    p->next = fist->next;
    fist->next = p;
    printf("%d   %d\n",d.xy.y,d.xy.x);
}


void pop(hehe*fist,DD *d)
{
    hehe*ww = fist->next;
    *d = fist->next->t;
    fist->next = ww->next;
    free(ww);
}


seat next(seat a,int i)
{
    seat b = a;
    if(i == 1)
        b.y++;
    if(i == 2)
        b.x++;
    if(i == 3)
        b.y--;
    if(i == 4)
        b.x--;
    return b;
}


int iszore(hehe*fist )
{
    if(fist->next == NULL)
        return 1;
    else
        return 0;
}


int pass(seat a_s)
{
    if(a[a_s.x][a_s.y] == 1)
        return 1;
    else
        return 0;
}



void footprint(seat a_s)
{
    a[a_s.x][a_s.y] = 2;
}


void makeprint(seat a_s)
{
    a[a_s.x][a_s.y] = -1;
}


int zoupath(seat go,seat end)
{
    hehe*f ;
    fist(&f);
    //printf("%d\n",f);
    int step = 1;
    DD e;
    seat pos = go;
    do{
        if(pass(pos))
        {
            footprint(pos);
            e.i = step;
            e.di = 1;
            e.xy = pos;
            push(f,e);
            if(pos.x == end.x&&pos.y ==end.y)
            {
                return 1;
            }
            pos = next(pos ,1);
            step++;
        }
        else
        {
            if(!iszore(f))
            {
                pop(f,&e);
                printf("%d   %d\n",e.xy.y,e.xy.x);
                while(e.di == 4&&!iszore(f))
                {
                    makeprint(e.xy); pop(f,&e);
                    printf("%d   %d\n",e.xy.y,e.xy.x);
                }   
                if(e.di<4)
                {
                    e.di++; push(f,e);
                    pos = next(e.xy,e.di);
                }
            }
        }
    }while(!iszore(f));
    return 0;
}

int main()
{
    seat a,b;
    a.x =1;
    a.y =1;
    b.x = 8;
    b.y = 8;
    printf("%d\n",zoupath(a,b));
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值