标签:++ push signed space none with iostream ini cto
#demo1
#include
#include
#include
#include
#includeusing 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 //用随机数0,1填充迷宫
{
for (int j = 0; 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 )
{
for (int j = 0; j )
{
cout maze[i][j];
if (j != WIDTH - 1)
{
cout " ";
}
else
{
cout endl;
}
}
}
}
//生成方向
int directory[8][2] = { {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1} };
//判断是否越界
bool isLeap(int x, int y)
{
return x >= 0 && x = 0 && y HEIGHT;
}
//任意位置的结构体
struct point {
int x;
int y;
};
//声明用于存储路径的结构体
struct dir
{
int x;
int y;
int d;
};
//声明用于存储路径的队列
queue
directoryQueue;
//迷宫循迹
dir path[HEIGHT][WIDTH];//记录迷宫的路径
int output[HEIGHT*WIDTH][3];
void mazeTravel(point start, point end, 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();
dir m = element;
directoryQueue.pop();
i = element.x;
j = element.y;
d = element.d + 1;
while (d 8)
{
a = i + directory[d][0];
b = j + directory[d][1];
if (a == end.x&&b == end.y&&maze[a][b] == 0)
{
//储存前一个点的信息至path
dir temp = m;
temp.d = d;
path[a][b] = temp;
isFound = true;
return;
}
if (isLeap(a, b)&&maze[a][b]==0)
{
//储存前一个点的信息至path
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;
}
输出
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
#include
#include 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 vectorint>>& path, vector>& mp, point src, point des, int step);
int main()
{
vectorint>> 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> mp(5, vector(5));
point src(0,0);
point des(3,3);
int step = 0;
cout endl;
cout "具体路径如下:" endl;
//vector res;
while (!(mp[des.x][des.y] == src))
{
cout " " endl;
des = mp[des.x][des.y];
}
cout " " endl;
return 0;
}
int minSteps_BFS(const vectorint>>& path, vector>& 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 };
vectorbool>> flag(n, vectorbool>(m, false));
flag[src.x][src.y] = true;
queue 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;
}
输出
6
具体路径如下:
3 3
3 2
3 1
3 0
2 0
1 0
Program ended with exit code: 0
参考:
https://www.cnblogs.com/xiugeng/p/9687354.html
https://blog.csdn.net/weixin_41106545/article/details/83211418
c++ 珊格迷宫问题
标签:++ push signed space none with iostream ini cto
原文地址:https://www.cnblogs.com/sea-stream/p/11100374.html