Problem p02825 (C++)

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

int main() {
  int n;
  scanf("%d", &n);
  if (n == 2) {
    puts("-1");
    return 0;
  }
  if (n == 3) {
    puts("aa.");
    puts("..b");
    puts("..b");
    return 0;
  }

  vector<vector<int>> p(n, vector<int>(n, -1));
  vector<int> col(n), row(n);

  function<void(int, int, int)> Dfs = [&](int r, int c, int dep) {
    // printf("r = %d c = %d dep = %d\n", r, c, dep);
    if (c == n)
      return Dfs(r + 1, 0, dep);
    if (r > 1 && row[r - 1] != row[0])
      return;
    if (r == n) {
      bool ok = true;
      for (int i = 0; i < n; ++i)
        ok &= row[i] == row[0];
      for (int i = 0; i < n; ++i)
        ok &= col[i] == row[0];
      if (ok) {
        printf("value = %d\n", row[0]);
        for (int i = 0; i < n; ++i) {
          for (int j = 0; j < n; ++j) {
            if (p[i][j] == -1)
              printf(".");
            else
              printf("%c", p[i][j] + 'a');
          }
          puts("");
        }
        puts("");
      }
      return;
    }
    if (c + 1 < n && p[r][c] == -1 && p[r][c + 1] == -1) {
      p[r][c] = p[r][c + 1] = dep;
      col[c]++, col[c + 1]++, row[r]++;
      Dfs(r, c + 1, dep + 1);
      col[c]--, col[c + 1]--, row[r]--;
      p[r][c] = p[r][c + 1] = -1;
    }
    if (r + 1 < n && p[r][c] == -1 && p[r + 1][c] == -1) {
      p[r][c] = p[r + 1][c] = dep;
      col[c]++, row[r + 1]++, row[r]++;
      Dfs(r, c + 1, dep + 1);
      col[c]--, row[r + 1]--, row[r]--;
      p[r][c] = p[r + 1][c] = -1;
    }
    Dfs(r, c + 1, dep);
  };

  // Dfs(0, 0, 0);
  const vector<vector<string>> kPattern = {
      {"aabc", "ddbc", "efgg", "efhh"},
      {"..abc", "..abc", "ddeff", "gge.h", "iijjh"},
      {"aabb.c", "..deec", "ffd..g", "hh..ig", "..j.ik", "..jllk"},
      {"aabbcc.", "dde...f", "..e..gf", "....hgi", "....hji", "...kkjl",
       "mmnn..l"}};

  if (n == 7) {
    for (int i = 0; i < 7; ++i)
      printf("%s\n", kPattern[3][i].c_str());
    return 0;
  }
  for (int i = 0; i * 4 <= n; ++i) {
    for (int j = 0; i * 5 + j * 5 <= n; ++j) {
      if ((n - i * 4 - j * 5) % 6 == 0) {
        int g = 0;
        vector<string> ans(n, string(n, '.'));
        for (int k = 0; k < i; ++k) {
          for (int a = 0; a < 4; ++a) {
            for (int b = 0; b < 4; ++b)
              ans[g + a][g + b] = kPattern[0][a][b];
          }
          g += 4;
        }
        for (int k = 0; k < j; ++k) {
          for (int a = 0; a < 5; ++a) {
            for (int b = 0; b < 5; ++b)
              ans[g + a][g + b] = kPattern[1][a][b];
          }
          g += 5;
        }
        for (int k = 0; g < n; ++k) {
          for (int a = 0; a < 6; ++a) {
            for (int b = 0; b < 6; ++b)
              ans[g + a][g + b] = kPattern[2][a][b];
          }
          g += 6;
        }
        for (int k = 0; k < n; ++k)
          printf("%s\n", ans[k].c_str());
        return 0;
      }
    }
  }
  assert(false);
}


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

int main() {
  int n;
  scanf("%d", &n);
  if (n == 2) {
    puts("-1");
    return 0;
  }
  if (n == 3) {
    puts("aa.");
    puts("..b");
    puts("..b");
    return 0;
  }

  vector<vector<int>> p(n, vector<int>(n, -1));
  vector<int> col(n), row(n);

  function<void(int, int, int)> Dfs = [&](int r, int c, int dep) {
    // printf("r = %d c = %d dep = %d\n", r, c, dep);
    if (c == n)
      return Dfs(r + 1, 0, dep);
    if (r > 1 && row[r - 1] != row[0])
      return;
    if (r == n) {
      bool ok = true;
      for (int i = 0; i < n; ++i)
        ok &= row[i] == row[0];
      for (int i = 0; i < n; ++i)
        ok &= col[i] == row[0];
      if (ok) {
        printf("value = %d\n", row[0]);
        for (int i = 0; i < n; ++i) {
          for (int j = 0; j < n; ++j) {
            if (p[i][j] == -1)
              printf(".");
            else
              printf("%c", p[i][j] + 'a');
          }
          puts("");
        }
        puts("");
      }
      return;
    }
    if (c + 1 < n && p[r][c] == -1 && p[r][c + 1] == -1) {
      p[r][c] = p[r][c + 1] = dep;
      col[c]++, col[c + 1]++, row[r]++;
      Dfs(r, c + 1, dep + 1);
      col[c]--, col[c + 1]--, row[r]--;
      p[r][c] = p[r][c + 1] = -1;
    }
    if (r + 1 < n && p[r][c] == -1 && p[r + 1][c] == -1) {
      p[r][c] = p[r + 1][c] = dep;
      col[c]++, row[r + 1]++, row[r]++;
      Dfs(r, c + 1, dep + 1);
      col[c]--, row[r + 1]--, row[r]--;
      p[r][c] = p[r + 1][c] = -1;
    }
    Dfs(r, c + 1, dep);
  };

  // Dfs(0, 0, 0);
  const vector<vector<string>> kPattern = {
      {"aabc", "ddbc", "efgg", "efhh"},
      {"..abc", "..abc", "ddeff", "gge.h", "iijjh"},
      {"aabb.c", "..deec", "ffd..g", "hh..ig", "..j.ik", "..jllk"},
      {"aabbcc.", "dde...f", "..e..gf", "....hgi", "....hji", "...kkjl",
       "mmnn..l"}};

  if (n == 7) {
    for (int i = 0; i < 7; ++i)
      printf("%s\n", kPattern[3][i].c_str());
    return 0;
  }
  for (int i = 0; i * 4 <= n; ++i) {
    for (int j = 0; i * 4 + j * 5 <= n; ++j) {
      if ((n - i * 4 - j * 5) % 6 == 0) {
        int g = 0;
        vector<string> ans(n, string(n, '.'));
        for (int k = 0; k < i; ++k) {
          for (int a = 0; a < 4; ++a) {
            for (int b = 0; b < 4; ++b)
              ans[g + a][g + b] = kPattern[0][a][b];
          }
          g += 4;
        }
        for (int k = 0; k < j; ++k) {
          for (int a = 0; a < 5; ++a) {
            for (int b = 0; b < 5; ++b)
              ans[g + a][g + b] = kPattern[1][a][b];
          }
          g += 5;
        }
        for (int k = 0; g < n; ++k) {
          for (int a = 0; a < 6; ++a) {
            for (int b = 0; b < 6; ++b)
              ans[g + a][g + b] = kPattern[2][a][b];
          }
          g += 6;
        }
        for (int k = 0; k < n; ++k)
          printf("%s\n", ans[k].c_str());
        return 0;
      }
    }
  }
  assert(false);
}