0

so I've a algorytmical problem where i need to find the biggest area of a certain type of pixels inside a 2D matrix with following conditions:

  1. Each pixel can be connected either diagonally or adjacently.
  2. The area is considered coherent only if it is surrounded by the other type of pixel

Pixel is considered an object with 3 fields:

int x,y;
String type;
boolean visited;

The input file is something like this:

00000000

01100100

00111000

00010000

00000000

Is someone able to tell me if BFS algorithm is a viable solution or should I try a different approach?

nvio0
  • 9
  • 1
  • 1
    This sounds like both an interview problem, and something that should go on https://cs.stackexchange.com/ because it's asking about algorithms and not code. – Water Oct 21 '19 at 18:55
  • So what you've got in your input file is the `xy`-implicit and `type` as one character? What is a sample answer? – Neil Oct 21 '19 at 19:19
  • @Neil Input is only a matrix of Strings depending on the String the type of pixel differs. The output should be the area of the biggest set of connected pixels of the same type. – nvio0 Oct 21 '19 at 20:28
  • @Water Ok, thank you for the response. I'll try there. – nvio0 Oct 21 '19 at 20:32
  • I'm still confused as to what `x` and `y` are. In your example, the biggest set of connected pixels would be all `0`s? In this case, BFS is probably the best bet. – Neil Oct 21 '19 at 21:31

1 Answers1

1

BFS would be a better option to go with. More specifically, try a flood fill approach. By making a smart use of visited variable, make sure that you visit every vertex at-most once, hence keeping the time complexity as minimum as possible.

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35