<p>An array is <strong>squareful</strong> if the sum of every pair of adjacent elements is a <strong>perfect square</strong>.</p>

<p>Given an integer array nums, return <em>the number of permutations of </em><code>nums</code><em> that are <strong>squareful</strong></em>.</p>

<p>Two permutations <code>perm1</code> and <code>perm2</code> are different if there is some index <code>i</code> such that <code>perm1[i] != perm2[i]</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,17,8]
<strong>Output:</strong> 2
<strong>Explanation:</strong> [1,8,17] and [17,8,1] are the valid permutations.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,2,2]
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 12</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int numSquarefulPerms(vector<int>& nums) 
    {
        auto isPerfectSquare = [](int n) -> bool 
        {
            int root = static_cast<int>(sqrt(n));
            return root * root == n;
        };

        auto check = [&](const vector<int>& nums, int length, int total = 2) -> bool 
        {
            int count = 0;
            for (int i = 0; i < length - 1; ++i) 
            {
                if (!isPerfectSquare(nums[i] + nums[i + 1])) count += 1;
                if (count == total) return false;
            }
            return true;
        };

        int n = nums.size();
        set<vector<int>> seen;
        set<vector<int>> res;
        queue<pair<vector<int>, int>> q;
        q.push({vector<int>(n, 0), 0});

        while (!q.empty()) 
        {
            auto [cur, i] = q.front(); q.pop();

            if (i == n) 
            {
                if (check(cur, i, 1)) res.insert(cur);
                continue;
            }

            for (int j = 0; j <= i; ++j) 
            {
                vector<int> permutations = cur;
                permutations.insert(permutations.begin() + j, nums[i]);
                if (seen.find(permutations) == seen.end() && check(permutations, i + 1)) 
                {
                    seen.insert(permutations);
                    q.push({permutations, i + 1 });
                }
            }
        }

        return res.size();
    }
};


// This is TLE
#if 0
class Solution {
public:
    int numSquarefulPerms(vector<int>& nums) 
    {
        auto isPerfectSquare = [](int n) -> bool 
        {
            int root = static_cast<int>(sqrt(n));
            return root * root == n;
        };

        auto check = [&](const vector<int>& nums, int length, int tol = 2) -> bool 
        {
            int count = 0;
            for (int i = 0; i < length - 1; ++i) 
            {
                if (!isPerfectSquare(nums[i] + nums[i + 1])) count += 1;
                if (count == tol) return false;
            }
            return true;
        };

        int n = nums.size();
        set<vector<int>> res;
        queue<pair<vector<int>, vector<bool>>> q;
        q.push({{}, vector<bool>(n, false)});

        while (!q.empty()) 
        {
            auto [cur, used] = q.front(); q.pop();

            if (cur.size() == n) 
            {
                if (check(cur, n, 1)) res.insert(cur);
                continue;
            }

            for (int i = 0; i < n; ++i) 
            {
                if (!used[i]) 
                {
                    used[i] = true;
                    cur.push_back(nums[i]);
                    if (check(cur, cur.size())) 
                    {
                        q.push({cur, used});
                    }
                    cur.pop_back();
                    used[i] = false;
                }
            }
        }

        return res.size();
    }
};
#endif
13300