当前位置:首页 » 编程语言 » c语言迷宫问题用队列实现
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言迷宫问题用队列实现

发布时间: 2022-03-03 04:44:43

c语言编程 迷宫问题(队列)

c#界面绘制的时候,底层重绘每次会清除画布背景,然后再全部重新绘制,这才是导致闪烁最主要的原因。于是重载消息发送函数操作,禁掉这条消息。代码如下:
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0014) // 禁掉清除背景消息
return;
base.WndProc(ref m);
}

㈡ 数据结构c语言迷宫求解,并用栈和队列实现,大神,帮帮忙吧 还要调试成功

㈢ 迷宫问题(栈或队列,最短路径)(c语言)

#include<stdio.h>
#include<stdlib.h>
#define M 15
#define N 15
struct mark //定义迷宫内点的坐标类型
{
int x;
int y;
};

struct Element //"恋"栈元素,嘿嘿。。
{
int x,y; //x行,y列
int d; //d下一步的方向
};

typedef struct LStack //链栈
{
Element elem;
struct LStack *next;
}*PLStack;

/*************栈函数****************/

int InitStack(PLStack &S)//构造空栈
{
S=NULL;
return 1;
}

int StackEmpty(PLStack S)//判断栈是否为空
{
if(S==NULL)
return 1;
else
return 0;
}

int Push(PLStack &S, Element e)//压入新数据元素
{
PLStack p;
p=(PLStack)malloc(sizeof(LStack));
p->elem=e;
p->next=S;
S=p;
return 1;
}

int Pop(PLStack &S,Element &e) //栈顶元素出栈
{
PLStack p;
if(!StackEmpty(S))
{
e=S->elem;
p=S;
S=S->next;
free(p);
return 1;
}
else
return 0;
}

/***************求迷宫路径函数***********************/
void MazePath(struct mark start,struct mark end,int maze

­[N],int diradd[4][2])
{
int i,j,d;int a,b;
Element elem,e;
PLStack S1, S2;
InitStack(S1);
InitStack(S2);
maze[start.x][start.y]=2; //入口点作上标记
elem.x=start.x;
elem.y=start.y;
elem.d=-1; //开始为-1
Push(S1,elem);
while(!StackEmpty(S1)) //栈不为空 有路径可走
{
Pop(S1,elem);
i=elem.x;
j=elem.y;
d=elem.d+1; //下一个方向
while(d<4) //试探东南西北各个方向
{
a=i+diradd[d][0];
b=j+diradd[d][1];
if(a==end.x && b==end.y && maze[a][b]==0) //如果到了出口
{
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem);
elem.x=a;
elem.y=b;
elem.d=886; //方向输出为-1 判断是否到了出口
Push(S1,elem);
printf("\n0=东 1=南 2=西 3=北 886为则走出迷宫\n\n通路为:(行坐标,列坐标,方向)\n");
while(S1) //逆置序列 并输出迷宫路径序列
{
Pop(S1,e);
Push(S2,e);
}
while(S2)
{
Pop(S2,e);
printf("-->(%d,%d,%d)",e.x,e.y,e.d);
}
return; //跳出两层循环,本来用break,但发现出错,exit又会结束程序,选用return还是不错滴o(∩_∩)o...
}
if(maze[a][b]==0) //找到可以前进的非出口的点
{
maze[a][b]=2; //标记走过此点
elem.x=i;
elem.y=j;
elem.d=d;
Push(S1,elem); //当前位置入栈
i=a; //下一点转化为当前点
j=b;
d=-1;
}
d++;
}
}
printf("没有找到可以走出此迷宫的路径\n");
}

/*************建立迷宫*******************/
void initmaze(int maze

­[N])
{
int i,j;
int m,n; //迷宫行,列

printf("请输入迷宫的行数 m=");
scanf("%d",&m);
printf("请输入迷宫的列数 n=");
scanf("%d",&n);
printf("\n请输入迷宫的各行各列:\n用空格隔开,0代表路,1代表墙\n",m,n);
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&maze[i][j]);
printf("你建立的迷宫为o(∩_∩)o...\n");
for(i=0;i<=m+1;i++) //加一圈围墙
{
maze[i][0]=1;
maze[i][n+1]=1;
}
for(j=0;j<=n+1;j++)
{
maze[0][j]=1;
maze[m+1][j]=1;
}
for(i=0;i<=m+1;i++) //输出迷宫
{
for(j=0;j<=n+1;j++)
printf("%d ",maze[i][j]);
printf("\n");
}
}

void main()
{
int sto

­[N];
struct mark start,end; //start,end入口和出口的坐标
int add[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//行增量和列增量 方向依次为东西南北

initmaze(sto);//建立迷宫

printf("输入入口的横坐标,纵坐标[逗号隔开]\n");
scanf("%d,%d",&start.x,&start.y);
printf("输入出口的横坐标,纵坐标[逗号隔开]\n");
scanf("%d,%d",&end.x,&end.y);

MazePath(start,end,sto,add); //find path
system("PAUSE");
}

㈣ c语言用栈解决迷宫问题

明摆着图的深度遍历 用堆栈模拟递归

㈤ c语言版数据结构,要求用队列求解迷宫最短路径。

// Migong_Queue.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include "stdlib.h"
#define MaxSize 100
struct
{
int i,j;
int pre;
}Qu[MaxSize];
int front = -1,rear = -1;

int mgpath(int **,int ,int ,int ,int);
void print(int);
int _tmain(int argc, _TCHAR* argv[])
{
int l,h;
FILE *migong=fopen("C:\\Users\\Administrator\\Documents\\Visual Studio 2010\\Projects\\数据结构实验\\Migong_Queue\\migong.txt","r");
if(migong==NULL)
{
printf("Can't open the file!!!");
exit(0);
}
char ch=fgetc(migong);
l=0;
while(ch!='\n')
{
if(ch!=',')
l++;
ch=fgetc(migong);
}//l为迷宫的长度
rewind(migong);
//接下来是算出迷宫的高度h!~~
h=0;
ch=fgetc(migong);
while(!feof(migong))
{
if(ch=='\n')
h++;
ch=fgetc(migong);
}

if(l<5||h<5)
{
printf("It's too small!");
exit(0);
}
rewind(migong);

int **mg=(int **)malloc(sizeof(int *)*h);
for(int h_=0;h_<h;h_++)
{
mg[h_]=(int *)malloc(sizeof(int)*l);
}
for(int h_=0;h_<h;h_++)
{
for(int l_=0;l_<l;)
{
ch=fgetc(migong);
if(ch!=',')
{mg[h_][l_]=ch-'0';l_++;}

}
ch=fgetc(migong);
}
fclose(migong);

printf("The map is presented as follows:\n");
for(int _x=0;_x<h;_x++)
for(int _y=0;_y<l;_y++)
{
printf("%d ",mg[_x][_y]);
if((_y+1)%l==0)
printf("\n");
}
mgpath(mg,1,1,8,8);
return 0;
}
int mgpath(int **mg,int xi,int yi,int xe,int ye)
{
int i,j,find=0,di;
rear++;
Qu[rear].i=xi;
Qu[rear].j=yi;
Qu[rear].pre=-1;
mg[xi][yi]=-1;
while(front<=rear&&!find)
{
front++;
i=Qu[front].i;j=Qu[front].j;
if(i==xe&&j==ye)
{
find=1;
print(front);
return 1;
}
for(di=0;di<4;di++)
{
switch(di)
{
case 0:i=Qu[front].i-1;j=Qu[front].j;break;
case 1:i=Qu[front].i;j=Qu[front].j+1;break;
case 2:i=Qu[front].i+1;j=Qu[front].j;break;
case 3:i=Qu[front].i;j=Qu[front].j-1;break;
}
if(mg[i][j]==0)
{
rear++;
Qu[rear].i=i;Qu[rear].j=j;
Qu[rear].pre=front;
mg[i][j]=-1;
}
}
}
return 0;
}
void print(int front)
{
int k=front,j,ns=0;
printf("\n");
do
{
j=k;
k=Qu[k].pre;
Qu[j].pre=-1;
}while(k!=0);
printf("迷宫路径如下:\n");
k=0;
while(k<MaxSize)
{
if(Qu[k].pre==-1)
{
ns++;
printf("\t(%d,%d)",Qu[k].i,Qu[k].j);
if(ns%5==0)printf("\n");
}
k++;
}
printf("\n");
}

我是从文件中读取迷宫数组的!
你也可以改为直接输入!
希望对你有帮助!谢谢!

㈥ C语言中用栈实现迷宫问题

#include
#define MAXSIZE 100
using namespace std;
struct stack{
int iway;
int jway;
int dire;
};
stack maze[MAXSIZE];
int top;
int map[14][28]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

void findway(int xS,int yS,int xE,int yE)
{
top=0;
maze[top].iway=xS;
maze[top].jway=yS;
map[xS][yS]=-1;
int i,j,di,find;
while(top>-1)
{
i=maze[top].iway;
j=maze[top].jway;
di=maze[top].dire;
if(i==xE&&j==yE)
{
cout<<"***********************************\n";
cout<<"path"<<":"<<endl;
cout<<"("<<maze[0].iway<<","<<maze[0].jway<<")";
for(int val=1;val<top+1;val++)
{
cout<("<<maze[val].iway<<","<<maze[val].jway<<")";
if((val+1)%5==0)
cout<<endl;
}
cout<<endl;
cout<<"***********************************\n";
return;
}
find=0;
while(find==0&&di<4)
{
di++;
switch(di)
{
case(0):i=maze[top].iway;
j=maze[top].jway+1;
break;
case(1):i=maze[top].iway;
j=maze[top].jway-1;
break;
case(2):i=maze[top].iway+1;
j=maze[top].jway;
break;
case(3):i=maze[top].iway-1;
j=maze[top].jway;
break;
}
if(map[i][j]==0)
{
find=1;
}
}
if(find==1)
{
maze[top].dire=di;
top++;
maze[top].iway=i;
maze[top].jway=j;
maze[top].dire=-1;
map[i][j]=-1;
}
else
{
map[maze[top].iway][maze[top].jway]=0;
top--;
}
}
}
int main()
{
for(int i=0;i<14;i++) //迷宫图形化输出
{
for(int j=0;j<28;j++)
{
if(map[i][j]==1)
cout<<"■";
else cout<<"□";
}
cout<<endl;
}
int xStart,yStart,xEnd,yEnd;
cout<<"请输入迷宫起点坐标,用空格隔开(左上角坐标为(0,0)):";
cin>>xStart>>yStart;
cout<<"请输入迷宫终点坐标,用空格隔开(右上角坐标为(13,27)):";
cin>>xEnd>>yEnd;
findway(xStart,yStart,xEnd,yEnd);
return 0;
}
满意请采纳!

㈦ c语言编写迷宫的问题 c语言

把格子设置为0、1状态,0就是没有障碍可以走,1就是有障碍不能走

㈧ 用C语言编写迷宫问题,

1995年第十二届IOCCC获奖作品,由葡萄牙的Carlos Duarte先生所编写。评委们评论说:
This could be used as the basis of an a-maze-ing screen exerciser.

以下是作者本人对程序的简要介绍:
A program that generates a maze, and then solves it, all this being seen by the user. Some highlights of obfuscation are: 3 steps functions in one - main(), several recursive calls with conditional actions, and just one (big and ugly) statement to solve the maze.

你可以从www.ioccc.org上找到这个名为ca.c的源程序,也可以参考下面的代码。由于这里上传的代码会被重新格式化,强烈建议你去下载原来的程序。
/*************************************************************/
#define r return

char*u0="<RET> to begin... ",*u1="Already been here!",*u2="Found a wall! \
",*u3="Walking... ",*u4="Finished. ",*u5="Going back..\
. ",*o="\033[23;1HDone!!\n",*x="\033[2J",*y="\033[1;1H",*z="\033[%d;%\
dH%c",*w="\033[1;1H%s",*v="\033[%d;%dH%c\033[%d;%dH%c\033[%d;%dH%c",b[1841
];int c,d,e,f,g;typedef int(*h)();h i,j,k,l,m,n;int printf(),srand(),rand(
),time(),getchar();int main(int a){i=printf,j=srand,k=rand,l=time,m=getchar,
n=main;if(!c)for(j(l(0)),g=a=1000,--d;++d<1840;b[c=d]=" #\n"[d%80==79?2:d/80
&&d%80&&d/80-22&&d%80-78]);if(!(c-1839))++c,i("%s%s%s",x,y,b);k:if(!(c-1840)
&&(b[a+2]+b[a-2]+b[a+160]+b[a-160]-4*' ')){while(b[a+(f=(e=k()%4)?e-1?e-2?-1
:1:-80:80)*2]!='#');b[a]=b[a+f]=b[f+a+f]=' ';i(v,a/80+1,1+a%80,' ',(a+f)/80+
1,1+(a+f)%80,' ',(f+a+f)/80+1,1+(f+a+f)%80,' ');n(f+a+f);goto k;}else if(!(g
-a))c=1,a=162,i(w,u0),m();if(c-1){}else r b[a]!=' '?(i(w,b[a]=='.'?u1:u2),0)
:(b[a]='.',i(w,u3),i(z,a/80+1,1+a%80,'.'),a==1676?(i(w,u4),i(o),1):n(a+1)||n
(a+80)||n(a-80)||n(a-1)?1:(b[a]=' ',i(w,u5),i(z,a/80+1,1+a%80,' '),0));r 0;}

我曾运行过这个程序,非常的有意思,当然,尝试理解这样的代码更有趣。

㈨ c语言迷宫问题

问题出在MazePath内部的e是一个局部变量,并且随着while循环其内容不断变化。保存一个局部变量的地址是没有意义的,函数返回后就被清除。解决的办法是动态分配QElemType类型的对象,还有要注意一下,这个程序中分配的内存最后都没有被释放,这可是个不好的编程习惯。

改好了,你自己看看有没有错误。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef 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;i<=4;i++)
{//将四个方向能走通的位置的节点入队,并将其的parent指向上一个节点
p=NextPos(e,i);
p->parent=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 ");//找不到路径
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();
}

㈩ c语言迷宫的栈、队实现

#include<iostream>
#defineMAXSIZE100
usingnamespacestd;
structstack{
intiway;
intjway;
intdire;
};
stackmaze[MAXSIZE];
inttop;
intmap[14][28]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};voidfindway(intxS,intyS,intxE,intyE)
{
top=0;
maze[top].iway=xS;
maze[top].jway=yS;
map[xS][yS]=-1;
inti,j,di,find;
while(top>-1)
{
i=maze[top].iway;
j=maze[top].jway;
di=maze[top].dire;
if(i==xE&&j==yE)
{
cout<<"*********************************** ";
cout<<"path"<<":"<<endl;
cout<<"("<<maze[0].iway<<","<<maze[0].jway<<")";
for(intval=1;val<top+1;val++)
{
cout<<"->("<<maze[val].iway<<","<<maze[val].jway<<")";
if((val+1)%5==0)
cout<<endl;
}
cout<<endl;
cout<<"*********************************** ";
return;
}
find=0;
while(find==0&&di<4)
{
di++;
switch(di)
{
case(0):i=maze[top].iway;
j=maze[top].jway+1;
break;
case(1):i=maze[top].iway;
j=maze[top].jway-1;
break;
case(2):i=maze[top].iway+1;
j=maze[top].jway;
break;
case(3):i=maze[top].iway-1;
j=maze[top].jway;
break;
}
if(map[i][j]==0)
{
find=1;
}
}
if(find==1)
{
maze[top].dire=di;
top++;
maze[top].iway=i;
maze[top].jway=j;
maze[top].dire=-1;
map[i][j]=-1;
}
else
{
map[maze[top].iway][maze[top].jway]=0;
top--;
}
}
}
intmain()
{
for(inti=0;i<14;i++)//迷宫图形化输出
{
for(intj=0;j<28;j++)
{
if(map[i][j]==1)
cout<<"■";
elsecout<<"□";
}
cout<<endl;
}
intxStart,yStart,xEnd,yEnd;
cout<<"请输入迷宫起点坐标,用空格隔开(左上角坐标为(0,0)):";
cin>>xStart>>yStart;
cout<<"请输入迷宫终点坐标,用空格隔开(右上角坐标为(13,27)):";
cin>>xEnd>>yEnd;
findway(xStart,yStart,xEnd,yEnd);
return0;
}
满意请采纳!