tags:

views:

52

answers:

1

I have a set of numbers which I need to put on a rectangular grid as dense as possible (=the area of a grid is minimal). For example if I have to put numbers 16, 31, and 63, I can put them into 2x2-grid

1 6

3 0.

I think the best way to solve the problem for an arbitrary collection of numbers is to make some kind of net or tree which tries every permutation of numbers and the directions they can be read. I have difficulties to find a proper NextPosition()-algorithm which adds a number to a grid and tries every possible order of given numbers, positions where a given number starts, and direction of a given number. Can anyone suggest a method which tries every possible position?

A: 

Why are you doing this, what criteria determines which combination is the one you ultimately want (or are you working on all of them), and why isn't 63 part of your grid, or why isn't your grid 2x3?

Lastly, if your grids are so small and non-sparse, wouldn't you like to use an array?

Okay.
Then I would break every input number into digits, and for each first digit try to find that digit in the grid. Then search for the next digit, to see if it's adjacent grid spot. If you can't find any, you'd need to backtrack to the next match for the first digit. If there's no existing occurrences, you need to start again looking to add the last digit into an empty space next to the first digit, assuming numbers are two digits. If it's not possible, you need to either back track to the last number added, etc, or fail to say it's not possible to add this number.

dlamblin
I just like to think some programming problems and optimization. 63 can be read as diagonally and I want to read all numbers from a given set. 2x3 is not an optimal grid if the numbers 16, 31 and 63 should be found on a grid as a smaller one exists. Array is a possible data structure but my difficulty is to develop an algorithm to find a suitable next_position() function.
Jaska