Wednesday, November 30, 2022
HomeSoftware DevelopmentExamine if adjoining cell of Begin could be reached visiting all cells...

# Examine if adjoining cell of Begin could be reached visiting all cells as soon as

Given two integers X and Y, which symbolize 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 beneath situations:

• Beginning of the trail is from the cell (1, 1).
• Ending of the trail is any cell adjoining to (1, 1).
• You need to traverse every cell of the matrix solely as soon as.

Notice: Right here 1 primarily based indexing is used.

Examples:

Enter:Â  X = 2, Y = 3
Output: YES
Rationalization:

Rationalization for enter check case

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
Output: NO
Rationalization: Â As there isn’t any adjoining cell of ( 1, 1 ) exists due to this fact, Output is NO.

Strategy: The issue could be solved primarily based on the next statement:

Statement:

Letâ€™s take some random test-cases.Â

Take a look at case 1: X = 4, Y = 4Â
Output: YES
Rationalization:

Rationalization for check case 1

Take a look at case 2: Â X=4, Y=5
Output: YES
Rationalization:

Rationalization for check case 2

Take a look at case 3: X = 1, Y =3Â
Output: NO
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 can have a path. Â  Â

Comply with the beneath steps to implement the concept:

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

Beneath is the implementation of the above method.

## Java

 `Â `Â  `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;` `Â `Â  `class` `GFG {` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``public` `static` `void` `essential(String args[])` `Â Â Â Â ``{` `Â Â Â Â Â Â Â Â ``lengthy` `X = ``2``, Y = ``2``;` `Â `Â  `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``System.out.println(Is_Path_Possible(X, Y));` `Â Â Â Â ``}` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``static` `String Is_Path_Possible(``lengthy` `X, ``lengthy` `Y)` `Â Â Â Â ``{` `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â ``if` `(X % ``2` `!= ``0` `&& Y % ``2` `!= ``0` `|| X == ``1` `&& Y > ``2` `Â Â Â Â Â Â Â Â Â Â Â Â ``|| Y == ``1` `&& X > ``2``) {` `Â `Â  `Â Â Â Â Â Â Â Â Â Â Â Â ``return` `"NO"``;` `Â Â Â Â Â Â Â Â ``}` `Â Â Â Â Â Â Â Â ``else` `{` `Â Â Â Â Â Â Â Â Â Â Â Â ``return` `"YES"``;` `Â Â Â Â Â Â Â Â ``}` `Â Â Â Â ``}` `}`

Time Complexity: O(1)
Auxiliary House: O(1) Â

RELATED ARTICLES