迷宮問題

monicaaaaan發表於2020-11-01

用棧求解
用佇列求解

棧:

#include <stdio.h>
#include <stdlib.h>

#define M 8
#define N 8
#define Max 100

int mg[M+2][N+2]=
{
	{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},
	{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,0,1},
	{1,0,1,1,1,0,0,0,0,1},{1,0,0,0,1,0,0,0,0,1},
	{1,0,1,0,0,0,1,0,0,1},{1,0,1,1,1,0,1,1,0,1},
	{1,1,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}
};

typedef struct
{
	int i;
	int j;
	int di;   //下一相鄰可走方位的方位號
}Box;

typedef struct
{
	Box data[Max];
	int top;
}Sqtype;

//初始化
void init(Sqtype *&s)
{
	s = (Sqtype *)malloc(sizeof(Sqtype));
	s->top = -1;
}

//進棧
void push(Sqtype *&s, Box e)
{
	if(s->top == Max-1)
	{
		printf("棧滿!\n");
		return;
	}
	s->top++;
	s->data[s->top] = e;
}

//判斷是否為空
bool stackempty(Sqtype *s)
{
	return (s->top == -1);
}

//獲取棧頂元素
void gettop(Sqtype *&s, Box &e)
{
	e = s->data[s->top];
}

//出棧
void pop(Sqtype *&s, Box &e)
{
	e = s->data[s->top];
	s->top--;
}

//銷燬
void des(Sqtype *&s)
{
	free(s);
}


int mgpath(int xi, int yi, int xe, int ye)
{
	Box path[Max], e;
	int i, j, i1, j1, k, di;
	int find;
	Sqtype *s;
	init(s);

	e.i = xi;
	e.j = yi;
	e.di = -1;
	push(s,e);
	mg[xi][yi] = -1;

	while(!stackempty(s))
	{
		gettop(s,e);
		i = e.i; j = e.j; di = e.di;
		if(i==xe && j==ye)   //走到出口
		{
			printf("迷宮路徑如下:\n");
			k=0;
			while(!stackempty(s))
			{
				pop(s,e);
				path[k++] = e;
			}
			while(k>=1)
			{
				k--;
				printf("\t(%d,%d)",path[k].i,path[k].j);
				if((k+2)%5 == 0)
					printf("\n");
			}
			printf("\n");
			des(s);
			return 1;
		}
		find = 0;
		while(!find && di<4)
		{
			di++;
			switch(di)   //試探可走方向
			{
			case 0:
				i1 = i-1;
				j1 = j;
				break;
			case 1:
				i1 = i+1;
				j1 = j;
				break;
			case 2:
				i1 = i;
				j1 = j-1;
				break;
			case 3:
				i1 = i;
				j1 = j+1;
				break;
			}
			if(mg[i1][j1] == 0)
				find = 1;
		}
		if(find)
		{
			s->data[s->top].di = di;
			e.i = i1;
			e.j = j1;
			e.di = -1;
			push(s,e);
			mg[i1][j1] = -1;
		}
		else
		{
			pop(s,e);
			mg[e.i][e.j] = 0;
		}
	}
	des(s);
	return 0;
}

int main(void)
{
	if(mgpath(1,1,M,N))
		printf("找到路徑!\n");
	else
		printf("沒有找到路徑!\n");
	return 0;
}

佇列:

#include <stdio.h>
#include <stdlib.h>

#define M 8
#define N 8
#define Max 100

int mg[M+2][N+2]=
{
	{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},
	{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,0,1},
	{1,0,1,1,1,0,0,0,0,1},{1,0,0,0,1,0,0,0,0,1},
	{1,0,1,0,0,0,1,0,0,1},{1,0,1,1,1,0,1,1,0,1},
	{1,1,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}
};

//方塊型別
typedef struct
{
	int i,j;
	int pre;   //上一個方塊在佇列中的下標
}Box;

//順序表型別
typedef struct
{
	Box data[Max];
	int front,rear;
}Qutype;

//進隊
void enqueue(Qutype *&qu, Box e)
{
	if(qu->rear == Max-1)
	{
		printf("隊滿!\n");
		return;
	}
	qu->rear++;
	qu->data[qu->rear] = e;
}

//判斷隊是否為空
bool empty(Qutype *qu)
{
	return (qu->rear == qu->front);
}

//出隊
void dequeue(Qutype *&qu, Box &e)
{
	qu->front++;
	e = qu->data[qu->front];
}

void des(Qutype *&qu)
{
	free(qu);
}

//列印輸出
void print(Qutype *qu, int front)
{
	int k=front, j, ns=0;
	printf("\n");
	do
	{
		j = k;
		k = qu->data[k].pre;
		qu->data[j].pre = -1;
	}while(k != 0);
	printf("迷宮路徑如下:\n");
	k = 0;
	while(k < Max)
	{
		if(qu->data[k].pre == -1)
		{
			ns++;
			printf("\t(%d,%d)",qu->data[k].i, qu->data[k].j);
			if(ns%5 == 0)
				printf("\n");
		}
		k++;
	}
	printf("\n");
}

//迷宮找路徑
int mgpath(int xi, int yi, int xe, int ye)
{
	Box e;
	int i, j, di, i1, j1;
	Qutype *qu;
	qu = (Qutype *)malloc(sizeof(Qutype));
	qu->front = qu->rear =-1;    //初始化
	e.i = xi;
	e.j = yi;
	e.pre = -1;
	enqueue(qu,e);
	mg[xi][yi] = -1;
	while(!empty(qu))
	{
		dequeue(qu,e);   //因為不是環形佇列,該出隊元素仍在佇列中
		i = e.i;
		j = e.j;
		if(i==xe && j==ye)   //找到出口
		{
			print(qu,qu->front); //輸出路徑
			des(qu);
			return 1;
		}
		
		for(di=0; di<4; di++)  //從四個方位找可走路徑
		{
			switch(di)
			{
			case 0:
				i1 = i-1;
				j1 = j;
				break;
			case 1:
				i1 = i+1;
				j1 = j;
				break;
			case 2:
				i1 = i;
				j1 = j-1;
				break;
			case 3:
				i1 = i;
				j1 = j+1;
				break;
			}
			if(mg[i1][j1] == 0) //若可走
			{
				e.i = i1;
				e.j = j1;
				e.pre = qu->front;
				enqueue(qu,e);
				mg[i1][j1] = -1;
			}
		}
	}
	des(qu);
	return 0;
}

int main(void)
{
	if(mgpath(1,1,M,N))
		printf("找到路徑!\n");
	else
		printf("沒有找到路徑!\n");
	return 0;
}

執行結果:
執行結果

相關文章