📌  相关文章
📜  在仅向下或向右移动的矩阵中打印从给定源到目的地的所有唯一路径

📅  最后修改于: 2022-05-13 01:56:07.550000             🧑  作者: Mango

在仅向下或向右移动的矩阵中打印从给定源到目的地的所有唯一路径

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

例子:

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

  • 如果当前单元格超出边界,则返回。
  • 将当前单元格的值推入向量path[]。
  • 如果当前单元格是目的地,则打印当前路径。
  • 为值{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.


Python3
# 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


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  static List s = new List();
  static List d = new List();
  static int m = 2, n = 3;
 
  // Function to print all the paths
  static void printList(List path, int[,] mat)
  {
    int cnt = path.Count;
    for (int i = 0; i < cnt; i += 2)
      Console.Write(mat[path[i],path[i + 1]] + " ");
    Console.WriteLine();
  }
 
  // Function to find all the paths recursively
  static void countPaths(int i, int j,
                         List path, int[,] mat)
  {
 
    // Base Case
    if (i > d[0] || j > d[1])
      return;
    path.Add(i);
    path.Add(j);
 
    // Destination is reached
    if (i == d[0] && j == d[1]) {
      printList(path, mat);
      path.RemoveAt(path.Count - 1);
      path.RemoveAt(path.Count - 1);
      return;
    }
 
    // Calling the function
    countPaths(i, j + 1, path, mat);
    countPaths(i + 1, j, path, mat);
    path.RemoveAt(path.Count - 1);
    path.RemoveAt(path.Count - 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);
    List path = new List();
 
    countPaths(s[0], s[1], path, mat);
  }
}
 
// This code is contributed by Rajput-Ji


Javascript



输出:
1 2 3 6 
1 2 5 6 
1 4 5 6

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