Test if adjoining cell of Begin may be reached visiting all cells as soon as
Given two integers X and Y, which characterize the variety of rows and columns respectively of a matrix, the duty is to test whether or not there exists a path, Which follows all of the under situations:
- Beginning of the trail is from the cell (1, 1).
- Ending of the trail is any cell adjoining to (1, 1).
- It’s a must to traverse every cell of the matrix solely as soon as.
Word: Right here 1 based mostly indexing is used.
Enter: X = 2, Y = 3
Every cell is traversed as soon as, beginning cell is (1, 1) and ending cell is (2, 1), Which is adjoining to (1, 1). Therefore, all of the situations met.
Enter: X = 1, Y = 1
Rationalization: As there is no such thing as a adjoining cell of ( 1, 1 ) exists due to this fact, Output is NO.
Strategy: The issue may be solved based mostly on the next statement:
Let’s take some random test-cases.
Check case 1: X = 4, Y = 4
Check case 2: X=4, Y=5
Check case 3: X = 1, Y =3
Rationalization: No path is feasible that fulfill given constraints.
From all above check circumstances, We are able to conclude some situations:
- If each X and Y are odd, Then there exists no path.
- If (X = 1 && Y > 2) or (Y = 1 && X > 2), Then no path exists.
- All of the circumstances besides above mentioned 2( quantity 1 and quantity 2 ) circumstances may have a path.
Comply with the under steps to implement the concept:
- Test the situations on X and Y mentioned above.
- If the values match any of the primary two situations then no path is feasible.
- In any other case, there exists a path.
Under is the implementation of the above strategy.
Time Complexity: O(1)
Auxiliary Area: O(1)