#標頭檔案
#include
#include
#include
#include
#include
"maze.h"
#define max 100
typedef
struct position datatype;
typedef
struct stack
stack;
void
stackinit
(stack* s)
;void
stackpush
(stack* s, datatype data)
;void
stackpop
(stack* s)
;datatype getstacktop
(stack*s)
;int
getstacksize
(stack*s)
;int
stackempty
(stack *s)
;
#pragma once
#include
#include
#include
#define row 6
#define col 6
typedef
struct maze
maze;
typedef
struct position
position;
// 初始化迷宮
void
initmaze
(maze* m,
int map[row]
[col]);
// 列印迷宮
void
printmaze
(maze* m)
;// 檢測是否為有效入口
intisvalidenter
(maze* m, position enter)
;// pos 必須在地圖中 pos位置如果是1
intispass
(maze* m, position pos)
;// 檢測是否在出口的位置
inti***it
(position pos, position enter)
;// 走迷宮
void
passmaze
(maze* m, position enter)
;
#操作函式
#include
"stack.h"
void
stackinit
(stack* s)
void
stackpush
(stack* s, datatype data)
(s->top)++;
s->stack[s-
>top]
= data;
}void
stackpop
(stack *s)
(s->top)--;
}datatype getstacktop (stack*s)
return s-
>stack[s-
>top];}
intgetstacksize
(stack*s)
intstackempty
(stack *s)
else
}
#include
"maze.h"
#include
"stack.h"
void
initmaze
(maze * m,
int map[row]
[col])}
}void
printmaze
(maze* m)
printf
("\n");
}}intisvalidenter
(maze* m, position enter)
else
return0;
}// pos 必須在地圖中 pos位置如果是1
intispass
(maze* m, position pos)
else
if(m-
>_map[pos._x]
[pos._y]!=1
)else
return1;
}// 檢測是否在出口的位置
inti***it
(position pos, position enter)
return0;
}void
passmaze
(maze* m, position enter)
stackinit
(&s)
;stackpush
(&s, enter)
;while(!
stackempty
(&s)
) m-
>_map[cur._x]
[cur._y]=2
; next = cur;
next._x = next._x -1;
if(ispass
(m, next)
) next._y = next._y -1;
if(ispass
(m, next)
) next._y = next._y +1;
if(ispass
(m, next)
) next._x = next._x +1;
if(ispass
(m, next)
)//cur 四個方向都走不通;
m->_map[cur._x]
[cur._y]=3
;stackpop
(&s);}
}
#驗證操作
#include
"maze.h"
#include
"stack.h"
intmain()
,,,,
,}; maze m;
initmaze
(&m, map)
; position enter;
enter._x =5;
enter._y =2;
printmaze
(&m)
;passmaze
(&m, enter)
;printf
("\n");
printmaze
(&m)
;system
("pause");
return0;
}
求解迷宮問題
求解迷宮問題 include using namespace std const int m 6 const int n 8 int maze m 2 n 2 m n大小的迷宮,0可前進,1通行受阻。並且在迷宮的周圍鑲上邊框 int mark m 2 n 2 儲存訪問標記,0未訪問,1已訪問 int...
迷宮問題求解
對於迷宮問題的求解,我們最後輸出的是迷宮的路徑,故符合佇列的先進先出特性,所以我們採用佇列的方式對迷宮進行求解 首先我們將建立乙個二維陣列 自定義初始化迷宮 7 7 int arr n n 二維陣列邊界用0填充,使之區域封閉,在實現過程 現陣列越界問題 接下來是 include include de...
問題 A 迷宮求解問題
時間限制 10 sec 記憶體限制 2048 mb 提交 574 解決 306 提交 狀態 討論版 用乙個m n的矩陣表示迷宮,0和1分別表示迷宮中的通路和障礙。設計乙個程式,對給定的迷宮,求出找到的第一條從入口到出口的通路,或得到沒有通路的結論。我們指定 1 迷宮的入口為矩陣的左上角 1,1 迷宮...