Problem:
8 Queens Problem
The goal of 8 Queens Problem is to put eight queens on a chess-board such that none of them threatens any of others. A queen threatens the squares in the same row, in the same column, or on the same diagonals as shown in the following figure.
For a given chess board where $k$ queens are already placed, find the solution of the 8 queens problem.
Input
In the first line, an integer $k$ is given. In the following $k$ lines, each square where a queen is already placed is given by two integers $r$ and $c$. $r$ and $c$ respectively denotes the row number and the column number. The row/column numbers start with 0.
Output
Print a $8 \times 8$ chess board by strings where a square with a queen is represented by '
Q
' and an empty square is represented by '
.
'.
Constraints
There is exactly one solution
Sample Input 1
2
2 2
5 3
Sample Output 1
......Q.
Q.......
..Q.....
.......Q
.....Q..
...Q....
.Q......
....Q...

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <functional>
#include <stack>

using namespace std;
using l = long long;
using ul = unsigned long long;

const int inf =  2147483647;         // 2e9     1 << 30
const l INF = 9223372036854775807;  // 9e18    1LL << 60

#define r(i, n) for (l i = 0; i < n; ++i)
#define r1(i, n) for (l i = 1; i < n; ++i)
#define r0(i) for (l i = -1; i < 2; ++i)
#define pll pair<l, l>


void YesNo(bool s=false) {
    if (s) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

void nans(l ans) {
    if (ans==INF) cout<<-1<<endl;
    else cout<<ans<<endl;
}

struct UnionFind {
    vector<l> parent, rank, size;
    l tree_count;

    UnionFind(l n) : parent(n), rank(n, 0), size(n, 1), tree_count(n) {
        for (l i = 0; i < n; i++) parent[i] = i;
    }
    
    l find(l x) {
        if (parent[x] == x) return x;
        return parent[x] = find(parent[x]);  // パス圧縮
    }
    bool unite(pll xy) {
        l rx = find(xy.first);
        l ry = find(xy.second);
        if (rx == ry) return false;
        if (rank[rx] < rank[ry]) swap(rx, ry);
        parent[ry] = rx;
        size[rx] += size[ry];
        if (rank[rx] == rank[ry]) rank[rx]++;

        tree_count--;

        return true;
    }
    
    bool same(pll xy) {
        return find(xy.first) == find(xy.second);
    }
    
    l getSize(l x) {
        return size[find(x)];
    }
};


int main() {
    l n;cin>>n;

    vector<string> f(8, "........");

    r(i, n) {
        l x, y;cin>>x>>y;

        f[x][y]='Q';
    }

    if (n==8) {
        r(i, 8) cout<<f[i]<<endl;
        return 0;
    }

    function<void(vector<string>, l, l, l)> dfs=[&](vector<string> sp, l x, l y, l cnt) {
        sp[x][y]='Q';

        if (++cnt==8) {
            r(i, 8) cout<<sp[i]<<endl;

            exit(0);
        }

        r(i, 8) r(j, 8) {
            bool flag=false;

            r0(dy)r0(dx) {
                r(k, 8) {
                    l nx=i+dx*k, ny=j+dy*k;

                    if (nx<0 || nx>=8 || ny<0 || ny>=8) continue;

                    if (sp[nx][ny]=='Q') flag=true;
                }
            }

            if (flag) continue;

            dfs(sp, i, j, cnt);
        }
    };

    r(i, 8) r(j, 8) {
        bool flag=false;

        r0(dy)r0(dx) {
            r(k,8) {
                l nx=i+dx*k, ny=j+dy*k;
                if (nx<0 || nx>=8 || ny<0 || nx>=8) continue;

                if (f[nx][ny]=='Q') flag=true;
            }
        }

        if (flag) continue;

        dfs(f, i, j, n);
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3440
final_rank: 3

code_snippet: #ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using ll = long long;

template<class T> istream& operator>>(istream& i,vector<T>& v) {rep(j,v.size()) i>>v[j]; return i;}
template <typename T> void vecout(const vector<T> &v) {
  rep(i, v.size()) {
    cout << v[i];
    cout << (i == v.size()-1 ? '\n' : ' ');
  }
}

// ------------------------------------------------------------------------------------------------

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int k; cin >> k;
  set<int> row, column, sum, diff;
  vector<vector<char>> grid(8, vector<char>(8, '.'));
  rep(_,k) {
    int r, c; cin >> r >> c;
    row.insert(r);
    column.insert(c);
    sum.insert(r+c);
    diff.insert(r-c);
    grid[r][c] = 'Q';
  }

  vector<int> p(8);
  iota(all(p), 0);
  do {
    set<int> r = row, c = column, s = sum, d = diff;
    bool ok = true;
    rep(i,8) {
      int j = p[i];
      if(grid[i][j] != 'Q' && (r.count(i) || c.count(j) || s.count(i+j) || d.count(i-j))) {
        ok = false;
        break;
      } else {
        r.insert(i);
        c.insert(j);
        s.insert(i+j);
        d.insert(i-j);
      }
    }
    if(ok) {
      rep(i,8) {
        grid[i][p[i]] = 'Q';
      }
      break;
    }
  } while(next_permutation(all(p)));
  
  rep(i,8) {
    rep(j,8) cout << grid[i][j];
    cout << '\n';
  }
}
accuracy: 1
time_ms: 30
memory_kb: 3540
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int main(){
    int k;
    cin >> k;

    set<pair<int, int>> st;

    rep(i, k){
        int r, c;
        cin >> r >> c;
        st.insert(make_pair(r, c));
    }

    vector<int> ord(8);
    iota(ord.begin(), ord.end(), 0);

    do{
        vector<pair<int, int>> a(8);
        rep(i, 8) a.at(i) = make_pair(i, ord.at(i));

        bool ok = true;
        for(pair<int, int> p : st){
            if(find(a.begin(), a.end(), p) == a.end()) ok = false;
        }

        set<int> wa, sa;
        rep(i, 8){
            if(wa.count(a.at(i).first + a.at(i).second)) ok = false;
            wa.insert(a.at(i).first + a.at(i).second);
        }

        rep(i, 8){
            if(sa.count(a.at(i).first - a.at(i).second)) ok = false;
            sa.insert(a.at(i).first - a.at(i).second);
        }

        if(ok){
            vector<string> ans(8);
            rep(i, 8) ans.at(i) = "........";
            rep(i, 8) ans.at(i).at(ord.at(i)) = 'Q';

            rep(i, 8){
                cout << ans.at(i) << endl;
            }
            return 0;
        }

    }while(next_permutation(ord.begin(), ord.end()));

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using namespace atcoder;
// if using this, use "g++ *.cpp -I ~/atcoder/ac..." to complile

typedef long long int ll;
typedef unsigned long long int ull;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main()
{
	int k;
	cin >> k;
	vector<pair<int, int>> v(k);
	rep(i, k) cin >> v[i].first >> v[i].second;

	vector<int> ord(8);
	rep(i, 8) ord[i] = i;

	do {
		bool b = true;
		set<pair<int, int>> st;
		vector<vector<bool>> fi(8, vector<bool>(8, false));
		for (int i = 0; i < 8; i++) {
			int x = i;
			int y = ord[i];
			
			if (fi[x][y]) {
				b = false;
				continue;
			}

			fi[x][y] = true;
			st.insert({x, y});

			int d = 0;
			while (1) {
				bool flag = false;
				if (x+d < 8) {
					fi[x+d][y] = true;
					flag = true;
				}
				if (0 <= x-d) {
					fi[x-d][y] = true;
					flag = true;
				}
				if (y+d < 8) {
					fi[x][y+d] = true;
					flag = true;
				}
				if (0 <= y-d) {
					fi[x][y-d] = true;
					flag = true;
				}
				if (x+d < 8 && y+d < 8) {
					fi[x+d][y+d] = true;
					flag = true;
				}
				if (x+d < 8 && y-d >= 0) {
					fi[x+d][y-d] = true;
					flag = true;
				}
				if (0 <= x-d && y+d < 8) {
					fi[x-d][y+d] = true;
					flag = true;
				}
				if (x-d >= 0 && y-d >= 0) {
					fi[x-d][y-d] = true;
					flag = true;
				}
				if (!flag) break;
				d++;
			}
		}

		if (b) {
			bool flag = true;
			rep(i, k) {
				if (!st.count(v[i])) flag = false;
			}

			if (flag) {
				rep(i, 8) {
					rep(j, 8) {
						if (st.count({i, j})) cout << 'Q';
						else cout << '.';
					}
					cout << '\n';
				}
				return 0;
			}
		}
	} while (next_permutation(ord.begin(), ord.end()));
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3452
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n, m) for (int i = (int)n; i < (int)m; i++)

bool put(int i, int j, vector<vector<bool>> &board) {
    if (board[i][j] == true) return false;

    rep(k, 0, 8) board[i][k] = true;
    rep(k, 0, 8) board[k][j] = true;
    rep(k, 0, 8) {
        if (j + (i - k) >= 0 && j + (i - k) < 8) board[k][j + (i - k)] = true;
    }
    rep(k, 0, 8) {
        if (j - (i - k) >= 0 && j - (i - k) < 8) board[k][j - (i - k)] = true;
    }

    return true;
}

int main() {
    int k;
    cin >> k;
    vector<int> rc(8, -1);
    rep(i, 0, k) {
        int r, c;
        cin >> r >> c;
        rc[r] = c;
    }

    vector<int> ord(8);
    rep(i, 0, 8) ord[i] = i;
    do {
        bool f = true;
        vector<int> q(8, -1);
        vector<vector<bool>> board(8, vector<bool>(8, false));
        rep(i, 0, 8) {
            int j = ord[i];
            if (rc[i] != -1 && rc[i] != j) break;

            if (put(i, j, board)) {
                q[i] = j;
            } else {
                break;
            }
        }

        if (q[7] != -1) {
            rep(i, 0, 8) {
                rep(j, 0, 8) {
                    if (q[i] == j) {
                        cout << 'Q';
                    } else {
                        cout << '.';
                    }
                }
                cout << '\n';
            }
        }
    } while(next_permutation(ord.begin(), ord.end()));

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3432
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef unsigned long long int ull;
typedef long long ll;
using namespace std;
const int INTINF = 1001001001;
const long long LLINF = 1e18;
const int MOD = 998244353;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define display(a) {for (auto b : (a)) cout << b << " "; cout << endl; }
#define Pii pair<int, int>
#define Pli pair<long long, int>
#define Pil pair<int, long long>
#define Pid pair<int, double>
#define Pdi pair<double, int>
#define Psi pair<string, int>
#define Tiii tuple<int, int, int>
#define Tlii tuple<long long, int, int>
#define Till tuple<int, long long, long long>
#define Tdii tuple<double, int, int>
#define chmax(x, y) x = max(x, y)
#define pqueue priority_queue
#define pb push_back

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int ddy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int ddx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};

double calcDist(int x1, int y1, int x2, int y2)
{ return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); }
double calcDist(Pii &A, Pii &B)
{ return sqrt((A.first-B.first)*(A.first-B.first) + (A.second-B.second)*(A.second-B.second)); }
double calcDist(double x1, double y1, double z1, double x2, double y2, double z2)
{ return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2)); }

int main()
{
	int N = 8, M;
	cin >> M;
	vector<int> Y(M), X(M);
	rep (i, M) cin >> Y[i] >> X[i];
	vector<int> O(N);
	rep (i, N) O[i] = i;
	do {
		vector<vector<char>> S(N, vector<char> (N, '.'));
		rep (i, N) S[i][O[i]] = 'Q';
		rep (i, M) S[Y[i]][X[i]] = 'Q';
		bool ok = true;
		rep (i, N) rep (j, N)
		{
			if (S[i][j] != 'Q') continue;
			for (int k = i + 1; k < N; k++) if (S[k][j] == 'Q') ok = false;
			for (int k = j + 1; k < N; k++) if (S[i][k] == 'Q') ok = false;
			for (int k = 1; i+k < N && j+k < N; k++) if (S[i+k][j+k] == 'Q') ok = false;
			for (int k = 1; 0 <= i-k && j+k < N; k++) if (S[i-k][j+k] == 'Q') ok = false;
		}
		if (ok)
		{
			for (auto ss : S) { for (auto s : ss) cout << s; cout << endl; }
			return 0;
		}
	} while (next_permutation(O.begin(), O.end()));
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef unsigned long long int ull;
typedef long long ll;
using namespace std;
const int INTINF = 1001001001;
const long long LLINF = 1e18;
const int MOD = 998244353;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define Pii pair<int, int>
#define Pli pair<long long, int>
#define Pil pair<int, long long>
#define Pid pair<int, double>
#define Pdi pair<double, int>
#define Psi pair<string, int>
#define Tiii tuple<int, int, int>
#define Tlii tuple<long long, int, int>
#define chmax(x, y) x = max(x, y)
#define pqueue priority_queue
#define pb push_back

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int ddy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int ddx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};

double calcDist(int x1, int y1, int x2, int y2)
{ return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); }
double calcDist(Pii &A, Pii &B)
{ return sqrt((A.first-B.first)*(A.first-B.first) + (A.second-B.second)*(A.second-B.second)); }

int main()
{
	int M, N = 8;
	cin >> M;
	vector<int> Y(M), X(M);
	rep (i, M) cin >> Y[i] >> X[i];
	vector<int> O(N);
	rep (i, N) O[i] = i;
	do {
		vector<vector<char>> ban(N, vector<char> (N, '.'));
		rep (i, N) ban[i][O[i]] = 'Q';
		rep (i, M) ban[Y[i]][X[i]] = 'Q';
		bool ok = true;
		rep (i, N) rep (j, N)
		{
			if (ban[i][j] != 'Q') continue;
			for (int k = i + 1; k < N; k++) if (ban[k][j] == 'Q') ok = false;
			for (int k = j + 1; k < N; k++) if (ban[i][k] == 'Q') ok = false;
			for (int k = 1; i+k < N && j+k < N; k++) if (ban[i+k][j+k]=='Q') ok = false;
			for (int k = 1; i+k < N && 0 <= j-k; k++) if (ban[i+k][j-k]=='Q') ok = false;
		}
		if (ok)
		{
			rep (i, N)
			{
				rep (j, N) cout << ban[i][j];
				cout << endl;
			}
			return 0;
		}
	} while (next_permutation(O.begin(), O.end()));
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 4

code_snippet: // ALDS1_13_A
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int N = 8, M;
	cin >> M;
	vector<int> Y(M), X(M), O(N);
	for (int i = 0; i < M; i++) cin >> Y[i] >> X[i];
	for (int i = 0; i < N; i++) O[i] = i;
	do {
		vector<vector<char>> B(N, vector<char>(N, '.'));
		for (int i = 0; i < M; i++) B[Y[i]][X[i]] = 'Q';
		for (int i = 0; i < N; i++) B[i][O[i]] = 'Q';
		bool ok = true;

		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < N; j++)
			{
				if (B[i][j] == '.') continue;
				for (int k = j + 1; k < N; k++) if (B[i][k] == 'Q') ok = false;
				for (int k = i + 1; k < N; k++) if (B[k][j] == 'Q') ok = false;
				for (int k = 1; i + k < N && j + k < N; k++) if (B[i + k][j + k] == 'Q') ok = false;
				for (int k = 1; i + k < N && 0 <= j - k; k++) if (B[i + k][j - k] == 'Q') ok = false;
			}
		}

		if (ok)
		{
			for (int i = 0; i < N; i++)
			{
				for (int j = 0; j < N; j++) cout << B[i][j];
				cout << endl;
			}
			return 0;
		}

	} while (next_permutation(O.begin(), O.end()));
}
accuracy: 1
time_ms: 10
memory_kb: 3452
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef unsigned long long int ull;
typedef long long ll;
using namespace std;
const int INF = 1001001001;
const int MOD = 998244353;
#define Pint pair<int, int>
#define Pli pair<long long, int>
#define Pid pair<int, double>
#define Pdi pair<double, int>
#define Tint tuple<int, int, int>
#define chmax(x, y) x = max(x, y)
#define pqueue priority_queue
#define pb push_back

void solve()
{
	int N, M = 8;
	cin >> N;
	vector<int> Y(N), X(N);
	rep (i, N) cin >> Y[i] >> X[i];
	vector<int> order(M);
	rep (i, M) order[i] = i;
	do {
		vector<vector<char>> ban(M, vector<char> (M, '.'));
		rep (i, M)
		{
			ban[i][order[i]] = 'Q';
		}
		rep (i, N) ban[Y[i]][X[i]] = 'Q';
		bool ok = true;
		rep (i, M)
		{
			rep (j, M)
			{
				if (ban[i][j] != 'Q') continue;
				for (int k = i+1; k < M; k++) if (ban[k][j] == 'Q') ok = false;
				for (int k = j+1; k < M; k++) if (ban[i][k] == 'Q') ok = false;
				for (int k = 1; i+k < M && j+k < M; k++) if (ban[i+k][j+k] == 'Q') ok = false;
				for (int k = 1; j-k >= 0 && i+k < M; k++) if (ban[i+k][j-k] == 'Q') ok = false;

			}
		}
		if (ok)
		{
			rep (i, M)
			{
				rep (j, M) cout << ban[i][j];
				cout << '\n';
			}
		}
	} while (next_permutation(order.begin(), order.end()));
}

#ifdef DEBUG
int Qnum = 3;
#else
int Qnum = 1;
#endif

int main()
{
	while (Qnum--)
	{
		solve();
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef unsigned long long int ull;
typedef long long ll;
using namespace std;
const int INF = 1001001001;
const int MOD = 998244353;
// #define P pair<int, int>
#define chmax(x, y) x = max(x, y)

void solve(void)
{
	int N, M = 8;
	cin >> N;
	vector<int> Y(N), X(N);
	rep(i, N) cin >> Y[i] >> X[i];
	vector<int> order(M);
	rep(i, M) order[i] = i;
	do {
		vector<vector<char>> ban(M, vector<char> (M, '.'));
		rep(i, M) ban[i][order[i]] = 'Q';
		rep(i, N) ban[Y[i]][X[i]] = 'Q';
		bool ok = true;
		rep(s, M) rep(t, M)
		{
			if (ban[s][t] != 'Q') continue;
			for (int k = s + 1; k < M; k++) if (ban[k][t] == 'Q') ok = false;
			for (int k = t + 1; k < M; k++) if (ban[s][k] == 'Q') ok = false;
			for (int k = 1; max(s+k, t+k) < M; k++) if (ban[s+k][t+k] == 'Q') ok = false;
			for (int k = 1; s+k < M&&t-k >= 0; k++) if (ban[s+k][t-k] == 'Q') ok = false;
		}
		if (ok)
		{
			for (auto cc : ban)
			{
				for (auto c : cc) cout << c;
				cout << endl;
			}
			return;
		}
	} while (next_permutation(order.begin(), order.end()));
}

#ifdef DEBUG
int Qnum = 3;
#else
int Qnum = 1;
#endif

int main()
{
	while (Qnum--)
	{
		solve();
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3436
final_rank: 2

code_snippet: /* Last modified: <2025-02-15 01:09:53> */
#include <bits/stdc++.h>
using namespace std;
using ll=long long;

template <typename T> bool next_combination(const T first, const T last, int k) {
  const T subset = first + k;
  // empty container | k = 0 | k == n
  if (first == last || first == subset || last == subset) {
      return false;
  }
  T src = subset;
  while (first != src) {
      src--;
      if (*src < *(last - 1)) {
          T dest = subset;
          while (*src >= *dest) {
              dest++;
          }
          iter_swap(src, dest);
          rotate(src + 1, dest + 1, last);
          rotate(subset, subset + (last - dest) - 1, last);
          return true;
      }
  }
  // restore
  rotate(first, subset, last);
  return false;
}

inline int convertXY2N(int x, int y) {
  return y * 8 + x;
}

inline pair<int, int> convertN2XY(int n) {
  return make_pair(n % 8, n / 8);
}

vector<int> pathOfQueen(int Q) {
  vector<int> path_of_queen = {Q};
  int x, y;
  tie(x, y) = convertN2XY(Q);
  int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
  int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
  int max_k = max(max(x, 7-x), max(y, 7-y));
  for (int k = 1; k <= max_k; ++k) {
    for (int i = 0; i < 8; ++i) {
      int nx = x + k * dx[i];
      int ny = y + k * dy[i];
      if (nx < 0 || nx >= 8 || ny < 0 || ny >= 8) continue;
      path_of_queen.push_back(convertXY2N(nx, ny));
    }
  }
  return path_of_queen;
}

void print(vector<int> &Q) {
  vector<vector<char> > v(8, vector<char>(8, '.'));
  if (! Q.empty()) {
    for (auto q: Q) {
      int x, y;
      tie(x, y) = convertN2XY(q);
      v.at(y).at(x) = 'Q';
    }
  }
  for (int i = 0; i < 8; ++i) {
    for (int j = 0; j < 8; ++j) {
      cout << v.at(i).at(j);
      if (j == 7) {
        cout << endl;
      }
    }
  }
}

int main(void) {
  int K; cin >> K;
  vector<int> Q(8, -1), Q_orig(8, -1);
  for (int i = 0; i < K; ++i) {
    int x, y;
    cin >> x >> y;
    Q.at(i) = convertXY2N(y, x);
    Q_orig.at(i) = Q.at(i);
  }
  if (K == 8) {
    print(Q);
    return 0;
  }
  set<int> movable_grids;
  for (int i = 0; i < 64; ++i) {
    movable_grids.insert(i);
  }
  for (auto q: Q) {
    if (q == -1) continue;
    vector<int> v = pathOfQueen(q);
    for (auto p: v) {
      movable_grids.erase(p);
    }
  }
  vector<int> grids;
  for (auto p: movable_grids) {
    grids.push_back(p);
  }

  int n = 1;
  do {
    copy(Q_orig.begin(), Q_orig.end(), Q.begin());
    for (int i = K; i < 8; ++i) {
      Q.at(i) = grids.at(i - K);
    }
    set<int> immovable_grids;
    for (int i = 0; i < 8; ++i) {
      vector<int> v = pathOfQueen(Q.at(i));
      for (auto p: v) {
        if (p != Q.at(i)) {
          immovable_grids.insert(p);
        }
      }
    }

    bool failed = false;
    for (int i = 0; i < 8; ++i) {
      auto it = immovable_grids.find(Q.at(i));
      if (it != immovable_grids.end()) {
        failed = true;
        break;
      }
    }
    if (! failed) {
      print(Q);
      return 0;
    }
    ++n;
  } while (next_combination(grids.begin(), grids.end(), 8 - K));
  cout << "Not found" << endl;
  return 0;
}
accuracy: 1
time_ms: 760
memory_kb: 3460
final_rank: 11