Problem p03334 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
using namespace std;
int n, D1, D2;
int vis[305][305], vst[305][305], dx[4] = {1, 1, -1, -1},
                                  dy[4] = {1, -1, 1, -1}, num[5][5];
void dfs(int x, int y, int D) {
  for (int i = 0; i * i <= D; i++) {
    int len = D - i * i;
    int j = sqrt(len);
    if (j * j != len)
      continue;
    for (int k = 0; k < 4; k++) {
      int nx = x + i * dx[k], ny = y + j * dy[k];
      if (nx < 1 || nx > n || ny < 1 || ny > n)
        continue;
      if (!vis[nx][ny]) {
        vis[nx][ny] = 3 - vis[x][y];
        dfs(nx, ny, D);
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &D1, &D2);
  n <<= 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (!vis[i][j]) {
        vis[i][j] = 1;
        dfs(i, j, D1);
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      vst[i][j] = vis[i][j];
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (!vis[i][j]) {
        vis[i][j] = 1;
        dfs(i, j, D2);
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      num[vis[i][j]][vst[i][j]]++;
  bool flag = 1;
  for (int a = 1; a <= 2; a++)
    for (int b = 1; b <= 2 && flag; b++) {
      if (num[a][b] < n * n / 4)
        continue;
      int nums = 0;
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          if (nums == n * n / 4)
            break;
          if (vis[i][j] == a && vst[i][j] == b)
            printf("%d %d\n", i - 1, j - 1), nums++;
        }
      flag = 0;
    }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
using namespace std;
int n, D1, D2;
int vis[605][605], vst[605][605], dx[4] = {1, 1, -1, -1},
                                  dy[4] = {1, -1, 1, -1}, num[5][5];
void dfs(int x, int y, int D) {
  for (int i = 0; i * i <= D; i++) {
    int len = D - i * i;
    int j = sqrt(len);
    if (j * j != len)
      continue;
    for (int k = 0; k < 4; k++) {
      int nx = x + i * dx[k], ny = y + j * dy[k];
      if (nx < 1 || nx > n || ny < 1 || ny > n)
        continue;
      if (!vis[nx][ny]) {
        vis[nx][ny] = 3 - vis[x][y];
        dfs(nx, ny, D);
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &D1, &D2);
  n <<= 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (!vis[i][j]) {
        vis[i][j] = 1;
        dfs(i, j, D1);
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      vst[i][j] = vis[i][j];
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (!vis[i][j]) {
        vis[i][j] = 1;
        dfs(i, j, D2);
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      num[vis[i][j]][vst[i][j]]++;
  bool flag = 1;
  for (int a = 1; a <= 2; a++)
    for (int b = 1; b <= 2 && flag; b++) {
      if (num[a][b] < n * n / 4)
        continue;
      int nums = 0;
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          if (nums == n * n / 4)
            break;
          if (vis[i][j] == a && vst[i][j] == b)
            printf("%d %d\n", i - 1, j - 1), nums++;
        }
      flag = 0;
    }
  return 0;
}