3

I'm looking for an algorithm to solve this problem other than reinforcement learning and those that take a long time to run.

On the x-y plane, n rectangles are placed. How many rectangles can a straight line pass through at maximum?

Input

The number of rectangle is n.
The upper left coordinate of number i rectangle is given by x[i], y[i].
Its width and height are given by w[i], h[i].

Example

n
x[0] y[0] w[0] h[0]
x[1] y[1] w[1] h[1]
・
・
x[n-1] y[n-1] w[n-1] h[n-1]

Rule

  • 0 < n <= 1000
  • Each rectangle is placed in 0 <= x <= 10000, 0 <= y <= 10000.
  • The coordinate must be integer.
  • Width and height must be bigger than 1 and integer as well.
  • Rectangles can overlap.
  • A straight line can pass throgh vertexs of rectangles.
  • The straight line doesn't have to pass through (0, 0).

Hints

Case1:

Input
4
0    0    1    1
9999 0    1    1
0    9999 1    1
9999 9999 1    1

Output
2

Case2:

Input
6
2    1    4    3
1   10    1    3
5    7    5    4
8    8    3    2
13   4    3    1
17   1    1   14

Output
5

1 Answers1

0

I will share with you one approach. What you can do is that you can calculate center of each rectangle. You will get as many centers as there are rectangles. For eg. for 100 rectangles you will get 100 centers (points) on the xy plane.

Now with these 100 points you can construct a best fit line. You can learn about best fit line here and here.

The line so obtained will be passing through most of the rectangles.

Syed Waris
  • 1,056
  • 1
  • 11
  • 16
  • Sounds promising for rectangles with similar size *or* "uniform" distribution. (Would weighting the points (by rectangle circumference rather than area) help cases with many small/"hard to hit" rectangles and numerous bigger ons?) – greybeard Mar 07 '19 at 12:08