jeudi 28 mai 2020

Given a periphery of island mark its interior in matrix (Algorithm)

We are given a matrix of size N which is completely filled with 0's and we are also given a list of coordinates which contains the coordinates of the periphery of an island. Now we have to mark the island (periphery + area that is part of the island) as 1's.

I am having trouble coming up with a solution to this problem, I thought of bfs/dfs but can't think of a way to implement this. (Please assume that there is 1 island only and the input is correct i.e. all input coordinates form a closed shape and are valid

N = 5
coordinates = 
0,2
1,1
1,3
2,0
2,4
3,1
3,3
4,2

So this is what output should look like ---

    0 1 2 3 4
  0 0 0 1 0 0
  1 0 1 1 1 0 
  2 1 1 1 1 1 
  3 0 1 1 1 0 
  4 0 0 1 0 0 

All the coordinates marked and the square boxes inside them marked which are a part of the island.

Aucun commentaire:

Enregistrer un commentaire