Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define clr(x) memset(x, 0, sizeof(x))
#define MN 303
using namespace std;
inline int in() {
  int x = 0;
  bool f = 0;
  char c;
  for (; (c = getchar()) < '0' || c > '9'; f = c == '-')
    ;
  for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';
       x = (x << 3) + (x << 1) + c - '0')
    ;
  return f ? -x : x;
}
const int dx[4] = {1, 1, -1, -1};
const int dy[4] = {1, -1, 1, -1};
int col[MN][MN], X[MN], Y[MN], num[4];
int n, m, d1, d2, tot, cnt, res;
bool vis[MN][MN];
inline bool check(int x, int y) {
  return ((x >= 0) && (y >= 0) && (x < m) && (y < m) && (!vis[x][y]));
}
inline void dfs(int x, int y, bool c) {
  vis[x][y] = 1;
  col[x][y] = (col[x][y] << 1) + c;
  for (int i = 0; i < cnt; ++i)
    for (int j = 0; j < 4; ++j)
      if (check(x + dx[j] * X[i], y + dy[j] * Y[i]))
        dfs(x + dx[j] * X[i], y + dy[j] * Y[i], c ^ 1);
}
inline void sol(int d) {
  clr(X);
  clr(Y);
  clr(vis);
  cnt = 0;
  for (int i = 0; i < m; ++i) {
    if (i * i > d)
      break;
    for (int j = 0; j < m; ++j)
      if (i * i + j * j == d)
        X[cnt] = i, Y[cnt] = j, ++cnt;
  }
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j)
      if (!vis[i][j])
        dfs(i, j, 0);
}
int main() {
  n = in();
  d1 = in();
  d2 = in();
  m = (n << 1);
  sol(d1);
  sol(d2);
  tot = 0;
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j)
      ++num[col[i][j]];
  for (int i = 0; i < 4; ++i)
    if (num[i] >= n * n)
      res = i;
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j) {
      if (res == col[i][j])
        ++tot, printf("%d %d\n", i, j);
      if (tot == n * n)
        return 0;
    }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define clr(x) memset(x, 0, sizeof(x))
#define MN 603
using namespace std;
inline int in() {
  int x = 0;
  bool f = 0;
  char c;
  for (; (c = getchar()) < '0' || c > '9'; f = c == '-')
    ;
  for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';
       x = (x << 3) + (x << 1) + c - '0')
    ;
  return f ? -x : x;
}
const int dx[4] = {1, 1, -1, -1};
const int dy[4] = {1, -1, 1, -1};
int col[MN][MN], X[MN], Y[MN], num[4];
int n, m, d1, d2, tot, cnt, res;
bool vis[MN][MN];
inline bool check(int x, int y) {
  return ((x >= 0) && (y >= 0) && (x < m) && (y < m) && (!vis[x][y]));
}
inline void dfs(int x, int y, bool c) {
  vis[x][y] = 1;
  col[x][y] = (col[x][y] << 1) + c;
  for (int i = 0; i < cnt; ++i)
    for (int j = 0; j < 4; ++j)
      if (check(x + dx[j] * X[i], y + dy[j] * Y[i]))
        dfs(x + dx[j] * X[i], y + dy[j] * Y[i], c ^ 1);
}
inline void sol(int d) {
  clr(X);
  clr(Y);
  clr(vis);
  cnt = 0;
  for (int i = 0; i < m; ++i) {
    if (i * i > d)
      break;
    for (int j = 0; j < m; ++j)
      if (i * i + j * j == d)
        X[cnt] = i, Y[cnt] = j, ++cnt;
  }
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j)
      if (!vis[i][j])
        dfs(i, j, 0);
}
int main() {
  n = in();
  d1 = in();
  d2 = in();
  m = (n << 1);
  sol(d1);
  sol(d2);
  tot = 0;
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j)
      ++num[col[i][j]];
  for (int i = 0; i < 4; ++i)
    if (num[i] >= n * n)
      res = i;
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < m; ++j) {
      if (res == col[i][j])
        ++tot, printf("%d %d\n", i, j);
      if (tot == n * n)
        return 0;
    }
  return 0;
}