S
S
Stanislav Ushakov2018-09-25 16:53:22
Algorithms
Stanislav Ushakov, 2018-09-25 16:53:22

The speed of the object grows exponentially with a step of 2. There is a reverse command. How to get to the right point?

The essence of the problem: there is a task, there is my implementation, which produces not quite optimal results.
Task:
An object moves along a single line on a surface. The line is divided into blocks, and each block is numbered, including blocks with negative numbers. The starting point is in block (position) 0. The initial speed of the object in this block is "+1".
The object understands 2 commands:
* "A" - acceleration at which the position changes by the speed value (position += speed), and the speed doubles (speed *= 2);
* "R" - reverse motion, in which the object turns on the spot and slows down to "+1" or "-1", i.e. the sign is reversed.
For example, if we pass the instruction "AAR", then the object moves in blocks 0->1->3->3, and its speed changes as 1->2->4->(-1).
They hired a person who will write such instructions, but at the same time it is necessary to check whether his instructions are optimal in size. In this problem, it is necessary to determine the length of the shortest sequence of instructions that will lead us to a predefined block.
The input is: an integer denoting the number of the block into which the object should fall.
Output: a natural number indicating the minimum number of instructions in an instruction that allows reaching the given block.
Example 1.
Input: 3
Response: 2
Explanation: the most optimal instruction is "AA", which allows you to move the object in blocks 0->1->3.
Example 2.
Input: 6
Answer: 5 Are there ways to solve this problem using some well-known algorithms and / or mathematical approaches.
Explanation: the most optimal instruction is "AAARA", which allows you to move the object in blocks 0->1->3->7->7->6.
Very important: the object does not start moving on its own, but only responds to commands.
--------------------------------
It was possible to implement finding instructions that will lead the object to the goal based on the incoming value with the target point itself. But the instructions are not optimal.
Can this task be used as a test task for a junior programmer?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2018-09-25
@dimonchik2013

smoke graphs

R
Rsa97, 2018-09-27
@Rsa97

I suspect (but do not guarantee) that the problem belongs to the class of NP-complete, which means that its exact solution can only be obtained by exhaustive enumeration of options.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question