#demo1 #include<iostream> #include<ctime> #include<cstdlib> #include<queue> #include<cstdio> using namespace std; //生成迷宫 const int HEIGHT = 10; const int WIDTH = 10; bool isFound = false; int maze[HEIGHT][WIDTH]; void initialMaze() { maze[0][0] = 0;//入口 maze[HEIGHT - 1][WIDTH - 1] = 0;//出口 for (int i = 0; i < HEIGHT; i++)//用随机数0,1填充迷宫 { for (int j = 0; j < WIDTH; j++) { if (i == 0 && j == 0) continue; if (i == HEIGHT - 1 && j == WIDTH - 1) continue; maze[i][j] = rand() % 2; } } //展示生成的迷宫 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { cout << maze[i][j]; if (j != WIDTH - 1) { cout << " "; } else { cout << endl; } } } } // generating direction int Directory [ . 8 ] [ 2 ] = {{ 0 , . 1 }, { . 1 , . 1 }, { . 1 , 0 }, { . 1 , - . 1 }, { 0 , - . 1 }, {- 1 - 1 }, {- 1 , 0 }, {- 1 , 1 }}; // determines whether bounds BOOL isLeap ( int X, int Y) { return X> = 0 && X <WIDTH && Y> = 0Y && < HEIGHT; } // structure anywhere struct Point { int X; int Y; }; // structure for storing the path statement struct the dir { int X; int Y; int D; }; // statement for storing path queue queue <the dir> directoryQueue; // maze tracking the dir path [HEIGHT] [WIDTH]; // record labyrinth path int Output [WIDTH * HEIGHT] [ . 3 ]; void mazeTravel (Start Point, End Point, int Maze [HEIGHT] [WIDTH], int directory[8][2]) { dir element; //dir tmp; int i; int j; int d; int a; int b; element.x = start.x; element.y = start.y; element.d = -1; maze[start.x][start.y] = 2; directoryQueue.push(element); while (!directoryQueue.empty()) { element =directoryQueue.front (); the dir m = Element; directoryQueue.pop (); I = element.x; J = element.y; D = + element.d . 1 ; the while (D < . 8 ) { A = I + Directory [ D] [ 0 ]; B = J + Directory [D] [ . 1 ]; IF (a == B == end.y end.x && && Maze [a] [B] == 0 ) { // store information before a point to the path the dir TEMP = m; temp.d =D; path [A] [B] = TEMP; isFound = to true ; return ; } IF (isLeap (A, B) && Maze [A] [B] == 0 ) { // information stored before a point to the path the dir TEMP = m; temp.d = D; path [A] [B] = TEMP; Maze [A] [B] = 2 ; element.x = A; element.y = B; element.d = -1; directoryQueue.push(element); } d++; } } } void printPath(point start, point end) { if (!isFound) printf("The path is not found"); else { int step = 0; dir q; q.x = end.x; q.y = end.y; q.d = 886; while (q.x != start.x || q.y != start.y) { output[step][0] = q.x; output[step][1] = q.y; output[step][2] = q.d; int x = q.x; int y = q.y; q.x = path[q.x][q.y].x; q.y = path[x][q.y].y; q.d = path[x][y].d; step++; } output[step][0] = q.x; output[step][1] = q.y; output[step][2] = q.d; printf("The path is as follows: \n"); for (int i = step; i >= 0; i--) { printf("(%d,%d)", output[i][0], output[i][1]); if (i != 0) printf("->"); } printf("\n"); } } int main() { srand(time(0)); initialMaze(); point a, b; a.x = 0; a.y = 0; b.x = HEIGHT - 1; b.y = WIDTH - 1; mazeTravel(a, b, maze, directory); printPath(a, b); return 0; }
Export
0 1 1 0 1 0 0 1 1 0 0 0 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 0 0 0 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 0 1 0 1 1 1 1 0 0 The path is as follows: (0,0)->(1,1)->(1,2)->(2,3)->(3,4)->(4,5)->(5,6)->(6,6)->(7,7)->(8,8)->(9,9) Program ended with exit code: 0
demo2
#demo2 #include <iostream> #include <vector> #include <queue> using namespace std; using std::vector; struct point { int x; int y; int step; point(int _x, int _y, int _step) :x(_x), y(_y), step(_step) {} point(int _x, int _y) :x(_x), y(_y), step(0){} point(){} bool operator==(const point& other)const { return x == other.x&&y == other.y; } }; int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step); int main() { vector<vector<int>> path = { { 1, 1, 1, 1, 1 }, { 1, 0, 1, 0, 1 }, { 1, 0, 0, 1, 1 }, { 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1 } }; vector<vector<point>> mp(5, vector<point>(5)); point src(0,0); point des(3,3); int step = 0; cout << minSteps_BFS(path, mp, src, des, step) << endl; cout << "具体路径如下:" << endl; //vector<point> res; while (!(mp[des.x][des.y] == src)) { cout << des.x << " " << des.y << endl; des = mp[des.x][des.y]; } cout << des.x << " " << des.y << endl; return 0; } int minSteps_BFS(const vector<vector<int>>& path, vector<vector<point>>& mp, point src, point des, int step) { const unsigned long n = path.size(); const unsigned long m = path[0].size(); const int dx[4] = { 0, 0, -1, 1 }; const int dy[4] = { 1, -1, 0, 0 }; vector<vector<bool>> flag(n, vector<bool>(m, false)); flag[src.x][src.y] = true; queue<point> que; que.push(src); while (!que.empty()) { point p = que.front(); for (int i = 0; i < 4; ++i) { if (p.x + dx[i] < 0 || p.x + dx[i] >= n || p.y + dy[i] < 0 || p.y + dy[i] >= m) continue; if (path[p.x + dx[i]][p.y + dy[i]] == 1 && !flag[p.x + dx[i]][p.y + dy[i]]) { flag[p.x + dx[i]][p.y + dy[i]] = true; que.push(point(p.x + dx[i], p.y + dy[i], p.step + 1)); mp[p.x + dx[i]][p.y + dy[i]]= p; if (point(p.x + dx[i], p.y + dy[i], p.step + 1) == des) { return p.step + 1; } } } que.pop(); } return -1; }
Export
6 following this path: . 3 . 3 . 3 2 . 3 . 1 . 3 0 2 0 . 1 0 Program ended with Exit code: 0
Reference:
https://www.cnblogs.com/xiugeng/p/9687354.html
https://blog.csdn.net/weixin_41106545/article/details/83211418