Problem p03594 (C++)

Buggy submission (status: Memory Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
// constexpr long long MOD = 998244353;
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline int popcount(int x) { return __builtin_popcount(x); }
inline int popcount(long long x) { return __builtin_popcountll(x); }
void print() { cout << "\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

const string color = "RYGB";
const int ex[4] = {-1, 0, 1, 0};
const int ey[4] = {0, -1, 0, 1};
const int B = 2000;
const int offset = 750;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W, d;
  cin >> H >> W >> d;

  vector<int> dx, dy;
  if (d & 1) {
    dx = {(d + 1) / 2, -(d / 2), -((d + 1) / 2), d / 2};
    dy = {d / 2, (d + 1) / 2, -(d / 2), -((d + 1) / 2)};
  } else {
    dx = {-d / 2, d / 2, d / 2, -d / 2};
    dy = {-d / 2, -d / 2, d / 2, d / 2};
  }
  vector<vector<char>> ans(B, vector<char>(B, '#'));
  auto dfs = [&](auto &&self, int x, int y, int c) -> void {
    // print(x,y,c);
    ans[x][y] = color[c];
    if (~d & 1 and y + 1 < B)
      ans[x][y + 1] = color[c];
    rep(k, 4) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      int nc = ((k & 1) ? (c ^ 1) : (c ^ 2));
      if (nx < 0 or nx >= B or ny < 0 or ny >= B)
        continue;
      if (ans[nx][ny] != '#')
        continue;
      self(self, nx, ny, nc);
    }
  };
  dfs(dfs, 0, 0, 0);

  queue<pii> que;
  rep(i, B) rep(j, B) {
    if (ans[i][j] != '#')
      que.emplace(i, j);
  }
  while (!que.empty()) {
    auto [x, y] = que.front();
    que.pop();
    rep(k, 4) {
      int nx = x + ex[k];
      int ny = y + ey[k];
      if (nx < 0 or nx >= B or ny < 0 or ny >= B)
        continue;
      if (ans[nx][ny] != '#')
        continue;
      ans[nx][ny] = ans[x][y];
      que.emplace(nx, ny);
    }
  }

  for (int i = offset; i < offset + H; i++) {
    for (int j = offset; j < offset + W; j++)
      cout << ans[i][j];
    cout << ln;
  }
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
// constexpr long long MOD = 998244353;
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
inline int popcount(int x) { return __builtin_popcount(x); }
inline int popcount(long long x) { return __builtin_popcountll(x); }
void print() { cout << "\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

const string color = "RYGB";
const int ex[4] = {-1, 0, 1, 0};
const int ey[4] = {0, -1, 0, 1};
const int B = 1700;
const int offset = 750;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W, d;
  cin >> H >> W >> d;

  vector<int> dx, dy;
  if (d & 1) {
    dx = {(d + 1) / 2, -(d / 2), -((d + 1) / 2), d / 2};
    dy = {d / 2, (d + 1) / 2, -(d / 2), -((d + 1) / 2)};
  } else {
    dx = {-d / 2, d / 2, d / 2, -d / 2};
    dy = {-d / 2, -d / 2, d / 2, d / 2};
  }
  vector<vector<char>> ans(B, vector<char>(B, '#'));
  auto dfs = [&](auto &&self, int x, int y, int c) -> void {
    // print(x,y,c);
    ans[x][y] = color[c];
    if (~d & 1 and y + 1 < B)
      ans[x][y + 1] = color[c];
    rep(k, 4) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      int nc = ((k & 1) ? (c ^ 1) : (c ^ 2));
      if (nx < 0 or nx >= B or ny < 0 or ny >= B)
        continue;
      if (ans[nx][ny] != '#')
        continue;
      self(self, nx, ny, nc);
    }
  };
  dfs(dfs, 0, 0, 0);

  queue<pii> que;
  rep(i, B) rep(j, B) {
    if (ans[i][j] != '#')
      que.emplace(i, j);
  }
  while (!que.empty()) {
    auto [x, y] = que.front();
    que.pop();
    rep(k, 4) {
      int nx = x + ex[k];
      int ny = y + ey[k];
      if (nx < 0 or nx >= B or ny < 0 or ny >= B)
        continue;
      if (ans[nx][ny] != '#')
        continue;
      ans[nx][ny] = ans[x][y];
      que.emplace(nx, ny);
    }
  }

  for (int i = offset; i < offset + H; i++) {
    for (int j = offset; j < offset + W; j++)
      cout << ans[i][j];
    cout << ln;
  }
}