當前位置:首頁 » 編程語言 » 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;
}
滿意請採納!