Open In App

Count of ways to traverse a Matrix according to given conditions

Given an integer N which represents an N x N Square Matrix, the task is to print the number of ways to move from top left to the bottom right of the Square Matrix by following the conditions:  

Examples: 

Input: N = 2 
Output:
Explanation: 
The three possible ways are: 
{0-0} – {0-1} – {1-1} 
{0-0} – {1-0} – {1-1} 
{0-0} – {1-1} 
Input:
Output: 18 
Explanation: 
The possible ways are: 
{0-0} – {2-1} – {2-2} 
{0-0} – {1-2} – {2-2} 
{0-0} – {0-1} – {2-2} 
{0-0} – {0-1} – {0-2} – {1-2} – {2-2} 
{0-0} – {0-1} – {0-2} – {2-2} 
{0-0} – {0-1} – {1-1} – {2-2} 
{0-0} – {0-1} – {2-1} – {2-2} 
{0-0} – {0-2} – {1-2} – {2-2} 
{0-0} – {0-2} – {2-2} 
{0-0} – {1-0} – {2-2} 
{0-0} – {1-0} – {1-1} – {2-2} 
{0-0} – {1-0} – {1-2} – {2-2} 
{0-0} – {1-0} – {2-0} – {2-1} – {2-2} 
{0-0} – {1-0} – {2-0} – {2-2} 
{0-0} – {2-0} – {2-1} – {2-2} 
{0-0} – {2-0} – {2-2} 
{0-0} – {1-1} – {2-2} 
{0-0} – {2-2} 

Approach: The idea is to recursively check for every possible case whether it reaches the end of the Square Matrix or not. To do this, a recursive function is defined which takes the current position and the last position as the input parameters. The following are the conditions of the recursive function:  

if (cr == er && cc == ec) {
    count++;
    return;
}

if (cr > er || cc > ec) {
   return;
}
for (int i = 1; i <= er; i++) {
    if (cc == 0 || cr == 0 
       || cr == er || cc == ec) {
        chessboard1(cr, cc + i, er, ec);
    }
}

for (int i = 1; i <= er; i++) {
    if (cc == 0 || cr == 0 
       || cr == er || cc == ec) {
        chessboard1(cr + i, cc, er, ec);
    }
}
for (int i = 1; i <= er; i++) {
    if (cr == cc || cr + cc == er) {
        chessboard1(cr + i, cc + i,
                    er, ec);
    }
}

Below is the implementation of the above approach:
 




// C++ program to count the number
// of ways to traverse the given
// N x N Square Matrix recursively
#include<bits/stdc++.h>
using namespace std;
   
// Variable to store the
// total number of ways to
// traverse the Square Matrix
     
// Function to recursively
// count the total number
// of ways to traverse the
// given N x N Square Matrix
void chessboard1(int cr, int cc,
                 int er, int ec,
                 int& count)
{
 
  // If the last index has been
  // reached, then the count is
  // incremented and returned
  if (cr == er && cc == ec)
  {
    count++;
    return;
  }
 
  // If the last index cannot
  // be reached
  if (cr > er || cc > ec)
  {
    return;
  }
 
  // If the current position is
  // neither on the edges nor
  // on the diagonal, then the
  // pointer moves like a knight
  chessboard1(cr + 2, cc + 1,
              er, ec,count);
  chessboard1(cr + 1, cc + 2,
              er, ec,count);
 
  // If the pointer is on the
  // edges of the Square Matrix
  // next move can be horizontal
  // or vertical
 
  // This for loop is used to include
  // all the horizontal traversal cases
  // recursively
  for (int i = 1; i <= er; i++)
  {
    if (cc == 0 || cr == 0||
        cr == er || cc == ec)
    {
      chessboard1(cr, cc + i,
                  er, ec,count);
    }
  }
 
  // This for loop is used to include
  // all the vertical traversal cases
  // recursively
  for (int i = 1; i <= er; i++)
  {
    if (cc == 0 || cr == 0||
        cr == er || cc == ec)
    {
      chessboard1(cr + i, cc,
                  er, ec,count);
    }
  }
 
  // If the pointer is on the
  // diagonal of the Square Matrix
  // next move is also diagonal.
  // For loop is used to include
  // all the diagonal traversal cases.
  for (int i = 1; i <= er; i++)
  {
    if (cr == cc || cr + cc == er)
    {
      chessboard1(cr + i,  cc + i, 
                  er, ec, count);
    }
  }
}
   
// Driver code
int main()
{
  int N = 3;
  int count=0;
  chessboard1(0, 0, N - 1, N - 1,count);
  cout<<count;
}
 
// This code is contributed by chahattekwani71




// Java program to count the number
// of ways to traverse the given
// N x N Square Matrix recursively
 
public class GFG {
 
    // Variable to store the total number
    // of ways to traverse the Square Matrix
    static int count = 0;
 
    // Function to recursively
    // count the total number
    // of ways to traverse the
    // given N x N Square Matrix
    public static void chessboard1(
        int cr, int cc,
        int er, int ec)
    {
 
        // If the last index has been reached, then
        // the count is incremented and returned
        if (cr == er && cc == ec) {
            count++;
            return;
        }
 
        // If the last index cannot be reached
        if (cr > er || cc > ec) {
            return;
        }
 
        // If the current position is neither
        // on the edges nor on the diagonal,
        // then the pointer moves
        // like a knight
        chessboard1(cr + 2, cc + 1, er, ec);
        chessboard1(cr + 1, cc + 2, er, ec);
 
        // If the pointer is on the
        // edges of the Square Matrix
        // next move can be horizontal or vertical
 
        // This for loop is used to include all the
        // horizontal traversal cases recursively
        for (int i = 1; i <= er; i++) {
            if (cc == 0 || cr == 0
                || cr == er || cc == ec) {
                chessboard1(cr, cc + i, er, ec);
            }
        }
 
        // This for loop is used to include all the
        // vertical traversal cases recursively
        for (int i = 1; i <= er; i++) {
            if (cc == 0 || cr == 0
                || cr == er || cc == ec) {
                chessboard1(cr + i, cc, er, ec);
            }
        }
 
        // If the pointer is on the
        // diagonal of the Square Matrix
        // next move is also diagonal.
        // For loop is used to include
        // all the diagonal traversal cases.
        for (int i = 1; i <= er; i++) {
            if (cr == cc
                || cr + cc == er) {
 
                chessboard1(cr + i,
                            cc + i,
                            er, ec);
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        int N = 3;
        chessboard1(0, 0, N - 1, N - 1);
        System.out.println(count);
    }
}




# Python3 program to count the number
# of ways to traverse the given
# N x N Square Matrix recursively
 
# Variable to store the
# total number of ways to
# traverse the Square Matrix
count = 0
      
# Function to recursively
# count the total number
# of ways to traverse the
# given N x N Square Matrix
def chessboard1(cr, cc, er, ec):
  global count
  # If the last index has been
  # reached, then the count is
  # incremented and returned
  if cr == er and cc == ec:
    count+=1
    return
  
  # If the last index cannot
  # be reached
  if cr > er or cc > ec:
    return
  
  # If the current position is
  # neither on the edges nor
  # on the diagonal, then the
  # pointer moves like a knight
  chessboard1(cr + 2, cc + 1, er, ec)
  chessboard1(cr + 1, cc + 2, er, ec)
  
  # If the pointer is on the
  # edges of the Square Matrix
  # next move can be horizontal
  # or vertical
  
  # This for loop is used to include
  # all the horizontal traversal cases
  # recursively
  for i in range(1, er + 1):
    if (cc == 0 or cr == 0 or cr == er or cc == ec):
      chessboard1(cr, cc + i, er, ec)
 
  # This for loop is used to include
  # all the vertical traversal cases
  # recursively
  for i in range(1, er + 1):
    if cc == 0 or cr == 0 or cr == er or cc == ec:
      chessboard1(cr + i, cc,er, ec)
  
  # If the pointer is on the
  # diagonal of the Square Matrix
  # next move is also diagonal.
  # For loop is used to include
  # all the diagonal traversal cases.
  for i in range(1, er + 1):
    if cr == cc or (cr + cc) == er:
      chessboard1(cr + i, cc + i, er, ec)
 
N = 3
chessboard1(0, 0, N - 1, N - 1)
print(count)
 
# This code is contributed by suresh07.




// C# program to count the number
// of ways to traverse the given
// N x N Square Matrix recursively
using System;
 
class GFG{
 
// Variable to store the total number
// of ways to traverse the Square Matrix
static int count = 0;
 
// Function to recursively
// count the total number
// of ways to traverse the
// given N x N Square Matrix
public static void chessboard1(int cr, int cc,
                               int er, int ec)
{
     
    // If the last index has been reached, then
    // the count is incremented and returned
    if (cr == er && cc == ec)
    {
        count++;
        return;
    }
 
    // If the last index cannot be reached
    if (cr > er || cc > ec)
    {
        return;
    }
 
    // If the current position is neither
    // on the edges nor on the diagonal,
    // then the pointer moves
    // like a knight
    chessboard1(cr + 2, cc + 1, er, ec);
    chessboard1(cr + 1, cc + 2, er, ec);
 
    // If the pointer is on the edges
    // of the Square Matrix next move
    // can be horizontal or vertical
 
    // This for loop is used to include all the
    // horizontal traversal cases recursively
    for(int i = 1; i <= er; i++)
    {
        if (cc == 0 || cr == 0 ||
           cr == er || cc == ec)
        {
            chessboard1(cr, cc + i, er, ec);
        }
    }
     
    // This for loop is used to include all the
    // vertical traversal cases recursively
    for(int i = 1; i <= er; i++)
    {
        if (cc == 0 || cr == 0 ||
           cr == er || cc == ec)
        {
            chessboard1(cr + i, cc, er, ec);
        }
    }
     
    // If the pointer is on the
    // diagonal of the Square Matrix
    // next move is also diagonal.
    // For loop is used to include
    // all the diagonal traversal cases.
    for(int i = 1; i <= er; i++)
    {
        if (cr == cc || cr + cc == er)
        {
            chessboard1(cr + i, cc + i,
                        er, ec);
        }
    }
}
 
// Driver code
public static void Main(string[] args)
{
    int N = 3;
     
    chessboard1(0, 0, N - 1, N - 1);
     
    Console.Write(count);
}
}
 
// This code is contributed by rutvik_56




<script>
 
// Javascript program to count the number
// of ways to traverse the given
// N x N Square Matrix recursively
 
   
// Variable to store the
// total number of ways to
// traverse the Square Matrix
let count = 0
     
// Function to recursively
// count the total number
// of ways to traverse the
// given N x N Square Matrix
function chessboard1(cr, cc, er, ec)
{
 
  // If the last index has been
  // reached, then the count is
  // incremented and returned
  if (cr == er && cc == ec)
  {
    count++;
    return;
  }
 
  // If the last index cannot
  // be reached
  if (cr > er || cc > ec)
  {
    return;
  }
 
  // If the current position is
  // neither on the edges nor
  // on the diagonal, then the
  // pointer moves like a knight
  chessboard1(cr + 2, cc + 1,
              er, ec);
  chessboard1(cr + 1, cc + 2,
              er, ec);
 
  // If the pointer is on the
  // edges of the Square Matrix
  // next move can be horizontal
  // or vertical
 
  // This for loop is used to include
  // all the horizontal traversal cases
  // recursively
  for (let i = 1; i <= er; i++)
  {
    if (cc == 0 || cr == 0||
        cr == er || cc == ec)
    {
      chessboard1(cr, cc + i, er, ec);
    }
  }
 
  // This for loop is used to include
  // all the vertical traversal cases
  // recursively
  for (let i = 1; i <= er; i++)
  {
    if (cc == 0 || cr == 0||
        cr == er || cc == ec)
    {
      chessboard1(cr + i, cc,er, ec);
    }
  }
 
  // If the pointer is on the
  // diagonal of the Square Matrix
  // next move is also diagonal.
  // For loop is used to include
  // all the diagonal traversal cases.
  for (let i = 1; i <= er; i++)
  {
    if (cr == cc || cr + cc == er)
    {
      chessboard1(cr + i, cc + i, er, ec);
    }
  }
}
   
 
// Driver Code
let N = 3;
chessboard1(0, 0, N - 1, N - 1,count);
document.write(count);
 
// This code is contributed by jana_sayantan.
</script>

Output :

18

Article Tags :