📌  相关文章
📜  使用最多 K 次移动打印所有可能的路径以从给定位置逃离矩阵

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

使用最多 K 次移动打印所有可能的路径以从给定位置逃离矩阵

给定一个维度为N*M的矩阵mat[][] 、一个正整数K和源单元(X, Y) ,任务是打印从源单元( X, Y) 移出矩阵的所有可能路径)通过在所有四个方向上移动,每次移动最多 K次移动。

例子:

方法:给定的问题可以通过使用递归和回溯来解决。请按照以下步骤解决给定的问题:

  • 初始化一个数组,比如说, arrayOfMoves[] ,它存储从源单元格移动到矩阵外的所有移动。
  • 定义一个递归函数,比如printAllmoves(N, M, move, X, Y, arrayOfMoves) ,然后执行以下步骤:
    • 基本情况:
      • 如果移动的值是非负的并且当前单元格(X, Y)不在矩阵中,则打印存储在ArrayOfMoves[]中的所有移动。
      • 如果move的值小于0 ,则从函数返回。
    • 在数组arrayOfMoves[]中插入当前单元格(X, Y)
    • 通过将moves的值减1来递归调用当前单元格(X,Y)的所有四个方向的函数
    • 如果数组arrayOfMoves[]的大小大于 1,则删除为回溯步骤插入的最后一个单元格。
  • 调用函数printAllmoves(N, M, move, X, Y, arrayOfMoves)打印所有可能的移动。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to print all the paths
// that are outside of the matrix
void printAllmoves(
  int n, int m, int x, int y,
  int moves, vector> ArrayOfMoves)
{
   
  // Base Case
  if (x <= 0 || y <= 0 || x >= n + 1
      || y >= m + 1 && moves >= 0) {
 
    // Add the last position
    ArrayOfMoves.push_back({x, y});
 
    // Traverse the pairs
    for (auto ob : ArrayOfMoves) {
 
      // Print all the paths
      cout<<"("< 1)
      ArrayOfMoves.pop_back();
 
    return;
  }
 
  // If no moves remain
  if (moves <= 0) {
    return;
  }
 
  // Add the current position
  // in the list
  ArrayOfMoves.push_back({x, y});
 
  // Recursive function Call
  // in all the four directions
  printAllmoves(n, m, x, y - 1, moves - 1,
                ArrayOfMoves);
  printAllmoves(n, m, x, y + 1, moves - 1,
                ArrayOfMoves);
  printAllmoves(n, m, x - 1, y, moves - 1,
                ArrayOfMoves);
  printAllmoves(n, m, x + 1, y, moves - 1,
                ArrayOfMoves);
 
  // Backtracking Steps
  if (ArrayOfMoves.size() > 1) {
    ArrayOfMoves.pop_back();
  }
}
 
// Driver Code
int main()
{
  int N = 2, M = 2;
  int X = 1;
  int Y = 1;
  int K = 2;
  vector> ArrayOfMoves;
 
  // Function Call
  printAllmoves(N, M, X, Y, K,
                ArrayOfMoves);
}
 
// This code is contributed by ipg2016107.


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Class for the pairs
    static class Pair {
        int a;
        int b;
        Pair(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
    }
 
    // Function to print all the paths
    // that are outside of the matrix
    static void printAllmoves(
        int n, int m, int x, int y,
        int moves, ArrayList ArrayOfMoves)
    {
        // Base Case
        if (x <= 0 || y <= 0 || x >= n + 1
            || y >= m + 1 && moves >= 0) {
 
            // Add the last position
            ArrayOfMoves.add(new Pair(x, y));
 
            // Traverse the pairs
            for (Pair ob : ArrayOfMoves) {
 
                // Print all the paths
                System.out.print("(" + ob.a
                                 + " " + ob.b
                                 + ")");
            }
 
            System.out.println();
 
            // Backtracking Steps
            if (ArrayOfMoves.size() > 1)
                ArrayOfMoves.remove(
                    ArrayOfMoves.size() - 1);
 
            return;
        }
 
        // If no moves remain
        if (moves <= 0) {
            return;
        }
 
        // Add the current position
        // in the list
        ArrayOfMoves.add(new Pair(x, y));
 
        // Recursive function Call
        // in all the four directions
        printAllmoves(n, m, x, y - 1, moves - 1,
                      ArrayOfMoves);
        printAllmoves(n, m, x, y + 1, moves - 1,
                      ArrayOfMoves);
        printAllmoves(n, m, x - 1, y, moves - 1,
                      ArrayOfMoves);
        printAllmoves(n, m, x + 1, y, moves - 1,
                      ArrayOfMoves);
 
        // Backtracking Steps
        if (ArrayOfMoves.size() > 1) {
            ArrayOfMoves.remove(
                ArrayOfMoves.size() - 1);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 2, M = 2;
        int X = 1;
        int Y = 1;
        int K = 2;
        ArrayList ArrayOfMoves
            = new ArrayList<>();
 
        // Function Call
        printAllmoves(N, M, X, Y, K,
                      ArrayOfMoves);
    }
}


Python3
# Python program for the above approach
 
# Function to print all the paths
# that are outside of the matrix
def printAllmoves(n,m,x,y, moves,ArrayOfMoves):
   
  # Base Case
  if (x <= 0 or y <= 0 or x >= n + 1 or y >= m + 1 and moves >= 0):
     
    # Add the last position
    ArrayOfMoves.append([x, y])
 
    # Traverse the pairs
    for ob in ArrayOfMoves:
       
      # Print all the paths
      print("(",ob[0],ob[1],")",end="")
 
    print("\n",end = "")
 
    # Backtracking Steps
    if(len(ArrayOfMoves) > 1):
      ArrayOfMoves.pop()
 
    return
 
  # If no moves remain
  if (moves <= 0):
    return
 
  # Add the current position
  # in the list
  ArrayOfMoves.append([x, y])
 
  # Recursive function Call
  # in all the four directions
  printAllmoves(n, m, x, y - 1, moves - 1,ArrayOfMoves)
  printAllmoves(n, m, x, y + 1, moves - 1,ArrayOfMoves)
  printAllmoves(n, m, x - 1, y, moves - 1,ArrayOfMoves)
  printAllmoves(n, m, x + 1, y, moves - 1,ArrayOfMoves)
 
  # Backtracking Steps
  if (len(ArrayOfMoves) > 1):
    ArrayOfMoves.pop()
 
# Driver Code
if __name__ == '__main__':
  N = 2
  M = 2
  X = 1
  Y = 1
  K = 2
  ArrayOfMoves = []
 
  # Function Call
  printAllmoves(N, M, X, Y, K,ArrayOfMoves)
 
  # This code is contributed by SURENDRA_GANGWAR.


C#
using System;
using System.Collections;
public class GFG {
    class Pair {
        public int a;
        public int b;
        public Pair(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
    }
 
    // Function to print all the paths
    // that are outside of the matrix
    static void printAllmoves(int n, int m, int x, int y,
                              int moves,
                              ArrayList ArrayOfMoves)
    {
        // Base Case
        if (x <= 0 || y <= 0 || x >= n + 1
            || y >= m + 1 && moves >= 0) {
 
            // Add the last position
            ArrayOfMoves.Add(new Pair(x, y));
 
            // Traverse the pairs
            foreach (Pair ob in ArrayOfMoves) {
 
                // Print all the paths
                Console.Write("(" + ob.a + " " + ob.b
                                  + ")");
            }
 
            Console.WriteLine();
 
            // Backtracking Steps
            if (ArrayOfMoves.Count > 1)
                ArrayOfMoves.Remove(ArrayOfMoves.Count
                                    - 1);
 
            return;
        }
 
        // If no moves remain
        if (moves <= 0) {
            return;
        }
 
        // Add the current position
        // in the list
        ArrayOfMoves.Add(new Pair(x, y));
 
        // Recursive function Call
        // in all the four directions
        printAllmoves(n, m, x, y - 1, moves - 1,
                      ArrayOfMoves);
        printAllmoves(n, m, x, y + 1, moves - 1,
                      ArrayOfMoves);
        printAllmoves(n, m, x - 1, y, moves - 1,
                      ArrayOfMoves);
        printAllmoves(n, m, x + 1, y, moves - 1,
                      ArrayOfMoves);
 
        // Backtracking Steps
        if (ArrayOfMoves.Count > 1) {
            ArrayOfMoves.Remove(ArrayOfMoves.Count - 1);
        }
    }
    static public void Main()
    {
        int N = 2, M = 2;
        int X = 1;
        int Y = 1;
        int K = 2;
        ArrayList ArrayOfMoves = new ArrayList();
 
        // Function Call
        printAllmoves(N, M, X, Y, K, ArrayOfMoves);
    }
}
 
// This code is contributed by maddler.


Javascript


输出:
(1 1)(1 0)
(1 1)(1 2)(1 3)
(1 1)(1 2)(0 2)
(1 1)(0 1)
(1 1)(2 1)(2 0)
(1 1)(2 1)(3 1)

时间复杂度: O(4 N )
辅助空间: O(4 N )