<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:
    struct TupleHash {
        template <typename T> size_t operator()(const T& tuple) const {
            auto hash1 = hash<tuple_element_t<0, T>>{}(get<0>(tuple));
            auto hash2 = hash<tuple_element_t<1, T>>{}(get<1>(tuple));
            return hash1 ^ (hash2 << 1);
        }
    };

    struct TupleEqual {
        template <typename T>
        bool operator()(const T& lhs, const T& rhs) const {
            return get<0>(lhs) == get<0>(rhs) && get<1>(lhs) == get<1>(rhs);
        }
    };

    int racecar(int target) {
        unordered_set<tuple<int, int, int>, TupleHash, TupleEqual> dp;
        queue<tuple<int, int, int>> BFS;
        auto s = make_tuple(0, 1, 0);
        dp.insert(s);
        BFS.push(s);
        int lim = target * 2;

        while (true) {
            auto state = BFS.front();
            int pos = get<0>(state);
            int speed = get<1>(state);
            int count = get<2>(state);
            BFS.pop();
            if (pos == target)
                return count;
            auto pushA = make_tuple(pos + speed, speed * 2, count + 1);
            if (dp.find(pushA) == dp.end() && abs(speed) < lim) {
                dp.insert(pushA);
                BFS.push(pushA);
            }
            if (speed < 0) {
                auto pushR = make_tuple(pos, 1, count + 1);
                if (dp.find(pushR) == dp.end()) {
                    dp.insert(pushR);
                    BFS.push(pushR);
                }
            }
            else{
                auto pushR = make_tuple(pos, -1, count + 1);
                if (dp.find(pushR) == dp.end()) {
                    dp.insert(pushR);
                    BFS.push(pushR);
                }
            }
            
        }
        return 0;
    }
};
182739