Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Dorothy has been caught up in a cyclone and has reached the Munichkin kingdom in the magical land of Oz . On her way, she

Dorothy has been caught up in a cyclone and has reached the Munichkin kingdom in the magical land of Oz. On her way, she comes across a yellow brick road across which, she can see the towering castle of the great wizard. The brick road, however, requires a test of the mind to cross it. It has been set up by the wizard so that unworthy people may not enter his castle.
The yellow brick road can only be crossed in the following way:
There are N bricks and each brick is labeled with a random number from 0 to 9.
Dorothy is on the 1th brick and needs to reach the Nth brick.
If she is on the ith brick with value V, then she can move to the i+1th brick, i-1th brick or any brick with the same value V.
There are N bricks and each brick is labeled with a random number from 0 to 9.
Dorothy is on the 1th brick and needs to reach the Nth brick.
If she is on the it brick with value V, then she can move to the i+1th brick, i-1th brick or any brick with the same value V.
Help Dorothy cross the golden brick road in the smallest number of moves.
Input format:
Line 1: N, number of bricks.
Line 2: Space separated array representing the numbers on each brick.
Output Format:
Your program should print the smallest number of moves to cross the brick road.
Examples:
Example 1:
Line 1: 5
Line 2:12341
Output: 1
Explanation:
Since the value at Oth position is same as at 4th position, Dorothy directly moves to the end position. The number of moves is 1.
Example 2:
Line 1: 6
Line 2:123413
Output: 2
Explanation:
The shortest path is position 0th-4th-5th. The number of moves is 2.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

Students also viewed these Databases questions

Question

a sin(2x) x Let f(x)=2x+1 In(be)

Answered: 1 week ago