Problem p01755 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

int main() {
  // ------ Variable, Input ------ //

  int H, W, ptr, cnt, sec, x, y, d;
  bool clear;

  vector<int> Par;

  vector<string> MAP;
  string S;

  cin >> H >> W;

  MAP = vector<string>(H);

  for (int i = 0; i < H; i++) {
    cin >> MAP[i];
  }

  cin >> S;

  // ------ Prepare ------ //

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (MAP[i][j] == 's') {
        x = j;
        y = i;
      }
    }
  }

  Par = vector<int>(S.size(), -1);

  stack<int> Par2;

  for (int i = 0; i < S.size(); i++) {
    if (S[i] == '[' || S[i] == '{') {
      Par2.push(i);
    }

    if (S[i] == ']' || S[i] == '}') {
      Par[i] = Par2.top();
      Par[Par2.top()] = i;

      Par2.pop();
    }
  }

  d = 0;
  ptr = 0;
  cnt = 0, sec = 0;
  clear = false;

  // ------ Move ------ //

  while (ptr < S.size()) {
    if (sec++ > 100000000) {
      break;
    }

    if (S[ptr] == '^') {
      if (MAP[y + dy[d]][x + dx[d]] != '#') {
        x += dx[d];
        y += dy[d];
      }

      cnt++;
    } else if (S[ptr] == 'v') {
      if (MAP[y - dy[d]][x - dx[d]] != '#') {
        x -= dx[d];
        y -= dy[d];
      }

      cnt++;
    } else if (S[ptr] == '<') {
      d -= 1;

      if (d < 0) {
        d += 4;
      }

      cnt++;
    } else if (S[ptr] == '>') {
      d += 1;

      if (d > 3) {
        d -= 4;
      }

      cnt++;
    } else if (S[ptr] == '[' || S[ptr] == '{') {
      string S2;
      S2 = S[ptr + 1];

      if (S2 == "~") {
        S2 += S[ptr + 2];
      }

      bool ok = false;

      if (S2 == "T") {
        ok = true;
      } else if (S2 == "N") {
        ok = (d == 0);
      } else if (S2 == "E") {
        ok = (d == 1);
      } else if (S2 == "S") {
        ok = (d == 2);
      } else if (S2 == "W") {
        ok = (d == 3);
      } else if (S2 == "~N") {
        ok = (d != 0);
      } else if (S2 == "~E") {
        ok = (d != 1);
      } else if (S2 == "~S") {
        ok = (d != 2);
      } else if (S2 == "~W") {
        ok = (d != 3);
      } else if (S2 == "C") {
        ok = (MAP[y + dy[d]][x + dx[d]] == '#');
      } else if (S2 == "~C") {
        ok = (MAP[y + dy[d]][x + dx[d]] != '#');
      }

      if (ok == true) {
        ptr += S2.size();
      } else {
        ptr = Par[ptr];
      }
    } else if (S[ptr] == '}') {
      ptr = Par[ptr] - 1;
    }

    if (MAP[y][x] == 'g') {
      clear = true;
      break;
    }

    ptr++;
  }

  if (clear == false) {
    cout << -1 << endl;
  } else {
    cout << cnt << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

int main() {
  // ------ Variable, Input ------ //

  int H, W, ptr, cnt, sec, x, y, d;
  bool clear;

  vector<int> Par;

  vector<string> MAP;
  string S;

  cin >> H >> W;

  MAP = vector<string>(H);

  for (int i = 0; i < H; i++) {
    cin >> MAP[i];
  }

  cin >> S;

  // ------ Prepare ------ //

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (MAP[i][j] == 's') {
        x = j;
        y = i;
      }
    }
  }

  Par = vector<int>(S.size(), -1);

  stack<int> Par2;

  for (int i = 0; i < S.size(); i++) {
    if (S[i] == '[' || S[i] == '{') {
      Par2.push(i);
    }

    if (S[i] == ']' || S[i] == '}') {
      Par[i] = Par2.top();
      Par[Par2.top()] = i;

      Par2.pop();
    }
  }

  d = 0;
  ptr = 0;
  cnt = 0, sec = 0;
  clear = false;

  // ------ Move ------ //

  while (ptr < S.size()) {
    if (sec++ > 30000000) {
      break;
    }

    if (S[ptr] == '^') {
      if (MAP[y + dy[d]][x + dx[d]] != '#') {
        x += dx[d];
        y += dy[d];
      }

      cnt++;
    } else if (S[ptr] == 'v') {
      if (MAP[y - dy[d]][x - dx[d]] != '#') {
        x -= dx[d];
        y -= dy[d];
      }

      cnt++;
    } else if (S[ptr] == '<') {
      d -= 1;

      if (d < 0) {
        d += 4;
      }

      cnt++;
    } else if (S[ptr] == '>') {
      d += 1;

      if (d > 3) {
        d -= 4;
      }

      cnt++;
    } else if (S[ptr] == '[' || S[ptr] == '{') {
      string S2;
      S2 = S[ptr + 1];

      if (S2 == "~") {
        S2 += S[ptr + 2];
      }

      bool ok = false;

      if (S2 == "T") {
        ok = true;
      } else if (S2 == "N") {
        ok = (d == 0);
      } else if (S2 == "E") {
        ok = (d == 1);
      } else if (S2 == "S") {
        ok = (d == 2);
      } else if (S2 == "W") {
        ok = (d == 3);
      } else if (S2 == "~N") {
        ok = (d != 0);
      } else if (S2 == "~E") {
        ok = (d != 1);
      } else if (S2 == "~S") {
        ok = (d != 2);
      } else if (S2 == "~W") {
        ok = (d != 3);
      } else if (S2 == "C") {
        ok = (MAP[y + dy[d]][x + dx[d]] == '#');
      } else if (S2 == "~C") {
        ok = (MAP[y + dy[d]][x + dx[d]] != '#');
      }

      if (ok == true) {
        ptr += S2.size();
      } else {
        ptr = Par[ptr];
      }
    } else if (S[ptr] == '}') {
      ptr = Par[ptr] - 1;
    }

    if (MAP[y][x] == 'g') {
      clear = true;
      break;
    }

    ptr++;
  }

  if (clear == false) {
    cout << -1 << endl;
  } else {
    cout << cnt << endl;
  }

  return 0;
}