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:
- If the current position of the pointer is at the edges of the Square Matrix, then the next move can either be a vertical or a horizontal movement, any number of steps (Like a rook on the chessboard).
- If the current position of the pointer is at the diagonals of the Square Matrix, then the next move should also lie on the diagonal. (Like a bishop on the chessboard).
- If the current position of the pointer is at any other place than the above two, then the next possible step can be in any way like a knight moves on a chessboard but the row and column of the new position > row and column of the old position.
Examples:
Input: N = 2
Output: 3
Explanation:
The three possible ways are:
{0-0} – {0-1} – {1-1}
{0-0} – {1-0} – {1-1}
{0-0} – {1-1}
Input: 3
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:
- Base Case: The base case of the function is to check if the current pointer has reached the bottom right position in the Square Matrix. If reached, then the counter count which keeps the note of the total number of ways is incremented. If the last position cannot be reached, then the function should be stopped and shouldn’t be called for the next iteration. This is implemented as:
if (cr == er && cc == ec) {
count++;
return;
}
if (cr > er || cc > ec) {
return;
}
- Recursive Case: Given that three types of traversals are possible. Therefore, the recursive case is to recursively call the function by checking if the current position. If the current position is at the edges of the Square Matrix, then the pointer can be moved only horizontally or vertically. And for every subsequent vertical traversal, two more choices of horizontal and vertical traversals are possible. Therefore, in order to keep a track of the total number of ways, both of these are called recursively in a loop.
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);
}
}
- Apart from this, if the current pointer is at the diagonals, then the pointer can move diagonally. However, for every subsequent diagonal traversal, another set of subsequent diagonals are possible. Therefore, in order to keep a track of the total number of ways, a for loop is used to recursively call the function.
for (int i = 1; i <= er; i++) {
if (cr == cc || cr + cc == er) {
chessboard1(cr + i, cc + i,
er, ec);
}
}
- If none of the above cases is satisfied, then the next position can be moved in such a way that:
- new row is > old row.
- new column > old column.
- After executing the above function, the value stored in the count variable is the total number of ways to traverse the Square Matrix.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void chessboard1( int cr, int cc,
int er, int ec,
int & count)
{
if (cr == er && cc == ec)
{
count++;
return ;
}
if (cr > er || cc > ec)
{
return ;
}
chessboard1(cr + 2, cc + 1,
er, ec,count);
chessboard1(cr + 1, cc + 2,
er, ec,count);
for ( int i = 1; i <= er; i++)
{
if (cc == 0 || cr == 0||
cr == er || cc == ec)
{
chessboard1(cr, cc + i,
er, ec,count);
}
}
for ( int i = 1; i <= er; i++)
{
if (cc == 0 || cr == 0||
cr == er || cc == ec)
{
chessboard1(cr + i, cc,
er, ec,count);
}
}
for ( int i = 1; i <= er; i++)
{
if (cr == cc || cr + cc == er)
{
chessboard1(cr + i, cc + i,
er, ec, count);
}
}
}
int main()
{
int N = 3;
int count=0;
chessboard1(0, 0, N - 1, N - 1,count);
cout<<count;
}
|
Java
public class GFG {
static int count = 0 ;
public static void chessboard1(
int cr, int cc,
int er, int ec)
{
if (cr == er && cc == ec) {
count++;
return ;
}
if (cr > er || cc > ec) {
return ;
}
chessboard1(cr + 2 , cc + 1 , er, ec);
chessboard1(cr + 1 , cc + 2 , er, ec);
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);
}
}
}
public static void main(String[] args)
{
int N = 3 ;
chessboard1( 0 , 0 , N - 1 , N - 1 );
System.out.println(count);
}
}
|
Python3
count = 0
def chessboard1(cr, cc, er, ec):
global count
if cr = = er and cc = = ec:
count + = 1
return
if cr > er or cc > ec:
return
chessboard1(cr + 2 , cc + 1 , er, ec)
chessboard1(cr + 1 , cc + 2 , er, ec)
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)
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)
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)
|
C#
using System;
class GFG{
static int count = 0;
public static void chessboard1( int cr, int cc,
int er, int ec)
{
if (cr == er && cc == ec)
{
count++;
return ;
}
if (cr > er || cc > ec)
{
return ;
}
chessboard1(cr + 2, cc + 1, er, ec);
chessboard1(cr + 1, cc + 2, er, ec);
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);
}
}
}
public static void Main( string [] args)
{
int N = 3;
chessboard1(0, 0, N - 1, N - 1);
Console.Write(count);
}
}
|
Javascript
<script>
let count = 0
function chessboard1(cr, cc, er, ec)
{
if (cr == er && cc == ec)
{
count++;
return ;
}
if (cr > er || cc > ec)
{
return ;
}
chessboard1(cr + 2, cc + 1,
er, ec);
chessboard1(cr + 1, cc + 2,
er, ec);
for (let i = 1; i <= er; i++)
{
if (cc == 0 || cr == 0||
cr == er || cc == ec)
{
chessboard1(cr, cc + i, er, ec);
}
}
for (let i = 1; i <= er; i++)
{
if (cc == 0 || cr == 0||
cr == er || cc == ec)
{
chessboard1(cr + i, cc,er, ec);
}
}
for (let i = 1; i <= er; i++)
{
if (cr == cc || cr + cc == er)
{
chessboard1(cr + i, cc + i, er, ec);
}
}
}
let N = 3;
chessboard1(0, 0, N - 1, N - 1,count);
document.write(count);
</script>
|
Output :
18