#include <iostream>
#include <vector>
using namespace std;
// Function to count the number of paths from (i, j) to
// bottom-right
int countPaths(const vector<vector<int> >& grid, int i,
int j)
{
// Base case: if out of bounds or obstacle encountered
if (i >= grid.size() || j >= grid[0].size()
|| grid[i][j] == 0)
return 0;
// If reached the bottom-right cell
if (i == grid.size() - 1 && j == grid[0].size() - 1)
return 1;
// Move down and right
return countPaths(grid, i + 1, j)
+ countPaths(grid, i, j + 1);
}
int main()
{
vector<vector<int> > grid1 = { { 1, 0, 0, 0 },
{ 1, 1, 1, 1 },
{ 0, 0, 1, 0 },
{ 0, 1, 1, 1 } };
vector<vector<int> > grid2 = { { 1, 1 }, { 1, 1 } };
cout << countPaths(grid1, 0, 0)
<< endl; // Output for grid1
cout << countPaths(grid2, 0, 0)
<< endl; // Output for grid2
return 0;
}
// This code is contributed by Ayush Mishra