What is the logic for this
2d magical matrix
hi @kumarakash121005,
Some Points to ponder before discussing the Solution :
- Start from the 0-0 index of the 2 - D matrix( No step would be count hence, strength remain intact).
- Changing row wonβt take any strength.
- If at any time, the Strength become smaller than K ( threshold for piyush), then no need to traverse rest of the array, you can say no nothing else.
Algorithm
- Take input N, M, K, S.
- Take input 2 - D matrix of size N x M.
- Put a loop on the array starting from 0 - 0 index to (N - 1) - (M - 1).
- check if the Strength is lower than the threshold viz, K, print βNoβ and return.
- otherwise,
-
if character is β*β, add 5 to the strength.
-
else if, character is β.β , subtract 2 from the strength.
-
else, if character is β#β, break.
-
- If you are not in the last column, decrement strength by 1.
- After the loop, print βYesβ and strength separated by a new line.
its 2d magical matrix from graph section
@kumarakash121005 oh ok sorry
Its a simple dfs question
8 direction dfs
you need to count no of connected components
refer here https://www.geeksforgeeks.org/find-number-of-islands/