当前位置:三九宝宝网 → 宝宝教育 → 教学论文 → 正文

c语言迷宫问题

更新:12-22 整理:39baobao.com
字体:

[C语言数组编程题]#include <stdio.h> #include <string.h> void charNo(char *ch) { int a,b,c,d; int temp; int i; a=b=c=d=0; for(i=0;i<strlen(ch);i++) { temp=(int)ch[i]; if((temp>=6...+阅读

问题出在MazePath内部的e是一个局部变量,并且随着while循环其内容不断变化。保存一个局部变量的地址是没有意义的,函数返回后就被清除。解决的办法是动态分配QElemType类型的对象,还有要注意一下,这个程序中分配的内存最后都没有被释放,这可是个不好的编程习惯。 改好了,你自己看看有没有错误。 #include#include#includetypedef struct QElemType { int x,y; struct QElemType *parent;//用于存储节点的前一个节点 } QElemType; typedef struct QNode//队列节点 { QElemType *data; struct QNode *next; } QNode, *QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; } LinkQueue; void InitQueue(LinkQueue *Q)//创建队列 { Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode)); Q->rear->next = NULL; //Q->front=Q->rear=NULL; } void EnQueue(LinkQueue *Q, QElemType *e)//将元素入队 { QueuePtr p; p=(QueuePtr)malloc(sizeof(QNode)); p->data=e; p->next=NULL; Q->rear->next=p; Q->rear=p; } QElemType *DeQueue(LinkQueue *Q)//将元素出对 { QElemType *e; QueuePtr p; p=Q->front->next; e=p->data; Q->front->next=p->next; if(Q->rear==p) Q->rear=Q->front; free(p); return (e); } int QueueEmpty(LinkQueue *Q)//判断队列是否为空 { if(Q->front==Q->rear ) return 1; else return 0; } QElemType *NextPos(QElemType *e,int i)//节点的下一个位置 { QElemType *t = (QElemType *)malloc(sizeof(QElemType)); *t = *e; switch(i) { case 1:t->y=t->y+1;break; case 2:t->x=t->x+1;break; case 3:t->y=t->y-1;break; case 4:t->x=t->x-1;break; } return (t); } QElemType *MazePath(int maze[][10],LinkQueue *Q)//迷宫函数 { int i; QElemType *e, *p; e = (QElemType *)malloc(sizeof(QElemType)); // InitQueue(&Q); e->x=1;//入口位置,即为第一个节点 e->y=1; e->parent=NULL; //第一个节点的前一个节点赋值为空 EnQueue(Q, e);//将第一个节点入队 while(!QueueEmpty(Q)) { e=DeQueue(Q); //把元素出对,并赋值给e if(e->x==8&e->y==8)//出口位置,直接返回最后一个节点 return e; for(i=1;iparent=e; if(maze[p->x][p->y]==1) { maze[p->x][p->y]=2; EnQueue(Q,p); } else { free(p); p = NULL; } } } printf("zhao bu dao");//找不到路径 return NULL; } void main()//主函数 { LinkQueue Q; QElemType *p, *t; int maze[10][10]=//其中1为能走通 {{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,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} }; InitQueue(&Q); p=MazePath(maze,&Q);//e即为最后一个节点 while(p)//回溯寻找路径 { printf("(%d,%d),",p->x,p->y); t = p->parent; free(p); p = t; } getchar(); }

本文地址:https://www.39baobao.com/show/29_12957.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

c语言编程题数组#include <stdio.h> int main() { char str[100]={0}; char ch; int i=0; printf("输入字符串!\n"); scanf("%c",&ch); while(ch!='\n') { if (ch<='z'&&ch>='a') { if(ch=='z')ch=...

C语言程序设计基础数组问题一. 输入不超过100个整数,然后按从小到大的次序输出。#define M 10 main() {int a[M],i,j,t; printf("请输入数据:"); for(i=0;i<M;i++) scanf("%d",&a[i]); for(i=0;i<M-1;i++) for(...

求几道简单C语言编程题关于数组的感谢 !第一题: #include "stdio.h" void main() {int a[10]; int i,max; for(i=0;i<10;++i) scanf("%d,",&a[i]); max=a[0]; for(i=1;i<10;i++) { if(max<a[i]); max=a[i]; } printf("最...

C语言一维数组程序题不知道你那一点不明白,我就一点一点的讲解啦!别嫌啰嗦啊! 1.在头文件stdio.h和string.h中给出了函数的原型。使用字符串处理函数时要引入相应的头文件。 2.char a[80]="AB",b[80]...

谁帮我做下c语言数组的题#include<stdio.h> int max,min; main() { int a[10],i,*p; void a1(); printf("请输入十个整数\n"); for(i=0;i<10;i++) scanf("%d",a[i]); p=a; a1(p); printf("max=%d,min=%d",ma...

C语言程序设计数组题原发布者:爱笑的涂鸦哥 第4章数组4.1内容概述本章主要介绍了数值数组和字符数组的定义、初始化、元素引用和数组数据的输入与输出,字符数组实现字符串、字符串函数的实现与调...

找一些C语言的关于数组的程序题14下面程序可求出矩阵 a 的两条对角线上的元素之和,请填空 j=2 j>=0 15. 下面程序段将输出 computer,请填空。 iif(i16. 下面程序的功能是在三个字符串中找出最小的。请填空。...

C语言一道简单的数组编程题//#include "stdafx.h"//vc++6.0加上这一行. #include "stdio.h" void main(void){ int a[1000],i,k; for(k=i=0;iif((a[i]=i+1)%77 & (a[i]%7==0 || a[i]%11==0)) printf(++k%5...

用C语言编一个迷宫程序-# --------### # ## # # ### ----# # #-## # # # #### #### # ## ## #-# ## # # # #---# ## # ## # # # # -# ### ## #### ## # # ----# # # ## # # # ## ### ## -# --# --...