原文:

给定一个 mat[][] 、一个源“ s 和一个目的地“ d ”,打印从给定的“ s ”到“ d 的所有唯一路径。从每个单元格中,您可以只向右或向下移动。

示例:

输入: mat[][] = {{1,2,3},{4,5,6}},s[] = {0,0},d[]={1,2} 输出: 1 4 5 6 1 2 5 6 1 2 3 6

输入 : mat[][] = {{1,2},{3,4}},s[] = {0,1},d[] = {1,1} 输出: 2 4

方法:使用从源开始,从矩阵路径中的每个单元格先右后下移动 mat[][] ,并将每个值存储在一个向量中。如果到达目的地,打印矢量作为可能的路径之一。按照以下步骤解决问题:

  • 如果当前单元格超出边界,则返回。
  • 将当前单元格的值推入向量路径[]。
  • 如果当前单元格是目标,则打印当前路径。
  • 对值 {i 1,j}{i,j 1}调用相同的函数。

下面是上述方法的实现。

c

// c   program for the above approach
#include 
using namespace std;
vector > mat;
vector s;
vector d;
int m = 2, n = 3;
// function to print all the  paths
void printvector(vector path)
{
    int cnt = path.size();
    for (int i = 0; i < cnt; i  = 2)
        cout << mat[path[i]][path[i   1]]
             << " ";
    cout << endl;
}
// function to find all the paths recursively
void countpaths(int i, int j, vector path)
{
    // base case
    if (i > d[0] || j > d[1])
        return;
    path.push_back(i);
    path.push_back(j);
    // destination is reached
    if (i == d[0] && j == d[1]) {
        printvector(path);
        return;
    }
    // calling the function
    countpaths(i, j   1, path);
    countpaths(i   1, j, path);
}
// drivercode
int main()
{
    mat = { { 1, 2, 3 },
            { 4, 5, 6 } };
    s = { 0, 0 };
    d = { 1, 2 };
    vector path;
    countpaths(s[0], s[1], path);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.util.*;
class gfg
{
  static vector s = new vector<>();
  static vector d = new vector<>();
  static int m = 2, n = 3;
  // function to print all the  paths
  static void printvector(vector path, int[][] mat)
  {
    int cnt = path.size();
    for (int i = 0; i < cnt; i  = 2)
      system.out.print(mat[path.get(i)][path.get(i   1)]  " ");
    system.out.println();
  }
  // function to find all the paths recursively
  static void countpaths(int i, int j, vector path, int[][]mat)
  {
    // base case
    if (i > d.get(0) || j > d.get(1))
      return;
    path.add(i);
    path.add(j);
    // destination is reached
    if (i == d.get(0) && j == d.get(1)) {
      printvector(path,mat);
      path.remove(path.size()-1);
      path.remove(path.size()-1);
      return;
    }
    // calling the function
    countpaths(i, j   1, path,mat);
    countpaths(i   1, j, path,mat);
    path.remove(path.size()-1);
    path.remove(path.size()-1);
  }
  // drivercode
  public static void main(string[] args) {
    int[][] mat = {{ 1, 2, 3 },
                   { 4, 5, 6 } };
    s.add(0);
    s.add(0);
    d.add(1);
    d.add(2);
    vector path = new vector<>();
    countpaths(s.get(0), s.get(1), path, mat);
  }
}
// this code is contributed by rajput-ji.

python 3

# python code for the above approach
mat = none
s = none
d = none
m = 2
n = 3
# function to print all the  paths
def printvector(path):
    cnt = len(path)
    for i in range(0, cnt, 2):
        print(mat[path[i]][path[i   1]], end=" ")
    print("")
# function to find all the paths recursively
def countpaths(i, j, path):
    # base case
    if (i > d[0] or j > d[1]):
        return
    path.append(i)
    path.append(j)
    # destination is reached
    if (i == d[0] and j == d[1]):
        printvector(path)
        path.pop()
        path.pop()
        return
    # calling the function
    countpaths(i, j   1, path)
    countpaths(i   1, j, path)
    path.pop()
    path.pop()
# drivercode
mat = [[1, 2, 3],
       [4, 5, 6]]
s = [0, 0]
d = [1, 2]
path = []
countpaths(s[0], s[1], path)
# this code is contributed by saurabh jaiswal

java 描述语言


output: 

1 2 3 6 
1 2 5 6 
1 4 5 6

时间复杂度:o(2n m) 辅助空间: o(1)