Problem p00141 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define F first
#define S second

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

char fld[200][200];
int N;
void init() {
  fill(*fld, *fld + 40000, ' ');
  REP(i, N + 4) fld[0][i] = fld[N + 3][i] = fld[i][0] = fld[i][N + 3] = '#';
}

void solve() {
  int x = 2, y = N + 1, d = 1;
  while (true) {
    fld[y][x] = '#';
    bool f2 = false;
    REP(i, 4) {
      int nd = (d + i) % 4;
      int ny = y + dy[nd];
      int nx = x + dx[nd];
      bool flag = true;
      REP(j, 4) {
        if (j == (nd + 2) % 4)
          continue;
        if (fld[ny + dy[j]][nx + dx[j]] == '#')
          flag = false;
      }
      if (flag) {
        x = nx;
        y = ny;
        d = nd;
        f2 = true;
        break;
      }
    }
    if (f2 == false)
      return;
  }
}

int main() {
  int n;
  cin >> n;
  bool flag = false;
  while (n--) {
    cin >> N;
    if (flag)
      cout << endl;
    else
      flag = true;
    init();
    solve();
    if (N == 2) {
      cout << "##" << endl;
      cout << "# " << endl;
      continue;
    }
    FOR(i, 2, N + 2) {
      FOR(j, 2, N + 2) cout << fld[i][j];
      cout << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define F first
#define S second

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

char fld[200][200];
int N;
void init() {
  fill(*fld, *fld + 40000, ' ');
  REP(i, N + 4) fld[0][i] = fld[N + 3][i] = fld[i][0] = fld[i][N + 3] = '#';
}

void solve() {
  int x = 2, y = N + 1, d = 1;
  while (true) {
    fld[y][x] = '#';
    REP(i, 4) {
      int nd = (d + i) % 4;
      int ny = y + dy[nd];
      int nx = x + dx[nd];
      bool flag = true;
      if (fld[ny][nx] == '#')
        return;
      REP(j, 4) {
        if (j == (nd + 2) % 4)
          continue;
        if (fld[ny + dy[j]][nx + dx[j]] == '#')
          flag = false;
      }
      if (flag) {
        x = nx;
        y = ny;
        d = nd;
        break;
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    cin >> N;
    init();
    solve();
    FOR(i, 2, N + 2) {
      FOR(j, 2, N + 2) cout << fld[i][j];
      cout << endl;
    }
  }

  return 0;
}