<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 getKey (int i, int a, int r){
        return to_string(i) + "*" + to_string(a) + "*" + to_string(r);
    }
    const static int INF = 1e7;
    unordered_map<string, int> mp;
    int solve(int i, int a, int r, int n){
        string key = to_string(i) + "*" + to_string(a) + "*" + to_string(r);
        cout<<key<<'\n';
        if(i == n) return 0;
        if(i<0 || i>16) return INF;
        if(mp.find(key)!=mp.end()) return mp[key];
        int ans = INF;
        int sign = (r == 0) ? 1 : -1;
        ans = min(ans, 1 + solve(i + sign*pow(2, a), a+1, r, n));
        ans = min(ans, 2 + solve(i-1, 1, 1-r, n));
        return mp[key] = ans;
    }
    int racecar(int target) {
        mp.clear();
        // int ans = solve(0, 0, 0, target);
        int visit[2*(target+1)][17][2];
        memset(visit, 0, sizeof(visit));
        queue<vector<int>> q;
        q.push({0,0,0});
        visit[0][0][0] = 1;
        int level = 0;
        while(!q.empty()){
            int size = q.size();
            while(size --){
                vector<int> v = q.front(); q.pop();
                int i = v[0];
                int a = v[1];
                int r = v[2];
                if(i == target) return level;

                int sign = (r == 0) ? 1 : -1;
                int new_i = i + sign*pow(2, a);
                if(new_i>=0 && new_i<2*(target+1)) {
                    if(visit[new_i][a+1][r] == 0) q.push({new_i, a+1, r});
                    visit[new_i][a+1][r] = 1;
                }
                if(visit[i][0][1-r] == 0) {
                    q.push({i, 0, 1-r});
                    visit[i][0][1-r] = 1;
                }
            }
            level++;
        }
        return -1;
    }
};
83444