迷宮問題
用棧求解
用佇列求解
棧:
#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;
}
執行結果:
相關文章
- 回溯法求迷宮問題
- POJ3984-迷宮問題
- POJ3984 迷宮問題【BFS】
- 洛谷 p1605 迷宮問題 詳解
- 迷宮問題——最短程式碼,不到70行
- 【dawn·資料結構】迷宮問題(C++)資料結構C++
- 解密迷宮問題:三種高效演算法Java實現,讓你輕鬆穿越未知迷宮解密演算法Java
- 用python深度優先遍歷解迷宮問題Python
- 使用A*演算法解迷宮最短路徑問題演算法
- (C++)資料結構實驗二——迷宮問題C++資料結構
- 回溯和遞迴實現迷宮問題(C語言)遞迴C語言
- c++迷宮問題回溯法遞迴演算法C++遞迴演算法
- 1744 迷宮
- 509迷宮
- 走迷宮
- [SDOI2012] 走迷宮 題解
- 【ybtoj】【BFS】【例題1】走迷宮
- 3090 走迷宮
- 3089 探索迷宮
- dfs深度優先搜尋解決迷宮類問題(遍歷)
- 簡單介紹Python迷宮生成和迷宮破解演算法Python演算法
- [省選聯考 2024] 迷宮守衛 題解
- 迷宮可行路徑數
- Python迷宮生成器Python
- PHP 生成迷宮路線PHP
- 迷宮的最短路徑
- 迷宮城堡(HDU-1269)
- 「IT運維迷宮」那些讓人頭疼的常見問題與破局之道運維
- PARL原始碼走讀——使用策略梯度演算法求解迷宮尋寶問題原始碼梯度演算法
- 創造你的專屬迷宮 《磚塊迷宮建造者》上架WeGameGAM
- 藍橋杯-走迷宮(BFS)
- PHP 解迷宮之 H 最小PHP
- 內容是超正統的迷宮RPG?PSP遊戲《迷宮旅人2》深度解析遊戲
- 自動走迷宮小遊戲~遊戲
- UOJ #810. 【UNR #7】位元迷宮
- 藍橋杯-迷宮(BFS+DFS)
- PHP 解迷宮之 G + H 最小PHP
- hdu 1728 逃離迷宮 搜尋