SRM: 477 - DIV: II - 500 Problem - Islands

  • 1
Trivial problem with normal iteration, you can view the problem here
the main idea is to count the no of beaches that each cell has - if it is a land piece-.

We define a beach as a land that has a side neighbouring a water cell.
we iterate through all cells, if the current cell is a land peice we call the function countBeaches(i , j)
that counts the no of peace surrounding the indexed cell [ i ] [ j ].





we can define the function countBeaches(i, j) with two ways ;
the first one is two construct nested if statements that checks for every condition that exist for the 8 possible directions ( North, North West, North East, South , South East, South West)

regarding of course the difference between the odd lines and the even lines.
see the following picture to notice the difference




but another way to do this is to construct an array with the delta positions and add them to the original indexes to make the affect of translation.
that of course saves lots of coding time.


1 comment :

  1. msa ya mohmd :):) bs m3lesh mesh fhma odd w even lines ???

    ReplyDelete