为什么我的迷宫求解器不工作? [英] Why does my maze solver not work?

查看:124
本文介绍了为什么我的迷宫求解器不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写递归解决特定输入的迷宫,其输出的每个移动后的迷宫位置的程序。

每当我尝试运行我的code,它会立即崩溃,我得到一个maze.exe已停止工作的错误。

为什么我的code工作?

 的#include<&iostream的GT;
#包括LT&;&stdio.h中GT;
使用命名空间std;
const int的MazeHeight = 12;
const int的MazeWidth = 16;焦炭迷宫[MazeHeight] [MazeWidth + 1] =
{
    {S,。,。,。,。,#,。,。,。,#,。,。, ','。','。',''},
    {'#','#','#','#',。,#,。,#,。,#,。,#, #,#,#,。},
    {'。','。','。','。','。','#',。,#,。,。,。,#, ','#','#',''},
    {'。','#','#','#','#','#',。,#,。,。,。,#, ','#','#',''},
    {'。','。','。','。','。','。','。','#',。,#,#,#, ','#','#',''},
    {'#','#','#','#','#','#','#','#',。,#,。,。, ','。','。',''},
    {'。','。','。','。','。','。','。','。','。','#','#','#',' #,#,#,。},
    {'。','#','#','#','#','#','#','#',。,。,。,#, ','。','。',''},
    {'。','。','。','。','。','。','。','#','#','#',。,#, #,#,#,#},
    {'#','#','#','#','#','#','#','#',。,。,。,。, ','。','。',''},
    {'。','。','。','。','。','。','。','#','#','#','#','#',' #,#,#,。},
    {'G','#','#','#','#','#','。','。','。','。','。','。',' ','。','。',''},
};为const char墙='#';
为const char免费='';
为const char开始='S';
为const char结束='G';INT解决(中间体X = 0,整数Y = 0)
{
    而(迷宫[Y] [X]!=完){    如果(迷宫[Y] [X] ==完)
    {
        COUT<<点¯x所述&;&下; Y'LT;< ENDL;
    }    否则如果(X≠0&放大器;&放大器;迷宫[Y] [X - 1] ==免&放大器;&放大器;解决(x - 1,y))
    {
        COUT<<点¯x所述&;&下; Y'LT;< ENDL;
    }
    否则,如果(X< MazeWidth&放大器;&安培;迷宫[Y] [X + 1] ==免费&功放;&安培;解决(X + 1,Y))
    {
        COUT<<点¯x所述&;&下; Y'LT;< ENDL;
    }
    否则如果(Y大于0&放大器;&放大器;迷宫[Y - 1] [X] ==免&放大器;&放大器;解决(X,Y - 1))
    {
        COUT<<点¯x所述&;&下; Y'LT;< ENDL;
    }
    否则如果(Y'LT; MazeHeight&放大器;&放大器;迷宫[Y + 1] [X] ==免&放大器;&放大器;解决(X,Y + 1))
    {
        COUT<<点¯x所述&;&下; Y'LT;< ENDL;
    }    其他迷宫[Y] [X] =自由;
    }    返回0;
}INT主(INT ARGC,字符** argv的){    //我如何从这里打电话?
}


解决方案

首先被调用运行时环境传递给它的参数,所以你的默认参数不使用。重命名功能,并从叫它主

I'm trying to write a program that solves a specific inputted maze recursively and outputs it's position in the maze after each move.

Whenever I try and run my code, it immediately crashes and I get a "maze.exe has stopped working" error.

Why isn't my code working?

    #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?
}

解决方案

When main is first called the runtime environment passes it arguments, so your default arguments aren't used. Rename the function and call it from main.

这篇关于为什么我的迷宫求解器不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆