r/dailyprogrammer • u/jnazario 2 0 • Jul 22 '15
[2015-07-22] Challenge #224 [Intermediate] Detecting Four Sided Figures
Description
I got this idea from the Mensa quiz, specifically question 17. It's a basic scanning challenge: can your program detect and count intersecting bounding boxes from an ASCII art input? A four-sided figure is an ASCII art rectangle. Note that it can overlap another one, as long as the four corners are fully connected.
Formal Inputs & Outputs
Your program will be given an ASCII art chart showing boxes and lines. - and | characters indicate horizontal and vertical lines, respectively, while "+" characters show intersections.
Your program should emit an integer, N, of how many unique four sided figures it found. Rectangles and squares both count.
Example Input
+----+
| |
+-------------------------+-----+----+
| | | |
| +-------------------+-----+ |
| | | | |
| | | | |
+-----+-------------------+-----+ |
| | | |
| | | |
+-------------------+-----+ |
| | |
| | |
| | |
+-----+----+
| |
| |
| |
+----+Example Output
For the above diagram your program should find 25 four sided figures.
Challenge Input
This one adds a bit to the complexity by throwing in some three sided figures. This should catch more naive implementations.
+-----------+
| |
| |
| |
| |
+-------------+-----------+-------------+
| | | |
| | | |
| | | |
| | | |
+-------------+-----------+-------------+
| |
| |
| |
| |
+-------------+-----------+-------------+
| | | |
| | | |
| | | |
| | | |
+-------------+-----------+-------------+
| |
| |
| |
| |
+-----------+Challenge Output
For the challenge diagram your program should find 25 four sided figures.
Finally
Have a good challenge idea? Consider submitting it to /r/dailyprogrammer_ideas
View all comments
Show parent comment
1
u/aaargha Aug 13 '15
C++
Finally got around to fixing my implementation of the O(n + v^(3/2)) variant. Credit goes to /u/HereBehindMyWall who came up with the idea, see his/her posts here and here for the reasoning behind it. As this is a re-work of my first submission, the first part is the same, we still construct the graph.
Finding a structure to store the open rectangles in was a bit tricky as we are only allowed O(v^(1/2)) operations per node, and we need to store O(v) records of open rectangles, many of which may need to be updated/deleted/created for each node. After I stopped corrupting the heap (god damn pointers, what was I thinking?), I found that some vectors and lists actually got me the performance needed.
I was pretty surprised when comparing the results to my previous solution, I really did not think that the difference would be that big.
Re-match in worst case land. Still on my i7-950:
100x100:
200x200:
400x400:
Code can be found here for those interested. Feedback and suggestions appreciated!