【问题标题】:Why does the following code returns wrong value of count?为什么以下代码返回错误的计数值?
【发布时间】:2019-11-29 09:23:31
【问题描述】:

问题: 给定一个 n x m 的二维矩阵。矩阵包含整数。给定目的地位置,找出满足以下条件的人可以从源(原点)到达目的地的方式数 - (i) 只能在北、南、东或西方向移动。 (ii) 当且仅当该单元格的值小于当前单元格中的值时,一个人才能从一个单元格移动到另一个单元格。

#include<iostream>
using namespace std;
void printSolution(int** solution, int n, int m)
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cout<<solution[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
}
int mazeHelp(int** maze, int** solution, int x, int y, int desx, int desy, int n, int m)
{
    int count = 0;
    if(x==desx-1 && y==desy-1)
    {
        solution[x][y] = 1;
        printSolution(solution, n, m);
        count++;
        solution[x][y]=0;
        //cout<<"Return 1"<<endl;
        return count;

    }
    if(x<0 || y<0 || x>=desx || y>=desy || solution[x][y]==1)
    {
        //cout<<"Return 2"<<endl;
        return count;   
    }
    solution[x][y] = 1;
    if(x+1<desx && solution[x+1][y]!=1 && maze[x+1][y]>maze[x][y])
    {
        count = mazeHelp(maze, solution, x+1, y, desx, desy, n, m); 
    }
    if(x-1>0 && solution[x-1][y]!=1 && maze[x-1][y]>maze[x][y])
    {
        count = mazeHelp(maze, solution, x-1, y, desx, desy, n, m);
    }
    if(y+1<desy && solution[x][y+1]!=1 && maze[x][y+1]>maze[x][y])
    {
        count = mazeHelp(maze, solution, x, y+1, desx, desy, n, m);
    }
    if(y-1>0 && solution[x][y-1]!=1 && maze[x][y-1]>maze[x][y])
    {
        count = mazeHelp(maze, solution, x, y-1, desx, desy, n, m);
    }
    solution[x][y] = 0;
}
int printPaths(int** maze, int desx, int desy, int** solution, int n, int m)
{
    int count = 0;
    count = mazeHelp(maze, solution, 0, 0, desx, desy, n, m);
    return count;
}

int main()
{
    int desx, desy, n, m;
    cout<<"Enter number of rows : ";
    cin>>n;
    cout<<"Enter number of columns : ";
    cin>>m;
    cout<<"Enter matrix : "<<endl;
    int** solution = new int*[n];
  for(int i=0;i<n;i++){
    solution[i] = new int[m];
  }
    int** maze = new int*[m];
    for(int i=0;i<n;i++){
    maze[i] = new int[m];
  }
  for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            cin>>maze[i][j];
        }
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0;j<m;j++)
        {
            solution[i][j] = 0;
        }
    }
    cout<<"Enter destination address (x,y) : ";
    cin>>desx>>desy;
    int res = printPaths(maze, desx, desy, solution, n, m);
    cout<<"Total number of paths : "<<res;
    return 0;
}

我的代码为每个输入打印正确的路径,但每次都返回错误的计数值。计算路径数有错误吗?

【问题讨论】:

  • mazeHelp 在其主路径上不返回任何内容
  • 最明显的问题是您并不总是从mazeHelp 函数返回计数。另一个问题是您应该添加计数,而不是在递归调用时分配计数。 IE。 count += mazeHelp(...);

标签: c++ path return backtracking recursive-backtracking


【解决方案1】:

正如 cmets 中已经提到的,在 mazeHelp 函数中,您在多个位置递归调用 mazeHelp。每次调用都会返回许多找到的路径。您必须将它们累积到 count 变量中,并在最后返回 count,以进行父递归调用。

int mazeHelp(int** maze, int** solution, int x, int y, int desx, int desy, int n, int m)
{
    int count = 0;
    if(x==desx-1 && y==desy-1)
    {
        solution[x][y] = 1;
        printSolution(solution, n, m);
        count++;
        solution[x][y]=0;
        //cout<<"Return 1"<<endl;
        return count;

    }
    if(x<0 || y<0 || x>=desx || y>=desy || solution[x][y]==1)
    {
        //cout<<"Return 2"<<endl;
        return count;   
    }
    solution[x][y] = 1;
    if(x+1<desx && solution[x+1][y]!=1 && maze[x+1][y]>maze[x][y])
    {
        count += mazeHelp(maze, solution, x+1, y, desx, desy, n, m); 
    }
    if(x-1>0 && solution[x-1][y]!=1 && maze[x-1][y]>maze[x][y])
    {
        count += mazeHelp(maze, solution, x-1, y, desx, desy, n, m);
    }
    if(y+1<desy && solution[x][y+1]!=1 && maze[x][y+1]>maze[x][y])
    {
        count += mazeHelp(maze, solution, x, y+1, desx, desy, n, m);
    }
    if(y-1>0 && solution[x][y-1]!=1 && maze[x][y-1]>maze[x][y])
    {
        count += mazeHelp(maze, solution, x, y-1, desx, desy, n, m);
    }
    solution[x][y] = 0;
    return count;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多