| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 342 人关注过本帖
标题:求助,为什么这段代码运行的结果出现内存不能读
取消只看楼主 加入收藏
_leaf
Rank: 1
来 自:江西农业大学
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-12-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
求助,为什么这段代码运行的结果出现内存不能读
#include<stdio.h>
#include<malloc.h>
#define Maxsize 100
#define NULL 0

typedef struct         //定义迷宫
{
    int arry[Maxsize][Maxsize];   
    int max_x,max_y;            
}sd;

typedef struct point        
{
    int vex_x,vex_y;        
    struct point *next;   
    int direction;         
}Point;

sd create()         
{
    int i,j,x;
    sd a;
    printf("Enter row and lie:");
    scanf("%d  %d",&a.max_x,&a.max_y);   
    printf("提示:请输入0或1,其中0表示通路,1表示不通\n");
    for(i=1;i<=a.max_x;i++)
    {
        printf("enter %d row, (please enter %d datas):",i,a.max_y);
        for(j = 1;j<= a.max_y;j++)
        {
            scanf("%d",&a.arry[i][j]);
        }
    }
    return a;
}


int found(int x,int y,Point *head)  
{
    Point *p = head;
    while(p!=NULL)
    {
        if(x==p->vex_x&&y==p->vex_y)
        return 1;
        p=p->next;
    }
    return 0;
}


Point *secret(sd a)  
{
    Point *top,*p;   
    int i,j,m,x,y;
    p = (Point*)malloc(sizeof(Point));  
    p->vex_x-1;p->vex_y=1;p->next=NULL;
    top=p;        
    j=1;        
    do{
        while(j<=4)
        {
            m=0;  
            x=top->vex_x;
            y=top->vex_y;
            switch(j)
            {
                case 1:   
                    if(y+1<=a.max_y&&!a.arry[x][y+1]&&!found(x,y+1,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x;p->vex_y=y+1;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
                case 2:
                    if(x+1<=a.max_x&&!a.arry[x+1][y]&&!found(x+1,y,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x+1;p->vex_y=y;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
                case 3:
                    if(y-1>1&&!a.arry[x][y-1]&&!found(x,y-1,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x;p->vex_y=y-1;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
                case 4:
                    if(x-1>=1&&!a.arry[x-1][y]&&!found(x-1,y,top))
                    {
                        p=(Point*)malloc(sizeof(Point));
                        p->vex_x=x-1;p->vex_y=y;p->next=top;
                        top->direction=j;
                        top=p;
                        m=1;
                    }
                    break;
            }
            if(m!=0)  
                {j=1;break;}
              else
                j++;  
            }
        if(j>4)  
        {
            if(top!=NULL)   
            {
                top=top->next;j=top->direction+1;top->direction=j;
            }
            else return NULL;   
        }
    }while(top->vex_x!=a.max_x||top->vex_y!=a.max_y);
    return top;
}

void disp(Point *po)
{
    int i=0,top=0;
    Point *stack[Maxsize];
    if(po==NULL)
        printf("NO ROAD!\n");
    else
    {
        while(po!=NULL)
        {
            stack[top++]=po;
            po=po->next;
        }
        while(top>0)
        {
            top--;
            printf("(%d,%d,%d)",stack[top]->vex_x,stack[top]->vex_y,stack[top]->direction);
            i++;
            if(i%8==0)
                printf("\n");
        }
    }
    printf("\n");
}

 main()
{
    sd road;
    Point *po;
    road=create();
    po=secret(road);
    disp(po);
    getchar();
}
搜索更多相关主题的帖子: create include direction 内存 
2012-12-15 21:13
快速回复:求助,为什么这段代码运行的结果出现内存不能读
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018785 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved