C++ 为什么下面的代码返回错误的count值?
问题: 给定一个nxm的二维矩阵。这个矩阵包含整数。给定一个目的地位置,找出满足以下条件的人员可以从源(始发地)到达目的地的方式的数量- (i) 运动方向只能是北、南、东或西。 (ii)当且仅当一个单元格的值小于当前单元格中的值时,一个人才能从一个单元格移动到另一个单元格C++ 为什么下面的代码返回错误的count值?,c++,path,return,backtracking,recursive-backtracking,C++,Path,Return,Backtracking,Recursive Backtracking,问题: 给定一个nxm的二维矩阵。这个矩阵包含整数。给定一个目的地位置,找出满足以下条件的人员可以从源(始发地)到达目的地的方式的数量- (i) 运动方向只能是北、南、东或西。 (ii)当且仅当一个单元格的值小于当前单元格中的值时,一个人才能从一个单元格移动到另一个单元格 #include<iostream> using namespace std; void printSolution(int** solution, int n, int m) { for(int i=0;
#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;
}
#包括
使用名称空间std;
无效打印解决方案(整数**解决方案,整数n,整数m)
{
对于(int i=0;i,如注释中所述,在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;
}
intmazehelp(int**maze,int**solution,intx,inty,intdesx,intdesy,intn,intm)
{
整数计数=0;
如果(x==desx-1&&y==desy-1)
{
溶液[x][y]=1;
打印溶液(溶液,n,m);
计数++;
溶液[x][y]=0;
//cout如注释中所述,在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;
}
intmazehelp(int**maze,int**solution,intx,inty,intdesx,intdesy,intn,intm)
{
整数计数=0;
如果(x==desx-1&&y==desy-1)
{
溶液[x][y]=1;
打印溶液(溶液,n,m);
计数++;
溶液[x][y]=0;
//coutmazeHelp
在其主路径上不返回任何内容最明显的问题是,您并不总是从mazeHelp
函数返回计数。另一个问题是,您应该添加计数,而不是在递归调用时分配计数。即count+=mazeHelp(…)
mazeHelp
在其主路径上不返回任何内容最明显的问题是您并不总是从mazeHelp
函数返回计数。另一个问题是您应该添加计数,而不是在递归调用时分配计数。即count+=mazeHelp(…);