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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll, unordered_map<ll, ll>> graph;

const ll e5 = 1 << 20;
const ll mod = 1000000007;
const ll e3 = 1 << 13;
const ll INF = 1ll << 60;

ll h, w;
ll a[51][51];

ll direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

string s;
vl x(2);
vl t(2);
ll d = 0;
ll cnt = 0;
ll clk = 0;

stack<ll> while_stack;

bool can_move(vl y) {
  if (a[y[0]][y[1]] != '#')
    return true;
  else
    return false;
}

bool condition(vl x, ll d, ll &i) {
  i++;
  if (s[i] == '~') {
    i++;
    if (s[i] == 'N')
      return d != 0;
    if (s[i] == 'E')
      return d != 1;
    if (s[i] == 'S')
      return d != 2;
    if (s[i] == 'W')
      return d != 3;
    if (s[i] == 'C') {
      vl y = x;
      y[0] += direction[d][0];
      y[1] += direction[d][1];
      if (can_move(y))
        return true;
      else
        return false;
    }
    if (s[i] == 'T')
      return false;
  } else {
    if (s[i] == 'N')
      return d == 0;
    if (s[i] == 'E')
      return d == 1;
    if (s[i] == 'S')
      return d == 2;
    if (s[i] == 'W')
      return d == 3;
    if (s[i] == 'C') {
      vl y = x;
      y[0] += direction[d][0];
      y[1] += direction[d][1];
      if (can_move(y))
        return false;
      else
        return true;
    }
    if (s[i] == 'T')
      return true;
  }
}

int main() {
  cin >> h >> w;
  for (ll i = 0; i < h; i++) {
    string hoge;
    cin >> hoge;
    for (ll j = 0; j < w; j++) {
      a[i][j] = hoge[j];
      if (hoge[j] == 's') {
        x[0] = i;
        x[1] = j;
      }
      if (hoge[j] == 'g') {
        t[0] = i;
        t[1] = j;
      }
    }
  }
  cin >> s;
  // cerr << 0 << endl;
  ll p = 0;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == '^' || s[i] == 'v' || s[i] == '>' || s[i] == '<')
      p++;
  }
  p = s.size();
  // cerr << 1 << endl;
  for (ll i = 0; i != s.size();) {
    // cerr << 1 << " " << i << " ";
    // cerr << x[0] << " " << x[1] << endl;
    ll mem = i;
    vl y = x;
    switch (s[i]) {
    case '[':
      if (condition(x, d, i)) {
        i++;
        continue;
      } else {
        ll k = 1;
        while (k > 0) {
          if (s[i] == ']')
            k--;
          if (s[i] == '[')
            k++;
          i++;
        }
      }
      break;
    case ']':
      i++;
      break;
    case '{':
      if (condition(x, d, i)) {
        while_stack.push(mem);
        i++;
      } else {
        ll k = 1;
        while (k > 0) {
          if (s[i] == '}')
            k--;
          if (s[i] == '{')
            k++;
          i++;
        }
      }
      break;

    case '}':
      i = while_stack.top();
      while_stack.pop();
      break;
    case '^':
      y[0] += direction[d][0];
      y[1] += direction[d][1];
      if (can_move(y)) {
        x = y;
      }
      i++;
      cnt++;
      break;
    case 'v':
      y[0] -= direction[d][0];
      y[1] -= direction[d][1];
      if (can_move(y)) {
        x = y;
      }
      i++;
      cnt++;
      break;
    case '<':
      d = (d - 1 + 4) % 4;
      i++;
      cnt++;
      break;
    case '>':
      d = (d + 1) % 4;
      i++;
      cnt++;
      break;
    }
    clk++;
    if (a[x[0]][x[1]] == 'g') {
      cout << cnt << endl;
      return 0;
    }
    if (clk > h * w * 4 * p) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll, unordered_map<ll, ll>> graph;

const ll e5 = 1 << 20;
const ll mod = 1000000007;
const ll e3 = 1 << 13;
const ll INF = 1ll << 60;

ll h, w;
ll a[51][51];

ll direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

string s;
vl x(2);
vl t(2);
ll d = 0;
ll cnt = 0;

stack<ll> while_stack;

bool can_move(vl y) {
  if (a[y[0]][y[1]] != '#')
    return true;
  else
    return false;
}

bool condition(vl x, ll d, ll &i) {
  i++;
  if (s[i] == '~') {
    i++;
    if (s[i] == 'N')
      return d != 0;
    if (s[i] == 'E')
      return d != 1;
    if (s[i] == 'S')
      return d != 2;
    if (s[i] == 'W')
      return d != 3;
    if (s[i] == 'C') {
      vl y = x;
      y[0] += direction[d][0];
      y[1] += direction[d][1];
      if (can_move(y))
        return true;
      else
        return false;
    }
    if (s[i] == 'T')
      return false;
  } else {
    if (s[i] == 'N')
      return d == 0;
    if (s[i] == 'E')
      return d == 1;
    if (s[i] == 'S')
      return d == 2;
    if (s[i] == 'W')
      return d == 3;
    if (s[i] == 'C') {
      vl y = x;
      y[0] += direction[d][0];
      y[1] += direction[d][1];
      if (can_move(y))
        return false;
      else
        return true;
    }
    if (s[i] == 'T')
      return true;
  }
}

int main() {
  cin >> h >> w;
  for (ll i = 0; i < h; i++) {
    string hoge;
    cin >> hoge;
    for (ll j = 0; j < w; j++) {
      a[i][j] = hoge[j];
      if (hoge[j] == 's') {
        x[0] = i;
        x[1] = j;
      }
      if (hoge[j] == 'g') {
        t[0] = i;
        t[1] = j;
      }
    }
  }
  cin >> s;
  // cerr << 0 << endl;
  ll p = 0;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == '^' || s[i] == 'v' || s[i] == '>' || s[i] == '<')
      p++;
  }
  // cerr << 1 << endl;
  for (ll i = 0; i != s.size();) {
    // cerr << 1 << " " << i << " ";
    // cerr << x[0] << " " << x[1] << endl;
    ll mem = i;
    vl y = x;
    switch (s[i]) {
    case '[':
      if (condition(x, d, i)) {
        i++;
        continue;
      } else {
        while (s[i] != ']')
          i++;
        i++;
      }
      break;
    case ']':
      i++;
      break;
    case '{':
      if (condition(x, d, i)) {
        while_stack.push(mem);
        i++;
      } else {
        ll k = 1;
        while (k > 0) {
          if (s[i] == '}')
            k--;
          if (s[i] == '{')
            k++;
          i++;
        }
      }
      break;

    case '}':
      i = while_stack.top();
      while_stack.pop();
      break;
    case '^':
      y[0] += direction[d][0];
      y[1] += direction[d][1];
      if (can_move(y)) {
        x = y;
      }
      i++;
      cnt++;
      break;
    case 'v':
      y[0] -= direction[d][0];
      y[1] -= direction[d][1];
      if (can_move(y)) {
        x = y;
      }
      i++;
      cnt++;
      break;
    case '<':
      d = (d - 1 + 4) % 4;
      i++;
      cnt++;
      break;
    case '>':
      d = (d + 1) % 4;
      i++;
      cnt++;
      break;
    }
    if (a[x[0]][x[1]] == 'g') {
      cout << cnt << endl;
      return 0;
    }
    if (cnt > h * w * 4 * p) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}