<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) {
        int arr[20001][21][2][2];
        memset(arr, -1, sizeof arr);
        queue<vector<int>> crr;
        crr.push({0, 0, 0,0,0});
        while(!crr.empty()){

            auto bb=crr.front();
            // cout<<bb[0]<<"=="<<bb[1]<<"=="<<bb[2]<<"=="<<bb[3]<<"=="<<bb[4]<<"\n";
            crr.pop();
            int distance=bb[0];
            if(bb[2]==1)
                distance*=-1;
            if(distance==target)
                return bb[4];
            int speed=(1<<bb[1]);
            if(bb[3]==1)
                speed*=-1;
            int newD=distance+speed;
            int newS=bb[1]+1;
            int newDSign=0;
            if(newD<0)
                newDSign=1;
            if(abs(newD)<20001 && newS<21 && arr[abs(newD)][newS][newDSign][bb[3]]==-1){
                // cout<<abs(newD)<<"++"<<newS<<"++"<<newDSign<<"++"<<bb[3]<<"\n";
                arr[abs(newD)][newS][newDSign][bb[3]]=bb[4]+1;
                crr.push({abs(newD), newS, newDSign, bb[3], bb[4]+1});
            }
            if(bb[3]==0)
                speed=-1;
            else
                speed=1;
            int newSSign=0;
            if(speed<0)
                newSSign=1;
            if(arr[bb[0]][0][bb[2]][newSSign]==-1){
                // cout<<bb[0]<<"++"<<0<<"++"<<bb[2]<<"++"<<newSSign<<"\n";
                arr[bb[0]][0][bb[2]][newSSign]=bb[4]+1;
                crr.push({bb[0], 0, bb[2], newSSign, bb[4]+1});
            }
        }
        return 0;
    }
};
267849