
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Race Car in C++
Suppose we have a car, that starts at position 0 and speed +1 on an infinite number line. The car runs automatically according to a sequence of instructions A: for accelerate and R − for reverse. When we get an instruction "A", our car does the following −
- position := position + speed, then speed = speed * 2.
When we get an instruction "R", our car does the following −
- if speed is positive then speed = -1,
- otherwise speed = 1.
For example, after executing the instructions "AAR", our car goes to positions 0->1->3->3, and speed goes to 1->2->4->-1.
Now suppose we have some target position; we have to find the length of the shortest sequence of instructions to get there.
So, if the input is like target = 6, then the output will be 5 as one of the possible sequence is AAARA, the position sequence will be 0->1->3->7->7->6
To solve this, we will follow these steps −
- Define one set visited
- Define one queue q
- insert { 0, 1 } into q
- for initialize level := 0, when not q is empty, update (increase level by 1), do −
- for initialize k := size of q, when k > 0, update (decrease k by 1), do −
- Define one pair curr := front element of q, delete element from q
- if first of curr is same as target, then −
- return level
- forward := first of curr + second of curr
- forwardSpeed := second of curr * 2
- key := convert forward to string concatenate " * " concatenate convert forwardSpeed to string
- if forward > 0 and |forward - target| < target and not key is not in visited, then −
- insert key into visited
- insert { forward, forwardSpeed } into q
- key := convert first of curr to string concatenate " * " concatenate 0 when second of curr > 0, otherwise -1
- if curr.first > 0 and |target - curr.first| < target and key is not in visited, then −
- insert key into visited
- insert { curr.first, (if curr.second > 0, then -1, otherwise 1 }) into q
- for initialize k := size of q, when k > 0, update (decrease k by 1), do −
- return -1
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int racecar(int target) { unordered_set < string > visited; queue < pair <int ,int> > q; q. push({0, 1}); for(int level = 0; !q.empty(); level++){ for(int k = q.size(); k > 0; k-- ){ pair <int, int> curr = q.front(); q.pop(); if(curr.first == target) return level; int forward = curr.first + curr.second; int forwardSpeed = curr.second * 2; string key = to_string(forward) + "*" + to_string(forwardSpeed); if(forward > 0 && abs(forward - target) < target && !visited.count(key)){ visited.insert(key); q.push({forward, forwardSpeed}); } key = to_string(curr.first) + "*" + to_string(curr.second > 0 ? - 1: 1); if(curr.first > 0 && abs(target - curr.first) < target && !visited.count(key)){ visited.insert(key); q.push({curr.first, curr.second > 0 ? - 1: 1}); } } } return -1; } }; main(){ Solution ob; cout << (ob.racecar(6)); }
Input
6
Output
5
- Related Articles
- Program to find the head start in a race in C++
- Race and Crime
- Car Fleet in C++
- Race-around Condition in JK Flip-flop
- Count passing car pairs in C++
- What is a rat-race junction in microwaves?
- Role of Gender and Race in Shaping Personality
- Race Condition, Critical Section and Semaphore
- Automate Mobile Testing to Win The Race
- Nuclear Arms Race-Post World War II
- Four cars A, B, C, and D are moving on a leveled road. Their distance versus time graphs is shown in the figure. Choose the correct statement$(a)$. Car A is faster than car D.$(b)$. Car B is the slowest.$(c)$. Car D is faster than car C.$(d)$. Car C is the slowest."
- How to Fix Race Condition using Atomic Functions in Golang?
- Car Pooling in Python
- The distance of the race is 22.5 km. The cyclist has completed 10 of the race. What is the distance covered by the cyclist?
- Four cars A, B, C and D are moving on a levelled, straight road. Their distance-time graphs are shown in the given figure. Which of the following is the correct statement regarding the motion of these cars?$(a)$. car A is faster than car D.$(b)$. car B is the slowest$(c)$. car D is faster than the car C$(d)$. car C is the slowest"\n
