<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>

class Solution {
public:
    string key(int a, int b)
    {
        return to_string(a) + "#" + to_string(b);
    }

    int racecar(int target) {
      queue<pair<int,int>> q;
      int speed = 1;
      int pos = 0;
      unordered_set<string> visited;
      q.push({pos, speed});
      int l = 0;
      int size = 1;

      while(!q.empty())
      {
        if(size==0)
        {
            l++;
            size = q.size();
        }

        pair<int,int> front = q.front();
        q.pop(); //0,1
        size--;
        
        if(visited.find(key(front.first, front.second)) == visited.end())
            visited.insert(key(front.first, front.second));
        else
            continue;

        if(front.first == target)
            return l;

        if((front.first < target && front.first > -target) || (front.first > target && front.second < 0) || (front.first <= -target && front.second > 0))
        {
            int nextPos = front.first + front.second; // 3,2
            int nextSpeed = 2*front.second;//3,4
        
            q.push({nextPos, nextSpeed}); //1,2
        }

        int speedAfterReverse = front.second < 0 ? 1:-1; //1,-1
        int posAfterReverse = front.first;

        q.push({posAfterReverse, speedAfterReverse});//1,-1
      }

        return -1;
    }

    // 4
    // 2 left, 1 or 3
    // 3
};
140184