Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) FORE(i, 0, n)
#define FORSZ(i, a, v) FOR(i, a, SZ(v))
#define REPSZ(i, v) REP(i, SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXN = 300;

int n, d1, d2;
vector<pair<int, int>> delta[2];
vector<pair<int, int>> adj[2][2 * MAXN][2 * MAXN];

int col[2][2 * MAXN][2 * MAXN];
int qx[2 * MAXN * 2 * MAXN], qy[2 * MAXN * 2 * MAXN], qhead, qtail;

pair<int, int> cur[2 * MAXN * 2 * MAXN];
int ncur;

void run() {
  scanf("%d%d%d", &n, &d1, &d2);

  FORE(dx, -2 * n, +2 * n) FORE(dy, -2 * n, +2 * n) {
    if (dx * dx + dy * dy == d1)
      delta[0].PB(MP(dx, dy));
    if (dx * dx + dy * dy == d2)
      delta[1].PB(MP(dx, dy));
  }
  REP(i, 2) REP(x, 2 * n) REP(y, 2 * n) REPSZ(j, delta[i]) {
    int nx = x + delta[i][j].first, ny = y + delta[i][j].second;
    if (nx < 0 || nx >= 2 * n || ny < 0 || nx >= 2 * n)
      continue;
    adj[i][x][y].PB(MP(nx, ny));
  }

  memset(col, -1, sizeof(col));
  REP(i, 2) REP(sx, 2 * n) REP(sy, 2 * n) if (col[i][sx][sy] == -1) {
    col[i][sx][sy] = 0;
    qhead = qtail = 0;
    qx[qhead] = sx, qy[qhead] = sy, ++qhead;
    while (qtail < qhead) {
      int x = qx[qtail], y = qy[qtail];
      ++qtail;
      REPSZ(j, adj[i][x][y]) {
        int nx = adj[i][x][y][j].first, ny = adj[i][x][y][j].second;
        if (col[i][nx][ny] != -1)
          assert(col[i][nx][ny] != col[i][x][y]);
        else
          col[i][nx][ny] = 1 - col[i][x][y], qx[qhead] = nx, qy[qhead] = ny,
          ++qhead;
      }
    }
  }
  REP(c0, 2) REP(c1, 2) {
    ncur = 0;
    REP(x, 2 * n)
    REP(y, 2 * n) if (col[0][x][y] == c0 && col[1][x][y] == c1) cur[ncur++] =
        MP(x, y);
    if (ncur >= n * n) {
      REP(i, n * n) printf("%d %d\n", cur[i].first, cur[i].second);
      return;
    }
  }
  assert(false);
}

int main() {
  run();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORE(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPE(i, n) FORE(i, 0, n)
#define FORSZ(i, a, v) FOR(i, a, SZ(v))
#define REPSZ(i, v) REP(i, SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXN = 300;

int n, d1, d2;
vector<pair<int, int>> delta[2];
vector<pair<int, int>> adj[2][2 * MAXN][2 * MAXN];

int col[2][2 * MAXN][2 * MAXN];
int qx[2 * MAXN * 2 * MAXN], qy[2 * MAXN * 2 * MAXN], qhead, qtail;

pair<int, int> cur[2 * MAXN * 2 * MAXN];
int ncur;

void run() {
  scanf("%d%d%d", &n, &d1, &d2);

  FORE(dx, -2 * n, +2 * n) FORE(dy, -2 * n, +2 * n) {
    if (dx * dx + dy * dy == d1)
      delta[0].PB(MP(dx, dy));
    if (dx * dx + dy * dy == d2)
      delta[1].PB(MP(dx, dy));
  }
  REP(i, 2) REP(x, 2 * n) REP(y, 2 * n) REPSZ(j, delta[i]) {
    int nx = x + delta[i][j].first, ny = y + delta[i][j].second;
    if (nx < 0 || nx >= 2 * n || ny < 0 || ny >= 2 * n)
      continue;
    adj[i][x][y].PB(MP(nx, ny));
  }

  memset(col, -1, sizeof(col));
  REP(i, 2) REP(sx, 2 * n) REP(sy, 2 * n) if (col[i][sx][sy] == -1) {
    col[i][sx][sy] = 0;
    qhead = qtail = 0;
    qx[qhead] = sx, qy[qhead] = sy, ++qhead;
    while (qtail < qhead) {
      int x = qx[qtail], y = qy[qtail];
      ++qtail;
      REPSZ(j, adj[i][x][y]) {
        int nx = adj[i][x][y][j].first, ny = adj[i][x][y][j].second;
        if (col[i][nx][ny] != -1)
          assert(col[i][nx][ny] != col[i][x][y]);
        else
          col[i][nx][ny] = 1 - col[i][x][y], qx[qhead] = nx, qy[qhead] = ny,
          ++qhead;
      }
    }
  }
  REP(c0, 2) REP(c1, 2) {
    ncur = 0;
    REP(x, 2 * n)
    REP(y, 2 * n) if (col[0][x][y] == c0 && col[1][x][y] == c1) cur[ncur++] =
        MP(x, y);
    if (ncur >= n * n) {
      REP(i, n * n) printf("%d %d\n", cur[i].first, cur[i].second);
      return;
    }
  }
  assert(false);
}

int main() {
  run();
  return 0;
}