In a recent competition organized by “AmDocs” , I came across the following question : (The basic Idea of the question)
You are a given a matrix of fixed size 12×12.
You are given six line segments of length 6,5,5,4,3,2.
The matrix has empty spaces and filled spaces.
You have to return “Yes” Or “No” , whether all the 6 line segments can be fit into the matrix simultaneously or not.
The lines can be placed horizontally or vertically Only.
What algorithm should be used to solve this problem ? Packing ? Knapsack ?
I would map the problem to SAT and use a SAT solver. There is a pretty natural mapping. Define the variables:
(d is “right” or “down”)
First, iterate over all the segments and starting positions, and see which are viable given the starting matrix.
example, M:
if segment 1 is length 2, then
L_seg1_0_0_down = false, because it hits a filled space.Then, write clauses that prohibit two crossing segments. If segment 1 and segment 2 are both length 2, then we add the clause:
because if segment 1 uses coordinates (0,0), and (1,0), then segment 2 can’t use (1,0) also.
finally, add the condition that each segment must be used at least once:
for all the positions that seg1 can go. Then throw your favorite SAT solver at it.