實驗三 棧和隊列的綜合應用迷宮問題

迷宮問題。假設迷宮由m行n列構成,有一個入口和一個出口,入口座標爲(1,1),出口座標爲(m,n),試設計並驗證以下算法:找出一條從入口通往出口的路徑,或報告一個“無法通過”的信息。
(1) 用C語言實現順序存儲結構上隊列的基本操作,然後利用該隊列的基本操作找出迷宮的一條最短路徑。
(2) 設計一個二維數組MAZE[m+2][n+2]表示迷宮,數組元素爲0表示該位置可以通過,數組元素爲1表示該位置不可以通行。MAZE[1][1]、MAZE[m][n]分別爲迷宮的入口和出口。
(3) 輸入迷宮的大小m行和n列,動態生成二維數組;由隨機數產生0或1,建立迷宮,注意m*n的迷宮需要進行擴展,擴展部分的元素設置爲1,相當於在迷宮周圍布上一圈不準通過的牆。
(4) 要求輸出模擬迷宮的二維數組;若存在最短路經,則由出口回溯到入口(出隊列並利用棧實現),再打印從入口到出口的這條路徑,例如(1,1),……,(i,j),……,(m,n);若沒有路徑,則打印“No path!”。
(5) 迷宮的任一位置(i,j)上均有八個可以移動的方向,用二維數組Direction存放八個方向上的位置偏移量。
Direction[8][2]={{0,1},{1,1},{0,-1},{-1,-1},{1,1},{0,-1},{-1,-1},{0,1}};
(6) 爲避免出現原地踏步的情況爲了標誌已經通過的位置,採用一個標誌數組MARK[m+2][n+2],初值均爲0,在尋找路徑的過程中,若通過了位置(i,j),則將MARK[i][j]置爲1。
(7) 爲了記錄查找過程中到達位置(i,j)及首次到達(i,j)的前一位置(i_pre,j_pre),需要記住前一位置(i_pre,j_pre)在隊列中的序號pre,即隊列中數據元素應該是一個三元組(i,j,pre)。
(8) 搜索過程簡單描述如下:將入口MAZE[1][1]作爲第一個出發點,依次在八個方向上搜索可通行的位置,將可通行位置(i,j,pre)入隊,形成第一層新的出發點,然後依次出隊,即對第一層中各個位置分別搜索它所在八個方向上的可通行位置,形成第二層新的出發點,…,如此進行下去,直至達到出口MAZE[m][n]或者迷宮所有位置都搜索完畢爲止。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
#include<ctype.h>
#include<windows.h>
#include<time.h>

#define TURE 1
#define FLASE 0
#define ERROR 0
#define OVERFLOW -2
#define OK 1
#define INFLASEBLE -1
#define MaxSize 2500
#define Size 50
typedef int ElemType;
typedef int Status;
typedef struct {
	ElemType i;
	ElemType j;
	ElemType pre;
} LNode;
typedef struct {
	LNode data[MaxSize];
	ElemType front,rear;
} SqQueue;
Status InitQueue(SqQueue *Q) { //建立隊列,使front,rear都指向0
	Q->front=0;
	Q->rear=0;
	return OK;
}
Status EnQueue(SqQueue *Q,ElemType x,ElemType y) { //座標進隊列
	(Q->data[Q->rear]).i=x;
	(Q->data[Q->rear]).j=y;
	(Q->data[Q->rear]).pre=Q->front;
	Q->rear++;
	return OK;
}
Status QueueEmpty(SqQueue Q) { //判斷是否爲空,空爲假,不空爲真
	if(Q.front==Q.rear) return FLASE;
	else return TURE;
}
//以上爲隊列
typedef struct {
	ElemType data[Size];
	ElemType top;
} SqStack;
Status InitStack(SqStack *S) { //使top的指針指向-1
	S->top=-1;
	return OK;
}
Status Push(SqStack *S,ElemType e) { //使棧頂的元素爲e
	S->top++;
	S->data[S->top]=e;
	return OK;
}
ElemType Pop(SqStack *S,ElemType *e) { //使棧頂的元素出棧,且返回值爲e
	*e=S->data[S->top];
	S->top--;
	return *e;
}
//以上爲棧
Status PrintMigong(ElemType (*MAZE)[Size],ElemType m,ElemType n) {//打印迷宮
	ElemType i,j;
	printf("迷宮如下:\n");
	for(j=0; j<n+2; j++) {
		for(i=0; i<m+2; i++) {
			if(j==0) printf("%d ",i);
			else if(i==0) printf("%d",-j);
			else if(MAZE[i][j]==0)
				printf("□",MAZE[i][j]);
			else printf("■",MAZE[i][j]);
		}
		printf("\n");
	}
	return OK;
}
Status CreatMigong(ElemType (*MAZE)[Size],ElemType (*MARK)[Size],ElemType m,ElemType n) { //構造迷宮
	srand(time(NULL));
	ElemType i,j;
	for(i=0; i<m+2; i++)
		for(j=0; j<n+2; j++) {
			if(i==0||i==m+1||j==0||j==n+1)MAZE[i][j]=1;//建立圍牆
			else MAZE[i][j]=rand()%2;//給迷宮的內部賦予隨機數0或1
		}
	MAZE[1][1]=0,MAZE[m][n]=0;
	MARK[1][1]=1;//標記第一個點已經走
	return OK;
}
Status PrintPath(SqStack S,SqQueue Q,ElemType m,ElemType n) {//顯示路徑
	ElemType l,e;
	for(l=S.top; l>=0; l--) {
		Pop(&S,&e);
		printf("(%-2d,%-2d) -> ",Q.data[e].i,-Q.data[e].j);
	}
	printf("(%-2d,%-2d)\n",m,-n);
	return OK;
}
Status Sloution() {
	ElemType k,p,flag=0;
	ElemType nexti=1,nextj=1;
	//建立了隊列Q,棧S
	SqQueue Q;
	SqStack S;
	InitQueue(&Q);
	InitStack(&S);
	ElemType Direction[8][2]= {{1,1},{0,1},{1,0},{-1,1},{1,-1},{0,-1},{-1,0},{-1,-1}};
	//用二維數組Direction存放八個方向上的位置偏移量
	ElemType MAZE[Size][Size]= {0}; //建立一個數組用於容納迷宮
	ElemType MARK[Size][Size]= {0}; //建立一個標記是否走過的數組
	ElemType m,n;
	printf("請輸入迷宮的行和列:");
	scanf("%d%d",&m,&n);//輸入迷宮的行和列
	CreatMigong(MAZE,MARK,m,n);
	//打印迷宮
	PrintMigong(MAZE,m,n);
	//把起始點進入隊列
	EnQueue(&Q,1,1);
	while(QueueEmpty(Q)) {
		for(k=0; k<8; k++) {//判斷八個方向是否有可以走的,有就壓進隊列
			nexti=Q.data[Q.front].i+Direction[k][0];
			nextj=Q.data[Q.front].j+Direction[k][1];
			if(MAZE[nexti][nextj]||MARK[nexti][nextj]);
			else {
				MARK[nexti][nextj]=1;
				EnQueue(&Q,nexti,nextj);
				if(nexti==m&&nextj==n) { //一旦發現了出口,就退出循環
					flag=1;
					break;
				}
			}
		}
		Q.front++;//每判斷完之後,頭指針要加一
		if(flag==1) {//如果找到出口就壓進棧中
			p=Q.rear-1;
			while(p) {
				p=Q.data[p].pre;
				Push(&S,p) ;
			}
			break;
		}
	}
	//輸出迷宮路徑
	printf("走出迷宮最短路經如下:\n");
	if(QueueEmpty(Q)) { //如果不是因爲隊列退出循環的,就把棧中的元素輸出
		PrintPath(S,Q,m,n);
	} else printf("No path!\n");
	return OK;
}
int main() {
	while(1) {
		Sloution();
	}
	return 0;
}

2019年11月7日 11:55:58改版

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
#include<ctype.h>
#include<windows.h>
#include<time.h>

#define TURE 1
#define FLASE 0
#define ERROR 0
#define OVERFLOW -2
#define OK 1
#define INFLASEBLE -1
#define MaxSize 2510

typedef int ElemType;
typedef int Status;
typedef struct {
	ElemType i;
	ElemType j;
	ElemType pre;
} LNode;
typedef struct {
	ElemType m;
	ElemType n;
	ElemType **MAZE;
	ElemType **MARK;
} M;
typedef struct {
	LNode data[MaxSize];
	LNode *front,*rear;
} SqQueue;
Status InitQueue(SqQueue *Q) { //初始化隊列,使front,rear都指向基準
	Q->front=Q->data;
	Q->rear=Q->data;
	Q->front->pre=0;
	return OK;
}
Status EnQueue(SqQueue *Q,ElemType x,ElemType y) {//座標進隊列
	Q->rear->i=x;
	Q->rear->j=y;
	Q->rear++;
	Q->rear->pre=(Q->rear-1)->pre+1;
	return OK;
}
Status QueueEmpty(SqQueue Q) { //判斷隊列是否爲空
	if(Q.front==Q.rear) return FLASE;
	else return TURE;
}
//以上爲隊列
typedef struct {
	LNode data[MaxSize];
	LNode *top,*base;
} SqStack;
Status InitStack(SqStack *S) {//初始化 
	S->top=NULL;
	S->base=S->data;
	return OK;
}
Status emptyStack(SqStack S) {//初始化 
	if(S.top==S.data) return FLASE;
	else return TURE;
}
Status Push(SqStack *S,LNode e) {//使棧頂的元素結點爲e
	if(S->top==NULL)
		S->top=S->data;
	else S->top++;
	S->top=&e;
	return OK;
}
Status Pop(SqStack *S,LNode *e) { //使棧頂的元素出棧,且返回值爲e
	e=S->top;
	S->top--;
	return OK;
}
//以上爲棧
Status CreatMigong(M *mg) ; //構造迷宮
Status PrintMigong(M *mg) ;//打印迷宮
Status Sloution(SqQueue *Q,SqStack *S,M *mg) ;//找最短路徑
Status PrintPath(SqStack S,ElemType n) ;//打印最短路徑
Status seek(SqQueue *Q,SqStack *S,M *mg) ;//找最短路徑非遞歸寫法
int main() {//主函數
	M mg;
	SqQueue Q;
	SqStack S;
	while(1) {
		CreatMigong(&mg);
		PrintMigong(&mg);
		Sloution(&Q,&S,&mg);
		PrintPath(S,(Q.rear-1)->pre);
	}
	return 0;
}
Status CreatMigong(M *mg) { //構造迷宮
	srand(time(NULL));
	ElemType i,j;
	do {
		printf("請輸入迷宮的行和列:");
		scanf("%d%d",&(mg->m),&(mg->n));
	} while(mg->m<1&&mg->n<1||mg->m>=50||mg->n>=50);
	mg->MAZE = (ElemType**)malloc((mg->m+2)*sizeof(ElemType*));
	for(int i=0; i<(mg->m+2); i++)
		mg->MAZE[i] = (ElemType*)malloc((mg->n+2)*sizeof(ElemType));
	for(i=0; i<mg->m+2; i++)
		for(j=0; j<mg->n+2; j++) {
			if(i==0||i==mg->m+1||j==0||j==mg->n+1) mg->MAZE[i][j]=1;//建立圍牆
			else mg->MAZE[i][j]=rand()%2;//給迷宮的內部賦予隨機數0或1
		}
	mg->MAZE[1][1]=0,mg->MAZE[mg->m][mg->n]=0;
	return OK;
}
Status PrintMigong(M *mg) {//打印迷宮
	ElemType i,j;
	printf("迷宮如下:\n");
	for(j=0; j<mg->n+2; j++) {
		for(i=0; i<mg->m+2; i++) {
			if(i==0) printf("%3d",-j);
			else if(j==0) printf("%-2d",i);
			else if(mg->MAZE[i][j]==0) printf("□",mg->MAZE[i][j]);
			else printf("■",mg->MAZE[i][j]);
		}
		printf("\n");
	}
	return OK;
}
Status Sloution(SqQueue *Q,SqStack *S,M *mg) {//找最短路徑
	ElemType i,flag=0;
	ElemType nexti=1,nextj=1;
	LNode *t;
	InitQueue(Q);
	InitStack(S);
	ElemType Direction[8][2]= {{1,1},{0,1},{1,0},{-1,1},{1,-1},{0,-1},{-1,0},{-1,-1}};
	mg->MARK = (ElemType**)malloc((mg->m+2)*sizeof(ElemType*));
	for(int i=0; i<(mg->m+2); i++)
		mg->MARK[i] = (ElemType*)calloc((mg->n+2),sizeof(ElemType));
	mg->MARK[1][1]=1;
	EnQueue(Q,1,1);
	while(QueueEmpty(*Q)) {
		for(i=0; i<8; i++) {
			nexti=Q->rear->i+Direction[i][0],nextj=Q->rear->j+Direction[i][1];
			if(mg->MAZE[nexti][nextj]||mg->MARK[nexti][nextj]);
			else {
				mg->MARK[nexti][nextj]=1;
				EnQueue(Q,nexti,nextj);
				if(nexti==mg->m&&nextj==mg->n) {
					flag=1;
					break;
				}
			}
		}
		if(flag==1) {
			t=Q->rear-1;
			while(t->pre) {
				Push(S,*t);
				t--;
			}
			break;
		}
	}
	return OK;
}
Status seek(SqQueue *Q,SqStack *S,M *mg) {//找最短路徑非遞歸寫法
	ElemType i;
	ElemType nexti=1,nextj=1;
	ElemType Direction[8][2]= {{1,1},{0,1},{1,0},{-1,1},{1,-1},{0,-1},{-1,0},{-1,-1}};
	while(QueueEmpty(*Q)) {
		for(i=0; i<8; i++) {
			nexti=Q->rear->i+Direction[i][0],nextj=Q->rear->j+Direction[i][1];
			if(mg->MAZE[nexti][nextj]||mg->MARK[nexti][nextj]);
			else {
				mg->MARK[nexti][nextj]=1;
				EnQueue(Q,nexti,nextj);
				Push(S,*(Q->rear-1));
				if(nexti==mg->m&&nextj==mg->n) {
					Push(S,*(Q->rear-1));
					return OK;
				}
			}
		}
		if(QueueEmpty(*Q)) {
			Pop(S,*(Q->rear-1));
			seek(Q,S,mg);
		} else return ERROR;
	}
}
Status PrintPath(SqStack S,ElemType n) {//打印最短路徑
	ElemType i;
	LNode e;
	if(emptyStack(S)) printf("No path!\n");
	else {
		printf("走出迷宮最短路經如下:\n");
		for(i=0; i<n-1; i++) {
			Pop(&S,&e);
			printf("(%-2d,%-2d) -> ",e.i,-e.j);
		}
		Pop(&S,&e);
		printf("(%-2d,%-2d)",e.i,-e.j);
	}
	return OK;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章