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

- 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.

And the pointer is moved in the form of a knight. Therefore, this is implemented as:

chessboard1(cr + 2, cc + 1, er, ec); chessboard1(cr + 1, cc + 2, er, ec);

- 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:

`// 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); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

18

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count the number of ways to traverse a Matrix
- Count of ways to traverse a Matrix and return to origin in K steps
- Count number of ways to reach a given score in a Matrix
- Traverse matrix in L shape
- Traverse a given Matrix using Recursion
- Traverse the matrix in Diagonally Bottum-Up fashion using Recursion
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Construct a Matrix of size NxN with values in range [1, N^2] as per given conditions
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to make exactly C components in a 2*N matrix
- Number of ways to reach (X, Y) in a matrix starting from the origin
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Number of ways to reach (M, N) in a matrix starting from the origin without visiting (X, Y)
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count number of ways to partition a set into k subsets
- Count ways to express a number as sum of powers
- Count number of ways to arrange first N numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.