Problem p02825 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define Inf 1000000000
#define nmax_def 110000
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;

int N;
vector<string> ans;

int main() {
  cin >> N;
  ans.resize(N);
  rep(i, N) {
    string anstmp;
    anstmp = "";
    rep(j, N) { anstmp.push_back('.'); }
    ans[i] = anstmp;
  }
  if (N == 2) {
    cout << "-1" << endl;
  } else if (N % 2 == 0) {
    int N2 = N / 2;
    rep(i, N2) {
      int j = i;
      ans[i * 2][j * 2] = 'a';
      ans[i * 2][j * 2 + 1] = 'a';
      ans[i * 2 + 1][j * 2] = 'b';
      ans[i * 2 + 1][j * 2 + 1] = 'b';
      j++;
      j %= N2;
      ans[i * 2][j * 2] = 'c';
      ans[i * 2][j * 2 + 1] = 'd';
      ans[i * 2 + 1][j * 2] = 'c';
      ans[i * 2 + 1][j * 2 + 1] = 'd';
    }
    rep(i, N) { cout << ans[i] << endl; }
  } else {
    int N2 = (N - 1) / 2;
    if (N >= 5) {
      int x, y;
      x = 0;
      y = 0;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'a';
        ans[x + 1][y] = 'e';
        ans[x][y + 1] = 'a';
        ans[x + 1][y + 1] = 'e';
        x += 2;
        y += 2;
      }
      x = N - 1;
      y = 0;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'b';
        ans[x - 1][y] = 'b';
        ans[x][y + 1] = 'f';
        ans[x - 1][y + 1] = 'f';
        x -= 2;
        y += 2;
      }
      x = N - 1;
      y = N - 1;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'c';
        ans[x - 1][y] = 'g';
        ans[x][y - 1] = 'c';
        ans[x - 1][y - 1] = 'g';
        x -= 2;
        y -= 2;
      }
      x = 0;
      y = N - 1;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'd';
        ans[x + 1][y] = 'd';
        ans[x][y - 1] = 'h';
        ans[x + 1][y - 1] = 'h';
        x += 2;
        y -= 2;
      }
    }
    if (N2 % 2 == 1) {
      int x = 0;
      int y = 0;
      int count = 0;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y++;
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y++;
      x = 0;
      y = 6;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x++;
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x++;
      x = 6;
      y = 6;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y--;
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y--;
      x = 6;
      y = 0;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x--;
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x--;
      ans[N2 - 1][N2 - 1] = 'i';
      ans[N2 - 1][N2] = 'i';
      ans[N2][N2 + 1] = 'j';
      ans[N2 + 1][N2 + 1] = 'j';
    } else {
      int x = 0;
      int y = 0;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x--;
      ans[N2 - 1][N2 - 1] = 'i';
      ans[N2 - 1][N2] = 'i';
      ans[N2][N2 + 1] = 'j';
      ans[N2 + 1][N2 + 1] = 'j';
    }

    rep(i, N) { cout << ans[i] << endl; }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define Inf 1000000000
#define nmax_def 110000
#define mod 1000000007
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;

int N;
vector<string> ans;

int main() {
  cin >> N;
  ans.resize(N);
  rep(i, N) {
    string anstmp;
    anstmp = "";
    rep(j, N) { anstmp.push_back('.'); }
    ans[i] = anstmp;
  }
  if (N == 2) {
    cout << "-1" << endl;
  } else if (N == 3) {
    ans[0][0] = 'a';
    ans[0][1] = 'a';
    ans[0][2] = 'b';
    ans[1][2] = 'b';
    ans[2][2] = 'c';
    ans[2][1] = 'c';
    ans[2][0] = 'd';
    ans[1][0] = 'd';
    rep(i, N) { cout << ans[i] << endl; }
  } else if (N % 2 == 0) {
    int N2 = N / 2;
    rep(i, N2) {
      int j = i;
      ans[i * 2][j * 2] = 'a';
      ans[i * 2][j * 2 + 1] = 'a';
      ans[i * 2 + 1][j * 2] = 'b';
      ans[i * 2 + 1][j * 2 + 1] = 'b';
      j++;
      j %= N2;
      ans[i * 2][j * 2] = 'c';
      ans[i * 2][j * 2 + 1] = 'd';
      ans[i * 2 + 1][j * 2] = 'c';
      ans[i * 2 + 1][j * 2 + 1] = 'd';
    }
    rep(i, N) { cout << ans[i] << endl; }
  } else {
    int N2 = (N - 1) / 2;
    if (N >= 5) {
      int x, y;
      x = 0;
      y = 0;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'a';
        ans[x + 1][y] = 'e';
        ans[x][y + 1] = 'a';
        ans[x + 1][y + 1] = 'e';
        x += 2;
        y += 2;
      }
      x = N - 1;
      y = 0;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'b';
        ans[x - 1][y] = 'b';
        ans[x][y + 1] = 'f';
        ans[x - 1][y + 1] = 'f';
        x -= 2;
        y += 2;
      }
      x = N - 1;
      y = N - 1;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'c';
        ans[x - 1][y] = 'g';
        ans[x][y - 1] = 'c';
        ans[x - 1][y - 1] = 'g';
        x -= 2;
        y -= 2;
      }
      x = 0;
      y = N - 1;
      rep(i, N2 / 2 - 1) {
        ans[x][y] = 'd';
        ans[x + 1][y] = 'd';
        ans[x][y - 1] = 'h';
        ans[x + 1][y - 1] = 'h';
        x += 2;
        y -= 2;
      }
    }
    if (N2 % 2 == 1) {
      int x = 0;
      int y = 0;
      int count = 0;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y++;
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y++;
      x = 0;
      y = 6;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x++;
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x++;
      x = 6;
      y = 6;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      y--;
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      y--;
      x = 6;
      y = 0;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'a';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'b';
      x--;
      y++;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x--;
      ans[N2 - 3 + x][N2 - 3 + y] = 'c';
      x--;
      ans[N2 - 1][N2 - 1] = 'i';
      ans[N2 - 1][N2] = 'i';
      ans[N2][N2 + 1] = 'j';
      ans[N2 + 1][N2 + 1] = 'j';
    } else {
      int x = 0;
      int y = 0;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x++;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      y--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'a';
      x--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x--;
      ans[N2 - 2 + x][N2 - 2 + y] = 'b';
      x--;
      ans[N2 - 1][N2 - 1] = 'i';
      ans[N2 - 1][N2] = 'i';
      ans[N2][N2 + 1] = 'j';
      ans[N2 + 1][N2 + 1] = 'j';
    }

    rep(i, N) { cout << ans[i] << endl; }
  }
  return 0;
}