2d magical matrix

What is the logic for this

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,
    1. if character is β€˜*’, add 5 to the strength.

    2. else if, character is β€˜.’ , subtract 2 from the strength.

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

code --> https://ide.codingblocks.com/s/660601

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/