c ++迷宫求解器(找到每个解决方案)堆栈溢出
c++ maze solver (find every solution) stack overflow
我有一个随机生成迷宫的任务("weird" 也是有效的),然后我必须找到迷宫的所有解决方案(一个解决方案是从第一行或第一列找到一条路到最后一行或最后一列),还要确定最短的那个。我这里有这段代码,但是当我 运行 它时,有一半时间它给出了一个好的答案,另一半时间它只是退出而没有任何错误弹出窗口或警告。我想原因是堆栈溢出。此外,在作业中它说我们必须在 20x20 的迷宫上解决这个问题。如果它在 5x5 上溢出,你能想象 20x20 吗?无论如何,我们将不胜感激任何帮助。请忽略评论,它实际上是分配匈牙利评论的一部分。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct vector2d {
int column, row;
};
const int N = 5; // sorok száma
const int M = 5; // oszlopok száma
bool ut[N][M];
int count = 0;
int currentLength = 0, minLength = 99999;
void initMaze(); // generál egy labirintust
void printMaze(); // kiiratja a labirintust
int exits(); // kijáratok számát adja vissza
int entrances(); // bejáratok számát adja vissza
bool findPath(int, int); // útkereső eljárás
int main() {
initMaze();
printMaze();
if (exits() > 0 && entrances() > 0) {
// bactrack
for (int i = 0; i < M; i++) // vegigjarjuk az elso sor osszes "utjat" es megkeressuk az onnan indulo megoldasokat
if (ut[0][i])
if (findPath(0, i)) {
count++;
if (currentLength < minLength)
minLength = currentLength;
currentLength = 0;
}
for (int i = 1; i < M; i++)
if (ut[i][0])
if (findPath(i, 0)) {
count++;
if (currentLength < minLength)
minLength = currentLength;
currentLength = 0;
}
} else {
cout << "Maze has no entrances or exits" << endl;
}
if (count > 0) {
cout << count << " solutions for the labyrinth, shortest one is " << minLength << " steps long" << endl;
} else {
cout << "Labyrinth has no solution" << endl;
}
system("pause");
return 0;
}
int exits() {
int count = 0;
for (int i = 1; i < M - 1; i++)
if (ut[N - 1][i])
count++;
for (int i = 1; i < N - 1; i++)
if (ut[i][M - 1])
count++;
return count;
}
int entrances() {
int count = 0;
for (int i = 1; i < M - 1; i++)
if (ut[0][i])
count++;
for (int i = 1; i < N - 1; i++)
if (ut[i][0])
count++;
return count;
}
void initMaze() {
srand(time(NULL));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
if (rand() % 3 == 0) // 3-ból egyszer legyen fal
ut[i][j] = false;
else
ut[i][j] = true;
}
}
void printMaze() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (ut[i][j])
cout << " - ";
else
cout << " | ";
}
cout << endl;
}
}
bool findPath(int row, int column) {
if (column < 0 || column > N - 1 || row < 0 || row > M - 1) return false;
if ((column == M - 1 || row == N - 1) && ut[row][column]) return true;
if (!ut[row][column]) return false;
currentLength++;
if (findPath(column + 1, row)) return true;
if (findPath(column, row + 1)) return true;
if (findPath(column - 1, row)) return true;
if (findPath(column, row - 1)) return true;
currentLength--;
return false;
}
你有递归调用
findPath(column + 1, row)
这个函数会在一段时间后进行递归调用
findPath(column - 1, row)
这两个调用的问题是会返回到调用的位置。
例如,如果使用坐标 2,4 调用 findPath
,则显示的第一个调用将调用 findPath(3,4)
,然后将调用 findPath(2,4)
(当然会调用 findPAth(3,4)
等等,直到无穷大。
我有一个随机生成迷宫的任务("weird" 也是有效的),然后我必须找到迷宫的所有解决方案(一个解决方案是从第一行或第一列找到一条路到最后一行或最后一列),还要确定最短的那个。我这里有这段代码,但是当我 运行 它时,有一半时间它给出了一个好的答案,另一半时间它只是退出而没有任何错误弹出窗口或警告。我想原因是堆栈溢出。此外,在作业中它说我们必须在 20x20 的迷宫上解决这个问题。如果它在 5x5 上溢出,你能想象 20x20 吗?无论如何,我们将不胜感激任何帮助。请忽略评论,它实际上是分配匈牙利评论的一部分。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct vector2d {
int column, row;
};
const int N = 5; // sorok száma
const int M = 5; // oszlopok száma
bool ut[N][M];
int count = 0;
int currentLength = 0, minLength = 99999;
void initMaze(); // generál egy labirintust
void printMaze(); // kiiratja a labirintust
int exits(); // kijáratok számát adja vissza
int entrances(); // bejáratok számát adja vissza
bool findPath(int, int); // útkereső eljárás
int main() {
initMaze();
printMaze();
if (exits() > 0 && entrances() > 0) {
// bactrack
for (int i = 0; i < M; i++) // vegigjarjuk az elso sor osszes "utjat" es megkeressuk az onnan indulo megoldasokat
if (ut[0][i])
if (findPath(0, i)) {
count++;
if (currentLength < minLength)
minLength = currentLength;
currentLength = 0;
}
for (int i = 1; i < M; i++)
if (ut[i][0])
if (findPath(i, 0)) {
count++;
if (currentLength < minLength)
minLength = currentLength;
currentLength = 0;
}
} else {
cout << "Maze has no entrances or exits" << endl;
}
if (count > 0) {
cout << count << " solutions for the labyrinth, shortest one is " << minLength << " steps long" << endl;
} else {
cout << "Labyrinth has no solution" << endl;
}
system("pause");
return 0;
}
int exits() {
int count = 0;
for (int i = 1; i < M - 1; i++)
if (ut[N - 1][i])
count++;
for (int i = 1; i < N - 1; i++)
if (ut[i][M - 1])
count++;
return count;
}
int entrances() {
int count = 0;
for (int i = 1; i < M - 1; i++)
if (ut[0][i])
count++;
for (int i = 1; i < N - 1; i++)
if (ut[i][0])
count++;
return count;
}
void initMaze() {
srand(time(NULL));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
if (rand() % 3 == 0) // 3-ból egyszer legyen fal
ut[i][j] = false;
else
ut[i][j] = true;
}
}
void printMaze() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (ut[i][j])
cout << " - ";
else
cout << " | ";
}
cout << endl;
}
}
bool findPath(int row, int column) {
if (column < 0 || column > N - 1 || row < 0 || row > M - 1) return false;
if ((column == M - 1 || row == N - 1) && ut[row][column]) return true;
if (!ut[row][column]) return false;
currentLength++;
if (findPath(column + 1, row)) return true;
if (findPath(column, row + 1)) return true;
if (findPath(column - 1, row)) return true;
if (findPath(column, row - 1)) return true;
currentLength--;
return false;
}
你有递归调用
findPath(column + 1, row)
这个函数会在一段时间后进行递归调用
findPath(column - 1, row)
这两个调用的问题是会返回到调用的位置。
例如,如果使用坐标 2,4 调用 findPath
,则显示的第一个调用将调用 findPath(3,4)
,然后将调用 findPath(2,4)
(当然会调用 findPAth(3,4)
等等,直到无穷大。