<p>Your car starts at position <code>0</code> and speed <code>+1</code> on an infinite number line. Your car can go into negative positions. Your car drives automatically according to a sequence of instructions <code>&#39;A&#39;</code> (accelerate) and <code>&#39;R&#39;</code> (reverse):</p>

<ul>
	<li>When you get an instruction <code>&#39;A&#39;</code>, your car does the following:

	<ul>
		<li><code>position += speed</code></li>
		<li><code>speed *= 2</code></li>
	</ul>
	</li>
	<li>When you get an instruction <code>&#39;R&#39;</code>, your car does the following:
	<ul>
		<li>If your speed is positive then <code>speed = -1</code></li>
		<li>otherwise <code>speed = 1</code></li>
	</ul>
	Your position stays the same.</li>
</ul>

<p>For example, after commands <code>&quot;AAR&quot;</code>, your car goes to positions <code>0 --&gt; 1 --&gt; 3 --&gt; 3</code>, and your speed goes to <code>1 --&gt; 2 --&gt; 4 --&gt; -1</code>.</p>

<p>Given a target position <code>target</code>, return <em>the length of the shortest sequence of instructions to get there</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> target = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
The shortest instruction sequence is &quot;AA&quot;.
Your position goes from 0 --&gt; 1 --&gt; 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> target = 6
<strong>Output:</strong> 5
<strong>Explanation:</strong> 
The shortest instruction sequence is &quot;AAARA&quot;.
Your position goes from 0 --&gt; 1 --&gt; 3 --&gt; 7 --&gt; 7 --&gt; 6.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= target &lt;= 10<sup>4</sup></code></li>
</ul>

/*
Here we have to find minmum steps to target if we have two operations A and R.
We see we are given target and two directions and source so we can use BFS to find the shortest way.
We use a set visited to check if we have encountered that combination position+speed somewhere. 
We can use set of pair but that has high time complexity so we can make a key from pos+" "+ speed and we can insert that in set and use that to find visited.
Now we push source to the q, iterate till q is empty.
for each level in q we pop the first pair from q, and if pos == target we return distance.
now we calculate distance and speed if we go forward.

We should check that the new position should lie between 0 to 2*target, 
beacuse if we reach 2* target it means we have to cover target distance to reach target 
and it is same as starting from 0, for similar reason if we start from 0 or any number 
before that we will have to cover target distance to reach target. so the new pos should be between 0 and 2*target. we see if the combination does not exists in visited and it is between 0 and 2*target we push the pair <pos, speed> to the q.
*/
class Solution {
public:
    int racecar(int target) {
        queue<pair<int, int>> q;
        q.push({0, 1});
        unordered_set<string> visited;
        
        visited.insert("0_1");
        int steps = 0;
        while(!q.empty())
        {
            int size = q.size();
            for(int i=0;i<size;i++)
            {
                pair<int,int> curr = q.front();
                q.pop();
                int pos = curr.first, speed = curr.second;

                if(pos == target)
                return steps;

                pair<int, int> forward = {pos+speed, speed*2};
                string forwardKey = to_string(forward.first) + " " + to_string(forward.second);
                
                if(visited.find(forwardKey) == visited.end() && forward.first >0 && forward.first < target*2)
                {                
                    q.push(forward);
                    visited.insert(forwardKey);
                }

                int reverseSpeed = (speed>0)? -1: 1;
                pair<int, int> reverse = {pos, reverseSpeed};
                string reverseKey = to_string(reverse.first)+" "+ to_string(reverse.second);
                
                if(visited.find(reverseKey) == visited.end() && reverse.first >0 && reverse.first< target*2)
                {                
                    q.push(reverse);
                    visited.insert(reverseKey);
                }
            }
            steps++;
        }
        return -1;
        
    }
};
86990