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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define mk make_pair

int main() {
  int time;
  cin >> time;
  for (int p = 0; p < time; p++) {

    if (p != 0) {
      cout << endl;
    }
    int n;
    cin >> n;
    char a[1000][1000];
    fill(&a[0][0], &a[999][1000], ' ');
    char t = 'u';
    a[n + 99][100] = '#';
    pair<int, int> w = mk(n + 99, 100);
    int j = 0;
    while (j != 2) {
      if (t == 'u') {
        if (a[w.first - 1][w.second] == '#') {
          break;
        }
        if (a[w.first - 2][w.second] == '#' || w.first == 100 ||
            a[w.first - 1][w.second + 1] == '#') {
          t = 'r';
          j++;
          continue;
        }
        w.first--;
      }
      if (t == 'r') {
        if (a[w.first][w.second + 1] == '#') {
          break;
        }
        if (a[w.first][w.second + 2] == '#' || w.second == 99 + n ||
            a[w.first + 1][w.second + 1] == '#') {
          t = 'd';
          j++;
          continue;
        }
        w.second++;
      }
      if (t == 'd') {
        if (a[w.first + 1][w.second] == '#') {
          break;
        }
        if (a[w.first + 2][w.second] == '#' || w.first == 99 + n ||
            a[w.first + 1][w.second - 1] == '#') {
          t = 'l';
          j++;
          continue;
        }
        w.first++;
      }
      if (t == 'l') {
        if (a[w.first][w.second - 1] == '#') {
          break;
        }
        if (a[w.first][w.second - 2] == '#' || w.second == 100 ||
            a[w.first - 1][w.second - 1] == '#') {
          t = 'u';
          j++;
          continue;
        }
        w.second--;
      }
      a[w.first][w.second] = '#';
      j = 0;
    }

    for (int i = 100; i < n + 100; i++) {
      for (int t = 100; t < n + 100; t++) {
        cout << a[i][t];
      }
      cout << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <iostream>
using namespace std;

int main() {
  int time;
  cin >> time;
  for (int i = 0; i < time; i++) {
    if (i != 0) {
      cout << endl;
    }
    char t = 'u';
    int n;
    cin >> n;
    char a[1000][1000] = {};
    fill(&a[0][0], &a[999][1000], ' ');
    a[n - 1][0] = '#';
    pair<int, int> w = make_pair(n - 1, 0);
    while (1) {
      // cout<<t<<" "<<w.first<<" "<<w.second<<endl;
      if (t == 'u') {
        if (a[w.first - 1][w.second] == '#' ||
            a[w.first - 1][w.second + 1] == '#') {
          break;
        }
        if (w.first == 0 || a[w.first - 2][w.second] == '#') {
          t = 'r';
          continue;
        }
        // w=make_pair(w.first+1,w.second);
        w.first--;
        a[w.first][w.second] = '#';
      }
      if (t == 'r') {
        if (a[w.first][w.second + 1] == '#' ||
            a[w.first + 1][w.second + 1] == '#') {
          break;
        }
        if (w.second == n - 1 || a[w.first][w.second + 2] == '#') {
          t = 'd';
          continue;
        }
        // w=make_pair(w.first,w.second+1);
        w.second++;
        a[w.first][w.second] = '#';
      }
      if (t == 'd') {
        if (a[w.first + 1][w.second] == '#' ||
            a[w.first + 1][w.second - 1] == '#') {
          break;
        }
        if (w.first == n - 1 || a[w.first + 2][w.second] == '#') {
          t = 'l';
          continue;
        }
        // w=make_pair(w.first-1,w.second);
        w.first++;
        a[w.first][w.second] = '#';
      }
      if (t == 'l') {
        if (a[w.first][w.second - 1] == '#' ||
            a[w.first + 1][w.second - 1] == '#') {
          break;
        }
        if (w.second == 0 || a[w.first][w.second - 2] == '#') {
          t = 'u';
          continue;
        }
        // w=make_pair(w.first,w.second-1);
        w.second--;
        a[w.first][w.second] = '#';
      }
    }
    for (int i = 0; i < n; i++) {
      for (int t = 0; t < n; t++) {
        cout << a[i][t];
      }
      cout << endl;
    }
  }

  return 0;
}