[英]Why does the following code returns wrong value of count?
問題:給定一個nx m的2D矩陣。 矩陣包含整數。 在給定目的地位置的情況下,找到滿足以下條件的人可以從源(來源)到達目的地的方式:(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;
}
我的代碼為每個輸入打印正確的路徑,但是每次都返回錯誤的count值。 計算路徑數時是否有錯誤?
如注釋中已經提到的,在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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.