Problem p03334 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> no;
pair<int, int> pt[360005];
bool in[605][605];
int n, d1, d2, X;

int f(pair<int, int> a) {
  int x = max(a.first % (2 * X), a.second % (2 * X));
  if (X == 1000)
    x = (a.first - n) * (a.first - n) + (a.second - n) * (a.second - n);
  else if (X > 2 * n)
    x = a.first * a.first + a.second * a.second;
  return x;
}

bool slv() {
  vector<pair<int, int>> ans;
  for (int i = 0; i < 4 * n * n; i++) {
    auto u = pt[i];
    bool ok = true;
    for (auto v : no) {
      if (u.first - v.first >= 0 && u.second - v.second >= 0 &&
          in[u.first - v.first][u.second - v.second])
        ok = false;
      if (u.first - v.first >= 0 && u.second + v.second < 2 * n &&
          in[u.first - v.first][u.second + v.second])
        ok = false;
      if (u.first + v.first < 2 * n && u.second - v.second >= 0 &&
          in[u.first + v.first][u.second - v.second])
        ok = false;
      if (u.first + v.first < 2 * n && u.second + v.second < 2 * n &&
          in[u.first + v.first][u.second + v.second])
        ok = false;
    }
    if (ok) {
      ans.push_back(u);
      in[u.first][u.second] = true;
      if (ans.size() == n * n)
        break;
    }
  }
  if (ans.size() == n * n) {
    for (auto u : ans)
      printf("%d %d\n", u.first, u.second);
    return true;
  }
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      in[i][j] = false;
  return false;
}

int main() {
  scanf("%d%d%d", &n, &d1, &d2);
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      pt[i * 2 * n + j] = make_pair(i, j);
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      if (i * i + j * j == d1 || i * i + j * j == d2)
        no.emplace_back(i, j);
  while (true) {
    X++;
    if (2 * X * X >= d1 || 2 * X * X >= d2 || X >= 2 * n)
      break;
    bool ok = true;
    for (int i = 0; i < 2 * n; i++)
      for (int j = 0; j < 2 * n; j++)
        if (i % (2 * X) < X && j % (2 * X) < X)
          for (auto d : no) {
            int I = i + d.first, J = j + d.second;
            if (I >= 0 && I < 2 * n && J >= 0 && J < 2 * n && I % (2 * X) < X &&
                J % (2 * X) < X)
              ok = false;
          }
    if (ok)
      break;
  }
  if (slv())
    goto done;
  sort(pt, pt + 4 * n * n,
       [](pair<int, int> a, pair<int, int> b) -> bool { return f(a) < f(b); });
  if (slv())
    goto done;
  X = 601;
  sort(pt, pt + 4 * n * n,
       [](pair<int, int> a, pair<int, int> b) -> bool { return f(a) < f(b); });
  if (slv())
    goto done;
  X = 1000;
  while (!slv())
    random_shuffle(pt, pt + 4 * n * n);
done:
  /*freopen("vis.txt", "w", stdout);
  for (int i = 0;i < 2*n;i++)
  {
          for (int j = 0;j < 2*n;j++) printf("%c", in[i][j]?'X':' ');
          printf("\n");
  }*/
  return 0;
}


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

vector<pair<int, int>> no;
pair<int, int> pt[360005];
bool in[605][605];
int n, d1, d2, X;

int f(pair<int, int> a) {
  int x = max(a.first % (2 * X), a.second % (2 * X));
  if (X == 1000)
    x = (a.first - n) * (a.first - n) + (a.second - n) * (a.second - n);
  else if (X > 2 * n)
    x = a.first * a.first + a.second * a.second;
  return x;
}

bool slv() {
  vector<pair<int, int>> ans;
  for (int i = 0; i < 4 * n * n; i++) {
    auto u = pt[i];
    bool ok = true;
    for (auto v : no) {
      if (u.first - v.first >= 0 && u.second - v.second >= 0 &&
          in[u.first - v.first][u.second - v.second])
        ok = false;
      if (u.first - v.first >= 0 && u.second + v.second < 2 * n &&
          in[u.first - v.first][u.second + v.second])
        ok = false;
      if (u.first + v.first < 2 * n && u.second - v.second >= 0 &&
          in[u.first + v.first][u.second - v.second])
        ok = false;
      if (u.first + v.first < 2 * n && u.second + v.second < 2 * n &&
          in[u.first + v.first][u.second + v.second])
        ok = false;
    }
    if (ok) {
      ans.push_back(u);
      in[u.first][u.second] = true;
      if (ans.size() == n * n)
        break;
    }
  }
  if (ans.size() == n * n) {
    for (auto u : ans)
      printf("%d %d\n", u.first, u.second);
    return true;
  }
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      in[i][j] = false;
  return false;
}

int main() {
  scanf("%d%d%d", &n, &d1, &d2);
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      pt[i * 2 * n + j] = make_pair(i, j);
  for (int i = 0; i < 2 * n; i++)
    for (int j = 0; j < 2 * n; j++)
      if (i * i + j * j == d1 || i * i + j * j == d2)
        no.emplace_back(i, j);
  while (true) {
    X++;
    if (2 * X * X >= d1 || 2 * X * X >= d2 || X >= 2 * n)
      break;
    bool ok = true;
    for (int i = 0; i < 2 * n; i++)
      for (int j = 0; j < 2 * n; j++)
        if (i % (2 * X) < X && j % (2 * X) < X)
          for (auto d : no) {
            int I = i + d.first, J = j + d.second;
            if (I >= 0 && I < 2 * n && J >= 0 && J < 2 * n && I % (2 * X) < X &&
                J % (2 * X) < X)
              ok = false;
          }
    if (ok)
      break;
  }
  if (slv())
    goto done;
  sort(pt, pt + 4 * n * n,
       [](pair<int, int> a, pair<int, int> b) -> bool { return f(a) < f(b); });
  if (slv())
    goto done;
  X = 601;
  sort(pt, pt + 4 * n * n,
       [](pair<int, int> a, pair<int, int> b) -> bool { return f(a) < f(b); });
  if (slv())
    goto done;
  X = 1;
  while (true) {
    sort(pt, pt + 4 * n * n, [](pair<int, int> a, pair<int, int> b) -> bool {
      return f(a) < f(b);
    });
    if (slv())
      goto done;
    X++;
  }
  // while (!slv()) random_shuffle(pt, pt+4*n*n);
done:
  /*freopen("vis.txt", "w", stdout);
  for (int i = 0;i < 2*n;i++)
  {
          for (int j = 0;j < 2*n;j++) printf("%c", in[i][j]?'X':' ');
          printf("\n");
  }*/
  return 0;
}