I need to arrange an uncertain number of Tetris pieces, into the smallest possible square. The pieces I get are static, so I am not allowed to turn them and the square is allowed to have holes inside.
I am supposed to write a program to do this, but I think this is more of a mathematical problem.
In total, there are 19 different possible pieces. I thought of starting to create the minimal square, in case all the pieces fit in perfectly.
The equation would be: $m = \sqrt{n\cdot 4}$ rounded up, with m being the minimal size and n being the number of pieces you have. All the pieces are typical Tetris pieces made out of 4 combined blocks.
My idea now was to try to fit all pieces into this square, and enlarge it by 1 if it doesn't fit. The obvious problem now is the algorithm, to place the pieces in the best way possible. For doing that I only came up with the idea to define certain pieces, which combine to a rectangle together. I the program would find all the pieces needed for this rectangle, it would know how to continue with those pieces.
This would actually work with some examples, but there are also a lot of possible combinations, where the smallest square possible wouldn't even contain one rectangle inside.
If anyone has an idea how to solve this, I'd really appreciate the help.
Edit: I am looking for an algorithm that actually places the pieces in the square.

A simple algorithm, which I have just now implemented, is the following:
I defined each piece as beginning with the coordinates of the topmost and leftmost part of the piece and then giving the relative coordinates of the 3 remaining "parts" of that piece in an array. An "L" piece, lying down with its "head" to the right, would then have coordinates (2,0), (2,1), (1,1), (0,1).
For each piece, find the coordinates at which its topmost and leftmost part could be located. For the above piece, in a square of size 4, the possible positions would be (2,0), (3,0), (2,1), (3,1), (2,2), (3,2). This assumes the square starts at (0,0).
At each moment, the function must know which positions in the square have already been taken.
If piece k can be placed, place it and attempt to place the next piece. If piece k cannot be placed, return and see if the previous piece can be moved to its next position.
If all K pieces have been placed, you are done. Otherwise (i.e. if the first piece has no other possible positions left to move to), no solution is possible.
The above algorithm is very simple but it does manage to solve N=7 cases within 5 seconds. When N=8, it can take over an hour. And the algorithm is done in Visual Basic for Excel, so other implementations could be much faster.
Below are some examples of its solutions:
If you are interested, I can post the code.
EDIT
Below is my attempt at pasting my code. I would appreciate any help in making it look better.