Problem p02825 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

char board[30][30];

int main() {
  bool f;
  int n;
  scanf("%d", &n);
  if (n < 3)
    printf("-1\n");
  else if (n == 4) {
    printf("aabd\nccbd\nbdaa\nbdcc\n");
  } else if (n % 3 == 0) {
    for (int i = 1; i <= n; i += 3) {
      for (int j = 1; j <= n; j += 3)
        printf("a..");
      printf("\n");
      for (int j = 1; j <= n; j += 3)
        printf("a..");
      printf("\n");
      for (int j = 1; j <= n; j += 3)
        printf(".bb");
      printf("\n");
    }
  } else if (n % 6 == 5) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';
    int tot = (n - 3) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 2; j < n; j += 3) {
        pos = (i * 3 + j - 2) % (n - 2) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 2; j < n; j += 3) {
        pos = (x * 3 + j - 2) % (n - 2) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    for (int j = 2; j <= n; j += 2, f = !f) {
      board[1][j] = board[1][j - 1] = f ? 'a' : 'b';
      board[n][j] = board[n][j + 1] = ((n / 6) & 1) ^ f ? 'b' : 'a';
    }
    for (int j = 2; j <= n; j += 2, f = !f) {
      board[j][n] = board[j - 1][n] = f ? 'a' : 'b';
      board[j][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'b' : 'a';
    }
    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  } else if (n % 6 == 4) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';

    int tot = (n - 5) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 3; j < n; j += 3) {
        pos = (i * 3 + j - 3) % (n - 4) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 3; j < n; j += 3) {
        pos = (x * 3 + j - 3) % (n - 4) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[1][j] = board[1][j - 1] = f ? 'b' : 'a';
      board[2][j] = board[2][j - 1] = f ? 'a' : 'b';
      board[n - 1][j + 1] = board[n - 1][j + 2] = ((n / 6) & 1) ^ f ? 'a' : 'b';
      board[n][j + 1] = board[n][j + 2] = ((n / 6) & 1) ^ f ? 'b' : 'a';
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[j][n] = board[j - 1][n] = f ? 'z' : 'y';
      board[j][n - 1] = board[j - 1][n - 1] = f ? 'y' : 'z';
      board[j + 2][2] = board[j + 1][2] = ((n / 6) & 1) ^ f ? 'z' : 'y';
      board[j + 2][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'y' : 'z';
    }

    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  } else if (n % 6 == 1) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';

    int tot = (n - 3) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 2; j < n; j += 3) {
        pos = (i * 3 + j - 2) % (n - 1) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 2; j < n; j += 3) {
        pos = (x * 3 + j - 2) % (n - 1) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[1][j] = board[1][j + 1] = f ? 'b' : 'a';
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[j][1] = board[j + 1][1] = f ? 'z' : 'y';
    }

    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  } else if (n % 6 == 2) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';

    int tot = (n - 6) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 3; j < n; j += 3) {
        pos = (i * 3 + j - 3) % (n - 2) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 3; j < n; j += 3) {
        pos = (x * 3 + j - 3) % (n - 2) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }

    for (int j = 3; j < n; j += 2, f = !f) {
      board[1][j] = board[1][j + 1] = f ? 'b' : 'a';
      board[2][j] = board[2][j + 1] = f ? 'a' : 'b';
    }

    for (int j = 3; j < n; j += 2, f = !f) {
      board[j][1] = board[j + 1][1] = f ? 'z' : 'y';
      board[j][2] = board[j + 1][2] = f ? 'y' : 'z';
    }

    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  }
}


Fixed submission:
#include <stdio.h>

char board[1010][1010];

int main() {
  bool f;
  int n;
  scanf("%d", &n);
  if (n < 3)
    printf("-1\n");
  else if (n == 4) {
    printf("aabd\nccbd\nbdaa\nbdcc\n");
  } else if (n % 3 == 0) {
    for (int i = 1; i <= n; i += 3) {
      for (int j = 1; j <= n; j += 3)
        printf("a..");
      printf("\n");
      for (int j = 1; j <= n; j += 3)
        printf("a..");
      printf("\n");
      for (int j = 1; j <= n; j += 3)
        printf(".bb");
      printf("\n");
    }
  } else if (n % 6 == 5) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';
    int tot = (n - 3) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 2; j < n; j += 3) {
        pos = (i * 3 + j - 2) % (n - 2) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 2; j < n; j += 3) {
        pos = (x * 3 + j - 2) % (n - 2) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    for (int j = 2; j <= n; j += 2, f = !f) {
      board[1][j] = board[1][j - 1] = f ? 'a' : 'b';
      board[n][j] = board[n][j + 1] = ((n / 6) & 1) ^ f ? 'b' : 'a';
    }
    for (int j = 2; j <= n; j += 2, f = !f) {
      board[j][n] = board[j - 1][n] = f ? 'a' : 'b';
      board[j][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'b' : 'a';
    }
    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  } else if (n % 6 == 4) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';

    int tot = (n - 5) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 3; j < n; j += 3) {
        pos = (i * 3 + j - 3) % (n - 4) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 3; j < n; j += 3) {
        pos = (x * 3 + j - 3) % (n - 4) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[1][j] = board[1][j - 1] = f ? 'b' : 'a';
      board[2][j] = board[2][j - 1] = f ? 'a' : 'b';
      board[n - 1][j + 1] = board[n - 1][j + 2] = ((n / 6) & 1) ^ f ? 'a' : 'b';
      board[n][j + 1] = board[n][j + 2] = ((n / 6) & 1) ^ f ? 'b' : 'a';
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[j][n] = board[j - 1][n] = f ? 'z' : 'y';
      board[j][n - 1] = board[j - 1][n - 1] = f ? 'y' : 'z';
      board[j + 2][2] = board[j + 1][2] = ((n / 6) & 1) ^ f ? 'z' : 'y';
      board[j + 2][1] = board[j + 1][1] = ((n / 6) & 1) ^ f ? 'y' : 'z';
    }

    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  } else if (n % 6 == 1) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';

    int tot = (n - 3) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 2; j < n; j += 3) {
        pos = (i * 3 + j - 2) % (n - 1) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 2; j < n; j += 3) {
        pos = (x * 3 + j - 2) % (n - 1) + 2;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[1][j] = board[1][j + 1] = f ? 'b' : 'a';
    }

    for (int j = 2; j < n; j += 2, f = !f) {
      board[j][1] = board[j + 1][1] = f ? 'z' : 'y';
    }

    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  } else if (n % 6 == 2) {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        board[i][j] = '.';

    int tot = (n - 6) >> 1, x, y, pos;
    x = tot / 2;
    y = tot % 2;

    for (int i = 0; i < x; i++) {
      for (int j = 3; j < n; j += 3) {
        pos = (i * 3 + j - 3) % (n - 2) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos][j + 2] = 'd';
        board[pos + 1][j] = 'f';
        board[pos + 1][j + 2] = 'd';
        board[pos + 2][j] = 'f';
        board[pos + 2][j + 1] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }
    if (y) {
      for (int j = 3; j < n; j += 3) {
        pos = (x * 3 + j - 3) % (n - 2) + 3;
        board[pos][j] = 'c';
        board[pos][j + 1] = 'c';
        board[pos + 1][j + 2] = 'e';
        board[pos + 2][j + 2] = 'e';
      }
    }

    for (int j = 3; j < n; j += 2, f = !f) {
      board[1][j] = board[1][j + 1] = f ? 'b' : 'a';
      board[2][j] = board[2][j + 1] = f ? 'a' : 'b';
    }

    for (int j = 3; j < n; j += 2, f = !f) {
      board[j][1] = board[j + 1][1] = f ? 'z' : 'y';
      board[j][2] = board[j + 1][2] = f ? 'y' : 'z';
    }

    for (int i = 1; i <= n; i++, printf("\n"))
      for (int j = 1; j <= n; j++)
        printf("%c", board[i][j]);
  }
}