【问题标题】:Backtracking logic error with maze solving program迷宫求解程序的回溯逻辑错误
【发布时间】:2019-07-08 18:25:15
【问题描述】:

我编写了一个简单的递归迷宫求解问题,它使用递归来找到要解决的最少步数。但是,在死胡同中,程序无法备份跟踪的路径。

为了解决这个问题,我开始编写移动函数的反函数。它们可用于反转路径,但仍需要某种方式来确定使用哪一个。

迷宫测试文件:

SWWWW
OOOOE
OWOWW
OOOWW

代码主体:

//Read in maze
ifstream mazeFile;
mazeFile.open("MazeSample.txt");

vector<string> mazeRead{ istream_iterator<string>(mazeFile), istream_iterator<string>() };
maze = mazeRead;


    //Move checks
vector<string> maze;
int numMoves;
int leastMoves = 1000;

int row;
int column;

bool canMoveUp(int row, int column) {
    try {
        if (maze.at(row - 1).at(column) != ('O')) {
            cout << "(Can't move up)" << endl;
            if (maze.at(row - 1).at(column) == 'E') {
                return true;
            }
            return false;
        }
    }
    catch (const out_of_range& error) {
        cout << "(Can't move up)" << endl;
        return false;
    }
    return true;
}

bool canMoveDown(int row, int column) {
    try {
        if (maze.at(row + 1).at(column) != ('O')) {
            cout << "(Can't move down)" << endl;
            if (maze.at(row + 1).at(column) == 'E') {
                return true;
            }
            return false;
        }
    }
    catch (const out_of_range& error) {
        cout << "(Can't move down)" << endl;
        return false;
    }
    return true;
}

bool canMoveLeft(int row, int column) {
    try {
        if (maze.at(row).at(column - 1) != ('O')) {
            cout << "(Can't move left)" << endl;
            if (maze.at(row).at(column - 1) == 'E') {
                return true;
            }
            return false;
        }
    }
    catch (const out_of_range& error) {
        cout << "(Can't move left)" << endl;
        return false;
    }
    return true;
}

bool canMoveRight(int row, int column) {
    try {
        if (maze.at(row).at(column + 1) != ('O')) {
            cout << "(Can't move right)" << endl;
            if (maze.at(row).at(column + 1) == 'E') {
                return true;
            }
            return false;
        }
    }
    catch (const out_of_range& error) {
        cout << "(Can't move right)" << endl;
    }
    return true;
}


    //Maze solve function
void solve(int row, int column) {
    numMoves = numMoves + 1; //count moves

    //Base case (solution found; current position is 'E')
    if (maze[row][column] == 'E') {
        if (numMoves < leastMoves) {
            leastMoves = numMoves;
        }
    }

    if (maze[row][column] != 'E') {
        maze[row][column] = 't'; //mark path
    }

    // move up and see if move leads to solution (recursively)
    if (canMoveUp(row, column)) {
        cout << "(Move up)" << endl;
        row = row - 1;
        column = column;
        solve(row, column);
    }

    // if move chosen above doesn't lead to solution, move down & check
    if (canMoveDown(row, column)) {
        cout << "(Move down)" << endl;
        row = row + 1;
        column = column;
        solve(row, column);
    }

    // if move chosen above doesn't lead to solution, move left & check
    if (canMoveLeft(row, column)) {
        cout << "(Move left)" << endl;
        row = row;
        column = column - 1;
        solve(row, column);
    }

    // if move chosen above doesn't lead to solution, move right & check
    if (canMoveRight(row, column)) {
        cout << "(Move right)" << endl;
        row = row;
        column = column + 1;
        solve(row, column);
    }

    // if no above solution works, then unmark cell
    //backtrack (keeps going until all solutions reached)
    maze[row][column] = 'O';
    cout << "Mark as 'O'";
    numMoves = numMoves - 1;


    //TODO: PROBLEM: ROW/COLUMN NOT RESET AFTER STUCK; KEEPS SAME VALUE
            //Questionable code
    if (!canMoveUp(row, column)) {
        //Inverse of canMove?
        row = row + 1;
        column = column;
    }


    //Display vector contents
    cout << endl;
    for (int row = 0; row < maze.size(); row++) {
        cout << endl;
        for (int column = 0; column < maze[row].size(); column++) {
            cout << maze[row][column];
        }
    }
    cout << endl;
}

当遇到死胡同时,我预计迷宫会通过移动选项返回到最后一个路口。相反,光标来回移动,未能解决。 这可能是由于执行了移动功能;如果能够移动,它会将行/列变量设置为新空间。

错误路径如下所示,在第 1 行第 1 列的“t”和“O”之间切换:

SWWWW
tttOE
tWtWW
tttWW

SWWWW
tOtOE
tWtWW
tttWW

如果无法在四个方向中的任何一个方向移动,我希望代码会撤消之前的移动,直到到达最后一个路口。

【问题讨论】:

  • 您是否将代码弹出到调试器中以查看它的作用而不是您所期望的?

标签: c++ c++11 recursion 2d-vector


【解决方案1】:

无需深入了解您的算法

if (canMoveUp(row, column)) {
    cout << "(Move up)" << endl;
    row = row - 1;
    column = column;
    solve(row, column);
}

看起来很可疑。您正在更改您在后续块中使用的变量 rowcolumn。如果canMoveUp 为真,则下一次检查将是canMoveDown('originalrow' - 1, column),这将失败(因为它再次将1 加到该行并检查您刚刚用t 标记的字段。

不应该

if (canMoveUp(row, column)) {
    cout << "(Move up)" << endl;
    solve(row - 1, column);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多