Friday 15 July 2011

algorithm - How can I find hole in a 2D matrix? -


I know that the title seems obscure and for this reason I have attached an image that will help to understand the problem clearly By form I need to find a hole inside the white area. A hole is defined as one or several cells, where the value is '0' within the white area, this means that cell '1' (for example We can mark three holes 1, 2 and 3.). I have come up with a very simple solution: 1. Find the entire matrix for cells with the value '0' 2. Run DFS (flooding) when facing such a cell (black one) and check that Can we touch the boundary of the main rectangular area 3. If we can touch the boundary during DFS then this is not a hole and if we can not reach the limit then it will be treated as a hole.

Now, this solution works But I was wondering if any other efficient / rapid solution to this problem.

Please tell me your thoughts Thank you.

Enter image details here

< Div class = "post-text" itemprop = "text">

With flood, you already have : Run with the matrix of your matrix and flood it, that is, all 2 (loaded black) from zero (black) and 3 people (full white); Ignore the first 2 and 3 coming from the flood and the flood.

For example, with your matrix, you start from the upper left, and there are floods in the black area with 11 regions. Then you move right, and move a black cell that you are just filled again and find a white area, very large (actually all white in your matrix). Fill it then you take it right again, another fresh black area that runs along the upper and right borders. Moving around, now you get two white cells which you had filled up before and left them. And finally you find the black area on the edge of the border below.

Then Scan the Matrix: In all areas that you are coloring, they are 0 hole in black

Another method, like "arrested flooding" / Strong>

Run around the boundary of the first matrix. Where you find "0", you are set to "2" where you find "1", you are set to "3".

Now run around the new internal border (the cells that touch the scanned range you have), the cells that touch 2 cells 2, the cells which touch 3 cells become 3.

Once you have to scan once, once in a clockwise clockwise, "outside" and "first" outside the current cell is because you can find something like this:

  22222222222333333 2AB11111111C 31   

Actually 1. You check your neighbors and you have 1 (but it is useless to check ) because you have not processed it so you can not know whether it should be 1 or 3 - which Is the case), 2 and 2a2 can not change a 1, then cell A remains 1. It goes with the same cell b which is again 1, and so on. When you come into cell C, you realize that it is 1, and it has 3 neighbors, so it toggles 3 ... but all cells should be toggled from now on .

The simplest, though not the most efficient, though is the way to deal with it, to scan the cells clockwise, which gives you incorrect answer (C and D1

  2222222222333333 211111111DC333333 33   

And then scan them again towards the counter-clockwise Now when you come in cell C, Has 3 neighbors nearby and Toggles 3. After this, you inspect cell D, which has the previous neighbor C, which is now 3, hence toggles again from D3. Finally you get the right answer.

  2222222222333333 23333333333333333 33   

And for each cell, you go to the clock direction of two neighbors, in a counterclockwise direction Apart from that, one of the neighbors is actually the cell you examine , so that you can keep it in a ready variable and save a matrix access.

If you find that you can stop the process without having to scan once a single cell, you can stop the process by checking 2 (W * H) Operations will cost, so it is only really meaningful if many are of holes.

In maximum W * H * 2 steps, you should be done.

You also want to check the parceling algorithm and try to optimize it.

No comments:

Post a Comment