我正在尝试编写一个程序,以递归方式解决特定输入的迷宫,并在每次移动后输出它在迷宫中的位置。
每当我尝试运行代码时,它都会立即崩溃,并收到“maze.exe 已停止工作”错误。
为什么我的代码不起作用?
#include <iostream>
#include <stdio.h>
using namespace std;
const int MazeHeight = 12;
const int MazeWidth = 16;
char Maze[MazeHeight][MazeWidth + 1] =
{
{'S','.','.','.','.','#','.','.','.','#','.','.','.','.','.','.'},
{'#','#','#','#','.','#','.','#','.','#','.','#','#','#','#','.'},
{'.','.','.','.','.','#','.','#','.','.','.','#','.','#','#','.'},
{'.','#','#','#','#','#','.','#','.','.','.','#','.','#','#','.'},
{'.','.','.','.','.','.','.','#','.','#','#','#','.','#','#','.'},
{'#','#','#','#','#','#','#','#','.','#','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','#','#','#','#','#','#','.'},
{'.','#','#','#','#','#','#','#','.','.','.','#','.','.','.','.'},
{'.','.','.','.','.','.','.','#','#','#','.','#','#','#','#','#'},
{'#','#','#','#','#','#','#','#','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','#','#','#','#','#','#','#','#','.'},
{'G','#','#','#','#','#','.','.','.','.','.','.','.','.','.','.'},
};
const char Wall = '#';
const char Free = '.';
const char Start = 'S';
const char End = 'G';
int solve(int X = 0, int Y = 0)
{
while(Maze[Y][X] != End){
if (Maze[Y][X] == End)
{
cout << X << Y << endl;
}
else if (X > 0 && Maze[Y][X - 1] == Free && solve(X - 1, Y))
{
cout << X << Y << endl;
}
else if (X < MazeWidth && Maze[Y][X + 1] == Free && solve(X + 1, Y))
{
cout << X << Y << endl;
}
else if (Y > 0 && Maze[Y - 1][X] == Free && solve(X, Y - 1))
{
cout << X << Y << endl;
}
else if(Y < MazeHeight && Maze[Y + 1][X] == Free && solve(X, Y + 1))
{
cout << X << Y << endl;
}
else Maze[Y][X] = Free;
}
return 0;
}
int main(int argc, char** argv){
// how do i call from here?
}