【发布时间】:2014-11-24 22:41:49
【问题描述】:
我正在尝试编写一个程序,以递归方式解决特定输入的迷宫并在每次移动后输出它在迷宫中的位置。
每当我尝试运行我的代码时,它都会立即崩溃并且我收到“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?
}
【问题讨论】:
-
"
int main(int X = 0, int Y = 0)" -- 哇哦。你的编译器不会警告你吗? -
不,一切都 100% 编译,没有错误。
-
你能连接一个调试器并获得失败的堆栈跟踪吗?
-
不允许在代码中显式调用
main。 /线程 -
@DeniseSkidmore:稍微好一点(不使用误导性的array-is-really-a-pointer 语法):
int main(int argc, char** argv)