Given a matrix mat[][] of dimension N*M, a positive integer K and the source cell (X, Y), the task is to print all possible paths to move out of the matrix from the source cell (X, Y) by moving in all four directions in each move in at most K moves.
Examples:
Input: N = 2, M = 2, X = 1, Y = 1, K = 2 Output: (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)
Input: N = 1, M = 1, X = 1, Y = 1, K = 2 Output: (1 1)(1 0) (1 1)(1 2) (1 1)(0 1) (1 1)(2 1)
Approach: The given problem can be solved by using Recursion and Backtracking. Follow the steps below to solve the given problem:
- Initialize an array, say, arrayOfMoves[] that stores all the moves moving from the source cell to the out of the matrix.
- Define a recursive function, say printAllmoves(N, M, moves, X, Y, arrayOfMoves), and perform the following steps:
- Base Case:
- If the value of the moves is non-negative and the current cell (X, Y) is out of the matrix, then print all the moves stored in the ArrayOfMoves[].
- If the value of moves is less than 0 then return from the function.
- Insert the current cell (X, Y) in the array arrayOfMoves[].
- Recursively call the function in all the four directions of the current cell (X, Y) by decrementing the value of moves by 1
- If the size of the array arrayOfMoves[] is greater than 1, then remove the last cell inserted for the Backtracking steps.
- Call the function printAllmoves(N, M, moves, X, Y, arrayOfMoves) to print all possible moves.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void printAllmoves(
int n, int m, int x, int y,
int moves, vector<pair< int , int >> ArrayOfMoves)
{
if (x <= 0 || y <= 0 || x >= n + 1
|| y >= m + 1 && moves >= 0) {
ArrayOfMoves.push_back({x, y});
for ( auto ob : ArrayOfMoves) {
cout<< "(" <<ob.first<< " " <<ob.second<< ")" ;
}
cout<<endl;
if (ArrayOfMoves.size() > 1)
ArrayOfMoves.pop_back();
return ;
}
if (moves <= 0) {
return ;
}
ArrayOfMoves.push_back({x, y});
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);
if (ArrayOfMoves.size() > 1) {
ArrayOfMoves.pop_back();
}
}
int main()
{
int N = 2, M = 2;
int X = 1;
int Y = 1;
int K = 2;
vector<pair< int , int >> ArrayOfMoves;
printAllmoves(N, M, X, Y, K,
ArrayOfMoves);
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static class Pair {
int a;
int b;
Pair( int a, int b)
{
this .a = a;
this .b = b;
}
}
static void printAllmoves(
int n, int m, int x, int y,
int moves, ArrayList<Pair> ArrayOfMoves)
{
if (x <= 0 || y <= 0 || x >= n + 1
|| y >= m + 1 && moves >= 0 ) {
ArrayOfMoves.add( new Pair(x, y));
for (Pair ob : ArrayOfMoves) {
System.out.print( "(" + ob.a
+ " " + ob.b
+ ")" );
}
System.out.println();
if (ArrayOfMoves.size() > 1 )
ArrayOfMoves.remove(
ArrayOfMoves.size() - 1 );
return ;
}
if (moves <= 0 ) {
return ;
}
ArrayOfMoves.add( new Pair(x, y));
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);
if (ArrayOfMoves.size() > 1 ) {
ArrayOfMoves.remove(
ArrayOfMoves.size() - 1 );
}
}
public static void main(String[] args)
{
int N = 2 , M = 2 ;
int X = 1 ;
int Y = 1 ;
int K = 2 ;
ArrayList<Pair> ArrayOfMoves
= new ArrayList<>();
printAllmoves(N, M, X, Y, K,
ArrayOfMoves);
}
}
|
Python3
def printAllmoves(n,m,x,y, moves,ArrayOfMoves):
if (x < = 0 or y < = 0 or x > = n + 1 or y > = m + 1 and moves > = 0 ):
ArrayOfMoves.append([x, y])
for ob in ArrayOfMoves:
print ( "(" ,ob[ 0 ],ob[ 1 ], ")" ,end = "")
print ( "\n" ,end = "")
if ( len (ArrayOfMoves) > 1 ):
ArrayOfMoves.pop()
return
if (moves < = 0 ):
return
ArrayOfMoves.append([x, y])
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)
if ( len (ArrayOfMoves) > 1 ):
ArrayOfMoves.pop()
if __name__ = = '__main__' :
N = 2
M = 2
X = 1
Y = 1
K = 2
ArrayOfMoves = []
printAllmoves(N, M, X, Y, K,ArrayOfMoves)
|
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;
}
}
static void printAllmoves( int n, int m, int x, int y,
int moves,
ArrayList ArrayOfMoves)
{
if (x <= 0 || y <= 0 || x >= n + 1
|| y >= m + 1 && moves >= 0) {
ArrayOfMoves.Add( new Pair(x, y));
foreach (Pair ob in ArrayOfMoves) {
Console.Write( "(" + ob.a + " " + ob.b
+ ")" );
}
Console.WriteLine();
if (ArrayOfMoves.Count > 1)
ArrayOfMoves.Remove(ArrayOfMoves.Count
- 1);
return ;
}
if (moves <= 0) {
return ;
}
ArrayOfMoves.Add( new Pair(x, y));
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);
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();
printAllmoves(N, M, X, Y, K, ArrayOfMoves);
}
}
|
Javascript
<script>
class Pair {
constructor(a , b) {
this .a = a;
this .b = b;
}
}
function ob(item)
{
document.write( "(" + item.a + " " + item.b + ")" );
}
function printAllmoves(n , m , x , y , moves, ArrayOfMoves)
{
if (x <= 0 || y <= 0 || x >= n + 1 || y >= m + 1 && moves >= 0) {
ArrayOfMoves.push( new Pair(x, y));
ArrayOfMoves.forEach (ob);
document.write( "<br/>" );
if (ArrayOfMoves.length > 1)
ArrayOfMoves.pop(ArrayOfMoves.length - 1);
return ;
}
if (moves <= 0) {
return ;
}
ArrayOfMoves.push( new Pair(x, y));
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);
if (ArrayOfMoves.length > 1) {
ArrayOfMoves.pop(ArrayOfMoves.length - 1);
}
}
var N = 2, M = 2;
var X = 1;
var Y = 1;
var K = 2;
var ArrayOfMoves = new Array();
printAllmoves(N, M, X, Y, K, ArrayOfMoves);
</script>
|
Output:
(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)
Time Complexity: O(4N) Auxiliary Space: O(4N)
|