<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:
   
   /* int racecar(int target) {
        queue<pair<string,pair<int,long long>>>q;
        q.push({"",{0,1}});
        int ans=0;
      //  map<pair<int,long long>,int>h;
        map<string,int>h;
        string s1=to_string(0)+" "+to_string(1);
        h[s1]=1;
        h[{0,1}]=1;
        while(!q.empty()){
            int n=q.size();
            for(int i=0;i<n;i++){
                string s=q.front().first;
                int a=q.front().second.first;
                long long b=q.front().second.second;
                if(a==target){
                    return s.size();
                }
                q.pop();
                string s1=to_string(a+b)+" "+to_string(b*2);
                if(h.count(s1)==0){
                    q.push({s+"A",{a+b,b*2}});
                    h[s1]=1;
                }
                long long c=1;
                if(b>0){
                    c=-1;
                }
                s1=to_string(a)+" "+to_string(c);
                if(h.count(s1)==0){
                    q.push({s+"A",{a,c}});
                    h[s1]=1;
                }
            }
        }
        return 0;
    }*/
   
int racecar(int target) {
    queue<pair<int, long long>> q;  // No need to store the string path
    q.push({0, 1});  // Initial position and speed
    map<pair<int, long long>, int> h;  // Using pair<int, long long> directly
    h[{0, 1}] = 1;
    int ans = 0;
    
    while (!q.empty()) {
        int n = q.size();
        for (int i = 0; i < n; ++i) {
            int a = q.front().first;  // Current position
            long long b = q.front().second;  // Current speed
            q.pop();
            
            if (a == target) {
                return ans;  // Return the number of steps taken
            }
            
            // "A" command: move to position (a+b) with speed (b*2)
            int new_a = a + b;
            long long new_b = b * 2;
            if (!h.count({new_a, new_b}) && new_a > 0 && new_a < 2 * target) {
                q.push({new_a, new_b});
                h[{new_a, new_b}] = 1;
            }
            
            // "R" command: reverse direction
            new_a = a;
            new_b = b > 0 ? -1 : 1;
            if (!h.count({new_a, new_b})) {
                q.push({new_a, new_b});
                h[{new_a, new_b}] = 1;
            }
        }
        ans++;  // Increment the step counter
    }
    
    return 0;  // Should not reach here
}

};
94083