algorithm - find all rectangular areas with certain properties in a matrix -


given n*m matrix possible values of 1, 2 , null:

  . . . . . 1 . .   . 1 . . . . . 1   . . . 2 . . . .   . . . . 2 . . .   1 . . . . . 1 .   . . . . . . . .   . . 1 . . 2 . .   2 . . . . . . 1 

i looking blocks b (containing values between (x0,y0) , (x1,y1)) that:

  • contain @ least 1 '1'
  • contain no '2'
  • are not subset of block above properties

example:

blocks

the red, green , blue area contain '1', no '2', , not part of larger area. there of course more 3 such blocks in picture. want find these blocks.

what fast way find these areas?

i have working brute-force solution, iterating on possible rectangles, checking if fulfill first 2 criteria; iterating on found rectangles, removing rectangles contained in rectangle; , can speed first removing consecutive identical rows , columns. there faster way.

you can somewhere between considering every rectangle, , clever solution.

for example, starting @ each 1 create rectangle , gradually expand edges outwards in 4 directions. stop when hit 2, record rectangle if (a) you've had stop in 4 directions, , (b) haven't seen rectangle before.

then backtrack: need able generate both red rectangle , green rectangle starting 1 near top left.

this algorithm has pretty bad worst cases, though. input consisting of 1s springs mind. need combined other cleverness, or constraints on input.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -