<p>You are given an array of integers <code>distance</code>.</p>

<p>You start at the point <code>(0, 0)</code> on an <strong>X-Y plane,</strong> and you move <code>distance[0]</code> meters to the north, then <code>distance[1]</code> meters to the west, <code>distance[2]</code> meters to the south, <code>distance[3]</code> meters to the east, and so on. In other words, after each move, your direction changes counter-clockwise.</p>

<p>Return <code>true</code> <em>if your path crosses itself or </em><code>false</code><em> if it does not</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/21/11.jpg" style="width: 400px; height: 413px;" />
<pre>
<strong>Input:</strong> distance = [2,1,1,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> The path crosses itself at the point (0, 1).
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/21/22.jpg" style="width: 400px; height: 413px;" />
<pre>
<strong>Input:</strong> distance = [1,2,3,4]
<strong>Output:</strong> false
<strong>Explanation:</strong> The path does not cross itself at any point.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/21/33.jpg" style="width: 400px; height: 413px;" />
<pre>
<strong>Input:</strong> distance = [1,1,1,2,1]
<strong>Output:</strong> true
<strong>Explanation:</strong> The path crosses itself at the point (0, 0).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;=&nbsp;distance.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;=&nbsp;distance[i] &lt;= 10<sup>5</sup></code></li>
</ul>

#include <utility>
#include <math.h>
using namespace std;

class Solution {
private:
/*
l1 0,0 0,1   l2 -2,-2 2,-2
l1 0,1 -2,1  l2 -2,-2 2,-2
*/
    bool isCollision(pair<pair<int, int>, pair<int, int>> l1, pair<pair<int, int>, pair<int, int>> l2) {
        if (max(l1.first.first, l1.second.first) < min(l2.first.first, l2.second.first)) {
            return false;
        }
        if (min(l1.first.first, l1.second.first) > max(l2.first.first, l2.second.first)) {
            return false;
        }
        if (max(l1.first.second, l1.second.second) < min(l2.first.second, l2.second.second)) {
            return false;
        }
        if (min(l1.first.second, l1.second.second) > max(l2.first.second, l2.second.second)) {
            return false;
        }
        return true;
    }

    const pair<int, int> DIR[4] = {make_pair(0, 1), make_pair(-1, 0), make_pair(0, -1), make_pair(1, 0)};
public:
    bool isSelfCrossing(vector<int>& distance) {
        deque<pair<int, int>> posList;
        int d = 0;
        pair<int, int> pos = make_pair(0, 0);
        posList.push_back(pos);
        for (auto i = distance.begin(); i != distance.end(); i ++) {
            pair<int, int> nextPos = make_pair(pos.first + DIR[d].first * (*i), pos.second + DIR[d].second * (*i));
            for (int j = 0; j + 2 < posList.size(); j ++) {
                if (isCollision(make_pair(posList[j], posList[j + 1]), make_pair(pos, nextPos))) {
                    return true;
                }
            }
            posList.push_back(nextPos);
            pos = nextPos;
            if (posList.size() > 6) {
                posList.pop_front();
            }
            d = (d + 1) % 4;
        }
        return false;
    }
};

/*
posList 0,0 0,1 -2,1 -2,-2
pos -2,-2
nextPos 2,-2
d 3
distance 1,2,3,4
*i->4
j=0


*/
22500