P
P
Prodion2017-09-14 00:26:11
JavaScript
Prodion, 2017-09-14 00:26:11

How to write a complex algorithm?

There is an array:

var level = [
    [
        'один', // 4 буквы
        'два', // 3 буквы
        'три', // 3 буквы
        'четыре' // 6 букв

        // и того 16 букв
    ]
];

There is a task: to parse the words from level[0] into separate letters and display them in the grid, each letter is a separate cell of the grid. The total number of letters may vary. For example: 16 (as in the example), 20, etc. I can’t understand how to parse words into letters and how to formulate the grid correctly, or rather how to make it dependent on the number of letters. For example: 16 letters = 4*4, 20 letters = 4*5, etc. That is, to be able to optionally specify the proportions of the grid, depending on the number of letters.
Maybe I initially have not the right idea about the implementation. I have never solved such complex problems and now it is very difficult to understand which side to take. I really hope for any help, perhaps you can suggest an elegant implementation.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Labunsky, 2017-09-14
@Labunsky

Not a complicated algorithm. You take a table and start from left to right:
1. You take a word;
2. You choose the first free cell that comes across (when walking from left to right, top to bottom, especially for longclaps . First this is a corner, then how it goes);
3. You choose a random direction of movement from the available ones (there must be at least one free cell in this direction);
4. You start spelling the word using depth-first traversal (cell cells are graph vertices, edges are formed between two free cells connected at a right angle), with a priority to maintain direction (no turns, except when resting against a wall);
5. If there are more words - take them and go to step 1.

X
xmoonlight, 2017-09-14
@xmoonlight

For example: 16 letters = 4*4, 20 letters = 4*5, etc. That is, to be able to optionally specify the proportions of the grid, depending on the number of letters.
The square root of the number of letters, or the closest square root of the given number to be extracted (whose modulus difference is smaller):
Condition: [total number of letters = 20 ] <= x^2,
i.e. we want to fit the words into a max-square area.
check: x=2 => x^2=4 => not enough.
check: x=3 => x^2=9 => not enough.
check: x=4 => x^2=16 => not enough.
check: x=5 => x^2=25 => a lot. stop.
Now, we compare:
I.e., we choose the maximum approximation to the desired one, namely, the number 4 and the square 4x4.
Then, we “add” (extend) one side of the square to the desired number (make a rectangle), a multiple of multiplying the short side by the newly created elongated one so that all the letters can fit.
So the grid will be exactly: 4x5 (4x4+4=20)
Then, fill in everything with the necessary words, and what remains - fill in the word, with the required (remaining) number of letters.
If perhaps you need to somehow stretch it, but the main thing is that everything fits "back to back", then you can immediately search through the covered area: S = X * Y and the minimum side length, a multiple of the number of all letters, and start from this ...
T. That is, the ultimate task is to find the GCD for pairs of values: S and X, and for S and Y, where S is the required number of letters for all words.
If the number of letters is prime, then it is done in the same way as it is written above: by the nearest addition to the product X * Y, exceeding the total number of letters by as few additional cells of the grid as possible (though it’s easier - find the word of the right size and replace it with a short one, or always follow the REQUIRED multiplicity of the total number of all letters in words).
You can stack words in a grid in a spiral from any point of the outer perimeter to a complete ring and then, shift to the center by 1 cell and again in the same direction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question