運用並查集自動生成迷宮地圖,並運用佇列和棧尋找迷宮通路並列印出來
#incaighfalrtlude
#include
#include
#include
#include
using namespace std;
using std::queue;
using std::stack;
typedef struct point
point;
queue mqueue;
stack mstack;
point pos, pos1;
int m, n;//迷宮行(tm-1)/2和列(tn-1)/2
int tm, tn;//實際作圖
int x, y, tx1, tx2, ty1, ty2;//點座標
int d;
int s[10000000];
int maze[1000][1000], mark[1000][1000];//最大迷宮
int sign[4][2] = ,,, };//上下左右四個方向 0上 1下 2上 3下
point start;
int find_x(int x);
void unionsets(int node1, int node2);
void init();
int getadd(int x, int y);
void foundpath();
void fixmaze();
int connected(int node1, int node2);
void findpath();
void changemaze();
int main()
} for (int i = 1; i < tm - 1; i += 2)
srand(time(null));
foundpath();
fixmaze();
cout << "迷宮全圖:" << endl;
for (int i = 0; i < tm; i++)
cout << endl;
} findpath();
changemaze();
cout << "找到的通路:「..」表示:" << endl;
for (int i = 0; i < tm; i++)
cout << endl;
} system("pause");
return 0;
}int connected(int node1, int node2)
int find_x(int x)
;void unionsets(int node1, int node2)
};int getadd(int x, int y)
;void init()
;void foundpath()
while (maze[x][y] == 0);
d = x % 2;
if (d == 0) }
else if (d == 1) }
}}void fixmaze()
} }for (int i = 1; i < tm - 1; i++)
if (maze[i - 1][j - 1] == 1 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 1 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 1 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 1 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 1 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == www.cppcns.com0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && 程式設計客棧maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 1 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 1 && maze[i + 1][j] == 0 && maze[i + 1www.cppcns.com][j + 1] == 0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 1 && maze[i + 1][j + 1] == 0)
if (maze[i - 1][j - 1] == 0 && maze[i - 1][j] == 0 && maze[i - 1][j + 1] == 0 && maze[i][j - 1] == 0 && maze[i][j] == 0 && maze[i][j + 1] == 0 && maze[i + 1][j - 1] == 0 && maze[i + 1][j] == 0 && maze[i + 1][j + 1] == 1)
}}//區域性優化,防止出現大面積通路
}void findpath()
} }if (flag) break;
mstack.push(mqueue.front());
if (!mqueue.empty())
mqueue.pop(); }}
void changemaze()
else if (!mstack.empty())
mstack.pop();
}}本文標題: c++自動生成迷宮遊戲
本文位址:
迷宮自動生成程式
這學期開始時本來打算寫個自動生成迷宮的程式。但當時水平所限,寫不出來。假期這兩天把這個想法付諸實施,現在想想這個程式挺有意思的。程式和道理都非常簡單,有些類似於走迷宮。思路是這樣 1.首先假設迷宮場地是充滿牆壁沒有道路的。我們的工作其實就是把迷宮 挖 出來。不妨把開始時的迷宮看成一些小的 房間 每個...
迷宮求解(自動生成)
主函式測試例項 棧的順序儲存表示 define stack init size 100 儲存空間初始分配量 define stackincerment 10 儲存空間分配增量 typedef bool status typedef struct sqstack 基本操作的函式原型說明 status ...
C 迷宮遊戲
includeusing namespace std 點為2表示迷宮圖為 點為0表示迷宮圖為 int migo 9 9 迷宮圖 int starti 1,startj 1 出發點 int endi 7,endj 7 出口 遞迴演算法 演算法中,if語句即為遞迴函式的出口,當到達迷宮出口時,輸出 若未...