Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c++ - Why does the findPaths function decrements the i and j variables after displaying the vector?

  1. This program finds all possible paths from top left to bottom right of a matrix.
  2. 'isvalid' checks if (i, j) is valid matrix coordinate
  3. 'printPath' prints the route taken
  4. 'findPaths' saves the content on a vector and checks if the path can go right or down
#include <iostream>
#include <vector>
using namespace std;
 
#define M 3
#define N 3
 
// Function to check if (i, j) is valid matrix coordinate

bool isvalid(int i, int j)
{
    return (i >= 0 && i < M && j >= 0 && j < N);
}
 
// Function to print the route taken

void printPath(vector<int> const &path, int last)
{
    for (int i : path)
        cout << i << " - ";
    cout << last << endl;
}
 

void findPaths(int mat[][N], vector<int> &path, int i, int j)
{
    // if we have reached the last cell, print the route
    if (i == M - 1 && j == N - 1)
    {
        printPath(path, mat[i][j]);
        return;
    }
 
    // include current cell in path
    path.push_back(mat[i][j]);
 
    // move right
    if (isvalid(i, j + 1))
        findPaths(mat, path, i, j + 1);
 
    // move down
    if (isvalid(i + 1, j))
        findPaths(mat, path, i + 1, j);
 
    // remove current cell from the path
    path.pop_back();
}
 
int main()
{
    int mat[M][N] =
    {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
 
    vector<int> path;
 
    // start from (0, 0) cell
    int x = 0, y = 0;
 
    findPaths(mat, path, x, y);
 
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because in Recursion we are making calls with i + 1 and j + 1; So when we make a call to the function in recursive way think of it as a new function that does same thing but with i + 1 and j + 1 value. So it will keep on making new function it the last function it make reaches its end } or it encounters a return statement. Then last function will be destroyed and we will be at second last function (which has i - 1 and j - 1 value and this will be our new last function). this may not be very accurate but i think it is a good way to understand.

Think of recursion like you are standing in a line and you want to know at what position you are standing but line is too long to be able to look and tell, So you ask a guy standing in front of you and he also doesn't know so he ask the guy in front of him and so on and on till we reach a guy who knows on which position he is standing so he say something like i am standing on 4th position to guy behind him(Now we start going back). guy behind him add one to it and say I am on 5th position and so on and on till it reaches back to us.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...