Problem p00141 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

inline int next(int d) { return (d + 1) % 4; }

int main() {
  int dataset, n;

  cin >> dataset;
  for (int cnt = 0; cnt < dataset; ++cnt) {
    if (cnt != 0)
      cout << "\\n";
    cin >> n;

    if (n == 1) {
      cout << "#\\n";
      continue;
    } else if (n == 2) {
      cout << "##\\n# \\n";
      continue;
    } else if (n == 3) {
      cout << "###\\n# #\\n# #\\n";
      continue;
    }

    vector<vector<bool>> map(n, vector<bool>(n, false));
    int d = 0, x = 0, y = n - 1;
    bool flag = true;

    while (flag) {
      map[y][x] = true;

      int nx = x + dx[d];
      int ny = y + dy[d];

      if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
        d = next(d);
        x += dx[d];
        y += dy[d];
        continue;
      }

      for (int i = 0; i < 4; ++i) {
        if ((i + 2) % 4 == d)
          continue;
        int nnx = nx + dx[i];
        int nny = ny + dy[i];

        if (nnx < 0 || nnx >= n || nny < 0 || nny >= n)
          continue;
        if (map[nny][nnx]) {
          d = next(d);
          break;
        }
      }
      x += dx[d];
      y += dy[d];

      for (int i = 0; i < 4; ++i) {
        if ((i + 2) % 4 == d)
          continue;
        nx = x + dx[i];
        ny = y + dy[i];

        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
          continue;
        if (map[ny][nx]) {
          flag = false;
          break;
        }
      }
    }

    // Output
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (map[i][j])
          cout << "#";
        else
          cout << " ";
      }
      cout << "\\n";
    }
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

inline int next(int d) { return (d + 1) % 4; }

int main() {
  int dataset, n;

  cin >> dataset;
  while (dataset--) {
    cin >> n;

    vector<vector<bool>> map(n, vector<bool>(n, false));
    int d = 0, x = 0, y = n - 1;
    bool flag = true;

    while (flag) {
      map[y][x] = true;

      int nx = x + dx[d];
      int ny = y + dy[d];

      if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
        d = next(d);
        x += dx[d];
        y += dy[d];
        continue;
      }

      for (int i = 0; i < 4; ++i) {
        if ((i + 2) % 4 == d)
          continue;
        int nnx = nx + dx[i];
        int nny = ny + dy[i];

        if (nnx < 0 || nnx >= n || nny < 0 || nny >= n)
          continue;
        if (map[nny][nnx]) {
          d = next(d);
          break;
        }
      }
      x += dx[d];
      y += dy[d];

      for (int i = 0; i < 4; ++i) {
        if ((i + 2) % 4 == d)
          continue;
        nx = x + dx[i];
        ny = y + dy[i];

        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
          continue;
        if (map[ny][nx]) {
          flag = false;
          break;
        }
      }
    }

    // Output
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (map[i][j])
          cout << "#";
        else
          cout << " ";
      }
      cout << "\\n";
    }
  }

  return 0;
}