Search and Match Algorithm

I am trying to come up with an algorithm to do the following:

I have only 12 cells that I need to fill out until the program stops. I have 3 rows and each row has 4 columns.

As an example, let me illustrate this as in an airplane. So you have 3 rows, and each row has 4 columns, and you have windows / aisles. Each row will have a seat, a passage, a seat and a seat in the cabin (WA AW | exactly the same as the seat on the plane). At each iteration (a different set of passengers) there would be a certain number of passengers (from 1 to 12), and I need to move them closer as much as possible (Seat together). And I do this for the next group (each iteration) until the program stops (it stops when I finish with each group).

For example, I have 3 passengers (A, B and C), and A wants to sit in Window, B wants to sit in Aisle, and C wants to sit in Window. Assuming all seats (all 12) are available, I could place them as | A # BC | or | CB #A | and mark the seats dirty (so I no longer choose the same seats for the next passengers). And I do it for the next group (iteration). I am not sure if this is the right forum, but if anyone can advise me how I should do this, I would really appreciate it. Thank.

+3
source share
3 answers

, 12, 2^12 , ( ). , /.

:

IsSeatingPossible( mask, passengers ) =
    if ( no more passengers ) return true
    return IsSeatingPossible =
        new_mask = mask + brute force on passenger constraints
        if any IsSeatingPossible( new_mask, 
                      passengers - just processed passengers )

:

12 , , seat_i ( 2D (x,y) → 1D (x)). . , (A_1, A_2, .., A_k) , .

, , , - , . , , (A_1, A_2, .., A_k) (x_1, x_2, .., x_k). , N choose k, . , . (x_1, x_2, .., x_k), , (x_1, x_2, .., x_k) ( , ) , , , (x_1, x_2, .., x_k). ( , .)

. , , , , , . , memoization. O(N 2^N).

, N = 12, . , , , .

+2

12 , , , , . (2 ^ 12) k (0 <= k <= 12) k , . ( k .) : , ? Et cetera.

+1

This is an instance of the Knapsack type problem with previously known solutions.

+1
source

Source: https://habr.com/ru/post/1736704/


All Articles