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

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {-1, 0, 1, 0}, vx[] = {0, 1, 0, -1};

const string p = "^v<>";

int H, W;
string S[50], P;
int x, y, dir;
int nxt[1001];
int step;

bool check(int &idx) {
  bool flip = false;
  if (P[idx] == '~')
    flip = true, ++idx;
  switch (P[idx++]) {
  case 'N':
    return (dir == 0) ^ flip;
  case 'E':
    return (dir == 1) ^ flip;
  case 'S':
    return (dir == 2) ^ flip;
  case 'W':
    return (dir == 3) ^ flip;
  case 'C':
    return (S[y + vy[dir]][x + vx[dir]] == '#') ^ flip;
  default:
    return !flip;
  }
}

void ai(int &idx) {
  if (step == 11451419) {
    cout << -1 << endl;
    exit(0);
  }
  int nowidx = idx;
  if (p.find(P[idx]) != string::npos) {
    ++step;
    if (P[idx] == '^') {
      if (S[y + vy[dir]][x + vx[dir]] != '#') {
        y += vy[dir], x += vx[dir];
      }
    } else if (P[idx] == 'v') {
      if (S[y - vy[dir]][x - vx[dir]] != '#') {
        y -= vy[dir], x -= vx[dir];
      }
    } else if (P[idx] == '<') {
      (dir += 3) %= 4;
    } else {
      (dir += 1) %= 4;
    }

    if (S[y][x] == 'g') {
      cout << step << endl;
      exit(0);
    }
    ++idx;
  } else if (P[idx] == '[') {
    if (check(++idx)) {
      while (P[idx] != ']')
        ai(idx);
    }
    idx = nxt[nowidx];
  } else if (P[idx] == '{') {
    int proc = 0;
    while (check(++idx)) {
      if (proc++ >= 11451419) {
        cout << -1 << endl;
        exit(0);
      }
      while (P[idx] != '}')
        ai(idx);
      idx = nowidx;
    }
    idx = nxt[nowidx];
  }
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
    for (int j = 0; j < W; j++) {
      if (S[i][j] == 's')
        x = j, y = i;
    }
  }
  cin >> P;
  P += "$";

  for (int i = 0; i < P.size(); i++) {
    if (P[i] == '[' || P[i] == '{') {
      int sum = 0;
      for (int j = i; j < P.size(); j++) {
        if (P[j] == '[' || P[j] == '{')
          ++sum;
        else if (P[j] == ']' || P[j] == '}')
          --sum;
        if (sum == 0) {
          nxt[i] = j + 1;
          break;
        }
      }
    }
  }

  int ptr = 0;
  while (P[ptr] != '$')
    ai(ptr);
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int vy[] = {-1, 0, 1, 0}, vx[] = {0, 1, 0, -1};

const string p = "^v<>";

int H, W;
string S[50], P;
int x, y, dir;
int nxt[1001];
int step;

bool check(int &idx) {
  bool flip = false;
  if (P[idx] == '~')
    flip = true, ++idx;
  switch (P[idx++]) {
  case 'N':
    return (dir == 0) ^ flip;
  case 'E':
    return (dir == 1) ^ flip;
  case 'S':
    return (dir == 2) ^ flip;
  case 'W':
    return (dir == 3) ^ flip;
  case 'C':
    return (S[y + vy[dir]][x + vx[dir]] == '#') ^ flip;
  default:
    return !flip;
  }
}

void ai(int &idx) {
  if (step == 114514) {
    cout << -1 << endl;
    exit(0);
  }
  int nowidx = idx;
  if (p.find(P[idx]) != string::npos) {
    ++step;
    if (P[idx] == '^') {
      if (S[y + vy[dir]][x + vx[dir]] != '#') {
        y += vy[dir], x += vx[dir];
      }
    } else if (P[idx] == 'v') {
      if (S[y - vy[dir]][x - vx[dir]] != '#') {
        y -= vy[dir], x -= vx[dir];
      }
    } else if (P[idx] == '<') {
      (dir += 3) %= 4;
    } else {
      (dir += 1) %= 4;
    }

    if (S[y][x] == 'g') {
      cout << step << endl;
      exit(0);
    }
    ++idx;
  } else if (P[idx] == '[') {
    if (check(++idx)) {
      while (P[idx] != ']')
        ai(idx);
    }
    idx = nxt[nowidx];
  } else if (P[idx] == '{') {
    while (check(++idx)) {
      while (P[idx] != '}')
        ai(idx);
      idx = nowidx;
    }
    idx = nxt[nowidx];
  }
}

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
    for (int j = 0; j < W; j++) {
      if (S[i][j] == 's')
        x = j, y = i;
    }
  }
  cin >> P;
  P += "$";

  for (int i = 0; i < P.size(); i++) {
    if (P[i] == '[' || P[i] == '{') {
      int sum = 0;
      for (int j = i; j < P.size(); j++) {
        if (P[j] == '[' || P[j] == '{')
          ++sum;
        else if (P[j] == ']' || P[j] == '}')
          --sum;
        if (sum == 0) {
          nxt[i] = j + 1;
          break;
        }
      }
    }
  }

  int ptr = 0;
  while (P[ptr] != '$')
    ai(ptr);
  cout << -1 << endl;
}