对于给定尺寸的所有的矩形搜索矩阵(座位选择块)有的、矩形、矩阵、座位

2023-09-10 23:16:18 作者:萌仔= ̄ω ̄=

所有,

我一直在试图找出如何选择座位单块中说,15票。

I have been trying to work out how to select say 15 tickets within a single block of seats.

修改:?问题是 - 如何找到给定尺寸的所有的矩形(说3×例如)的可用座位

EDIT: the problem is - how to find all rectangles of given dimensions (say 3x5 for example) of free seats?

下面是我的表,查询选择连续4个席位(或15或其他)这是好的...

The below is my table, and the query selects 4 consecutive seats (or 15 or whatever) which is fine...

但我想要做的就是选择说的15个席位,这可能会被分割在多个行,即3×5,但我希望他们被封锁起来,即

But what I want to do is select say 15 seats, these may be split over multiple rows, i.e. 3 x 5, but I would want them to be blocked together i.e.

row 9 ..(some seats)..[5 seats]..(some seats)..
row 8 ..(some seats)..[5 seats]..(some seats)..
row 7 ..(some seats)..[5 seats]..(some seats)..

即。它们将是所有在彼此的前面3行。 row9座位10至25日,row8席10至25日,row7席10至25。

I.e. they would be 3 rows all in front of each other. row9 seats 10 to 25, row8 seats 10 to 25, row7 seats 10 to 25.

可能还需要考虑是否席位块有不同的座位数,即一个角块可以是一个弧形,以拥有更多的席位在后面比前面。

Also may need to consider if a block of seats have varying number of seats i.e. a corner block may be in an arc to has more seats at the back than the front.

在ehnaceing的SQL或某种算法或一些PHP code形式的任何指导。我一直令人头大我的大脑大部分的一周了。

Any guidance in form of ehnaceing the SQL or some algorithm or some PHP code. I have been wracking my brain for most of the week now.

CREATE TABLE `seats` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `event_id` int(11) DEFAULT NULL,
  `performance` int(11) DEFAULT NULL,
  `block` int(11) DEFAULT NULL,
  `row` int(11) DEFAULT NULL,
  `seat` int(11) DEFAULT NULL,
  `status` int(10) DEFAULT 1,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

我查询最新 - 返回第X个座位块的组合

My query to-date - which returns combinations of blocks of X seats.

SELECT    a.event_id, a.performance, a.block,
          a.row, a.seat AS start_seat,
          a.seat + (4 - 1) AS end_seat,
          4 AS requested_seats,
          a.id AS start_allocation_id
FROM      seats a
          LEFT JOIN seats b ON
              a.event_id = b.event_id AND
              a.performance = b.performance AND
              a.block = b.block AND
              a.row = b.row AND
              a.seat < b.seat AND
              b.seat < a.seat + 4 AND
              b.status = 1
WHERE     a.status = 1 AND
          a.event_id = 1
GROUP BY  a.seat
HAVING COUNT(b.seat) + 1 = 4
ORDER BY performance

在此先感谢,需要更多的信息,请只问!

Thanks in advance, need more info please just ask!

推荐答案

这个问题是好多了mysql的外解决,在另一种语言。换句话说,有座位的矩阵,其中的一些被占用(灰色的):

This problem is much better solved outside mysql, in another language. In other words, you have a matrix of seats, some of which are occupied (grey ones):

和你想的找到给定尺寸的所有的矩形,说3x5的。您可以通过二传这样做非常有效的线性 O(N)时间算法(n为座位数):

and you want to find all rectangles of given dimensions, say 3x5. You can do this very efficiently by two pass linear O(n) time algorithm (n being number of seats):

1)在第一遍,按列去,从底部到顶部,并为每个座位,表示连续的议席数目可多达这一个:

1) in a first pass, go by columns, from bottom to top, and for each seat, denote the number of consecutive seats available up to this one:

重复,直到:

2)在第二遍,按行去,和看至少连续5号大于或等于3:

所以,最后,你会得到:

so, finally, you get:

而产生的解决方案:这些数字序列(绿色区域)的免费座位的2种可能的矩形3×5的顶部边缘

which yields the solution: these number sequences (green areas) are top edges of the 2 possible rectangles 3x5 of free seats.

的算法可以容易地提高到例如获得最大区域所有的矩形。或者,它可以用来寻找任何连续的区域(不仅长方形状)N个席位 - 只是,第二遍时,寻找数字连续序列总计为至少N

The algorithm could be easily enhanced to e.g. get all rectangles with maximum area. Or, it could be used to find any continuous regions (not only rectangle-shaped) of N seats - just, during the second pass, look for continuous sequence of numbers which sums up to at least N.

 
精彩推荐
图片推荐