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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (n); ++(i))
using namespace std;

char board[55][55];

string s;
int match[10010];

int cy = -1, cx = -1, dir = 0;
int gy = -1, gx = -1;
const int dy[4] = {-1, 0, 1, 0};
const int dx[4] = {0, 1, 0, -1};

const int limit = 10000010;

int cnt = 0;
int ans = 0;
bool term = false;

void move(int i) {
  ans++;
  if (cnt > limit)
    term = true;

  if (s[i] == '^') {
    const int ny = cy + dy[dir];
    const int nx = cx + dx[dir];

    if (board[ny][nx] != '#') {
      cy = ny;
      cx = nx;
    }
  }

  if (s[i] == 'v') {
    const int ny = cy + dy[dir ^ 2];
    const int nx = cx + dx[dir ^ 2];

    if (board[ny][nx] != '#') {
      cy = ny;
      cx = nx;
    }
  }

  if (s[i] == '>') {
    dir = (dir + 1) % 4;
  }

  if (s[i] == '<') {
    dir = (dir + 3) % 4;
  }

  // cerr << cy << " " << cx << endl;
}

bool condition(char c, bool flag) {
  if (cnt > limit)
    return false;
  const int ny = cy + dy[dir];
  const int nx = cx + dx[dir];

  bool cond;
  if (c == 'T')
    cond = true;
  if (c == 'N')
    cond = (dir == 0);
  if (c == 'E')
    cond = (dir == 1);
  if (c == 'S')
    cond = (dir == 2);
  if (c == 'W')
    cond = (dir == 3);
  if (c == 'C')
    cond = (board[ny][nx] == '#');

  if (cond)
    return flag;
  else
    return flag ^ true;
}

void program(int f, int t) {
  cnt++;
  if (cnt > limit)
    term = true;
  if (gy == cy and gx == cx)
    term = true;
  if (f >= t)
    return;
  if (term)
    return;

  if (s[f] == '[') {
    int nf = f + 1;
    bool flag = true;
    char c = s[nf++];

    if (c == '~') {
      flag = false;
      c = s[nf++];
    }

    const int nt = match[f];

    if (term == false and condition(c, flag)) {
      program(nf, nt);
    }

    program(nt + 1, t);
  } else if (s[f] == '{') {
    int nf = f + 1;

    bool flag = true;
    char c = s[nf++];

    if (c == '~') {
      flag = false;
      c = s[nf++];
    }

    const int nt = match[f];

    while (term == false and condition(c, flag)) {
      program(nf, nt);
    }

    program(nt + 1, t);
  } else {
    int nf = f + 1, nt = t;
    move(f);
    program(nf, nt);
  }
}

int main(void) {
  int h, w;
  cin >> h >> w;

  rep(i, h) rep(j, w) cin >> board[i][j];

  rep(i, h) rep(j, w) {
    if (board[i][j] == 's') {
      cy = i, cx = j, dir = 0;
    }
    if (board[i][j] == 'g') {
      gy = i, gx = j;
    }
  }

  cin >> s;
  const int n = s.size();

  stack<int> stk;
  rep(i, n) {
    if (s[i] == '[' or s[i] == '{') {
      stk.push(i);
    }

    if (s[i] == ']' or s[i] == '}') {
      int cur = stk.top();
      stk.pop();
      match[i] = cur, match[cur] = i;
    }
  }

  program(0, n);

  if (cy == gy and cx == gx)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (n); ++(i))
using namespace std;

char board[55][55];

string s;
int match[10010];

int cy = -1, cx = -1, dir = 0;
int gy = -1, gx = -1;
const int dy[4] = {-1, 0, 1, 0};
const int dx[4] = {0, 1, 0, -1};

const int limit = 100010;

int cnt = 0;
bool term = false;

void move(int i) {
  cnt++;
  if (cnt > limit)
    term = true;

  if (s[i] == '^') {
    const int ny = cy + dy[dir];
    const int nx = cx + dx[dir];

    if (board[ny][nx] != '#') {
      cy = ny;
      cx = nx;
    }
  }

  if (s[i] == 'v') {
    const int ny = cy + dy[dir ^ 2];
    const int nx = cx + dx[dir ^ 2];

    if (board[ny][nx] != '#') {
      cy = ny;
      cx = nx;
    }
  }

  if (s[i] == '>') {
    dir = (dir + 1) % 4;
  }

  if (s[i] == '<') {
    dir = (dir + 3) % 4;
  }

  // cerr << cy << " " << cx << endl;
}

bool condition(char c, bool flag) {
  if (cnt > limit)
    return false;
  const int ny = cy + dy[dir];
  const int nx = cx + dx[dir];

  bool cond;
  if (c == 'T')
    cond = true;
  if (c == 'N')
    cond = (dir == 0);
  if (c == 'E')
    cond = (dir == 1);
  if (c == 'S')
    cond = (dir == 2);
  if (c == 'W')
    cond = (dir == 3);
  if (c == 'C')
    cond = (board[ny][nx] == '#');

  if (cond)
    return flag;
  else
    return flag ^ true;
}

void program(int f, int t) {
  // cerr << f << " " << t << endl;
  if (gy == cy and gx == cx)
    term = true;
  if (f >= t)
    return;
  if (term)
    return;

  if (s[f] == '[') {
    int nf = f + 1;
    bool flag = true;
    char c = s[nf++];

    if (c == '~') {
      flag = false;
      c = s[nf++];
    }

    const int nt = match[f];

    if (term == false and condition(c, flag)) {
      program(nf, nt);
    }

    program(nt + 1, t);
  } else if (s[f] == '{') {
    int nf = f + 1;

    bool flag = true;
    char c = s[nf++];

    if (c == '~') {
      flag = false;
      c = s[nf++];
    }

    const int nt = match[f];

    while (term == false and condition(c, flag)) {
      program(nf, nt);
    }

    program(nt + 1, t);
  } else {
    int nf = f + 1, nt = t;
    move(f);
    program(nf, nt);
  }
}

int main(void) {
  int h, w;
  cin >> h >> w;

  rep(i, h) rep(j, w) cin >> board[i][j];

  rep(i, h) rep(j, w) {
    if (board[i][j] == 's') {
      cy = i, cx = j, dir = 0;
    }
    if (board[i][j] == 'g') {
      gy = i, gx = j;
    }
  }

  cin >> s;
  const int n = s.size();

  stack<int> stk;
  rep(i, n) {
    if (s[i] == '[' or s[i] == '{') {
      stk.push(i);
    }

    if (s[i] == ']' or s[i] == '}') {
      int cur = stk.top();
      stk.pop();
      match[i] = cur, match[cur] = i;
    }
  }

  program(0, n);

  if (cy == gy and cx == gx)
    cout << cnt << endl;
  else
    cout << -1 << endl;

  return 0;
}