Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
0 down vote
favorite
This question was recently ask in a contest which is now over, but they don't share the editorials with us and this question is stuck in my mind and i am not able to get the answer in optimal way, anybody please explain the proper approach for this is very helpful-:(And if possible please explain like a editorial THNAKS)

"Satish has a combination safe that consists of multiple rotating dials that are placed next to each other. Each dial is etched with digits 0 through 9, in order. At any stage, exactly one of the digits on each dial is visible. The string formed by concatenating the visible digits of each dial is termed as the current combination. The visible digit on a dial can be changed by rotating the dial up or down. Rotating the dial up increases the digit by 1 i.e. 0 to 1, 1 to 2, and so on. The digits on a dial wrap around so a 9 is followed by 0. Rotating the dial down changes the digit in the reverse direction. We can rotate multiple adjacent dials at the same time. That is, in a single step we can take an arbitrary number of consecutive dials, and rotate all of them one step in the same direction (i.e., either all of them down, or all of them up). Satish is given the current combination (C) and the required combination (R) that unlocks the safe. Write a program to determine the minimum number of steps required to open the safe.
Example: Consider the case where C is “986” and R is “007”. This can be solved in two steps as follows:
Step-1: rotate all 3 dials up. “986” to “097”
Step-2: rotate 2nd dial up. “097” to “007”,

ex. 9 2 7 7 8 2 4 3 2 8 to 5 4 5 7 4 9 4 6 2 9 need 16 steps."

What I have tried:

I tried to solve it by brute force but it give TLE .
Posted
Updated 23-Jul-18 10:51am
v2
Comments
Patrice T 23-Jul-18 19:27pm    
Can you give a link to the contest ?

So, you've posted this on stack exchange: programming practices - Solving Safe Breaking - Software Engineering Stack Exchange[^]
And stack overflow: python - Solving Safe Breaking - Stack Overflow[^]
And how because they won't do your homework you bring it here.

Guess what? We don't do your homework either... it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Quote:
0 down vote

LOL
Quote:
This question was recently ask in a contest which is now over

These contest are especially designed to challenge your skills, so you got the answer: You are unable to solve it.
Quote:
I tried to solve it by brute force but it give TLE

brute force is never the solution of these contests, never. Nota: there is brute force and brute force, the brute force of a seasoned programmer can be in no way similar the a beginner brute force, so we have no idea what you did and how bad is your solution.
Quote:
anybody please explain the proper approach for this is very helpful

Just giving you a full blowup solution will never help you to learn anything. Because the necessary explanations would be hours just explain you how the solution works. The reason of our solutions is years of practice and of data structures / algorithm studying. We can't teach you those years of experience even in matter of days.
So you would endup with a magic solution that you can't understand.

The only useful help is an advice: take a sheet of paper and a pencil and solve by hand, the analyze how you solved the problem, how you did choices, it is basically your algorithm, because you didn't used brute force (testing every possible steps combinations), you did better.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900