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

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

int h, w, ni, nj, nmuki = 0, ans = -1, sum = 0;
string s;
char grid[51][51];
int Y[4] = {-1, 0, 1, 0};
int X[4] = {0, 1, 0, -1};
bool memo[51][51][4][1001] = {}, C = false;

bool check(char c) {
  bool flag = false;
  if (c == 'N' && nmuki == 0)
    flag = true;
  else if (c == 'E' && nmuki == 1)
    flag = true;
  else if (c == 'S' && nmuki == 2)
    flag = true;
  else if (c == 'W' && nmuki == 3)
    flag = true;
  else if (c == 'C' && grid[ni + Y[nmuki]][nj + X[nmuki]] == '#')
    flag = true;
  else if (c == 'T')
    flag = true;
  return flag;
}

void solve(int a, int b) {

  for (int k = a; k < b; k++) {
    // cout<<ni<<" "<<nj<<" "<<nmuki<<" "<<sum<<" "<<s[k]<<endl;
    if (C)
      return;
    if (memo[ni][nj][nmuki][k]) {
      C = true;
      return;
    }
    memo[ni][nj][nmuki][k] = true;
    if (s[k] == '[') {
      bool B;
      int index = k + 1, l = 0, r = 0;
      if (s[index] != '~')
        B = check(s[index]);
      else
        B = !check(s[++index]);
      for (int i = k; i < s.length(); i++) {
        if (s[i] == '[')
          l++;
        if (s[i] == ']')
          r++;
        if (l == r) {
          if (B)
            solve(index + 1, i);
          k = i;
          break;
        }
      }
    } else if (s[k] == '{') {
      int tmp = k;
      bool B;
      int index = k + 1, l = 0, r = 0;
      if (s[index] != '~')
        B = check(s[index]);
      else
        B = !check(s[++index]);
      for (int i = k; i < s.length(); i++) {
        if (s[i] == '{')
          l++;
        if (s[i] == '}')
          r++;
        if (l == r) {
          tmp = i;
          break;
        }
      }
      if (B)
        solve(index + 1, tmp);
      if (C)
        return;
      if (ans != -1)
        return;
      if (B)
        k = k - 1;
      else
        k = tmp;
    } else if (s[k] == '^') {
      int y = ni + Y[nmuki], x = nj + X[nmuki];
      if (grid[y][x] != '#') {
        ni = y;
        nj = x;
      }
      sum++;
    } else if (s[k] == '>') {
      nmuki = (nmuki + 1) % 4;
      sum++;
    } else if (s[k] == 'v') {
      int y = ni + Y[(nmuki + 2) % 4], x = nj + X[(nmuki + 2) % 4];
      if (grid[y][x] != '#') {
        ni = y;
        nj = x;
      }
      sum++;
    } else if (s[k] == '<') {
      nmuki = (nmuki + 3) % 4;
      sum++;
    }
    if (grid[ni][nj] == 'g') {
      ans = sum;
      return;
    }
  }
  return;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 's') {
        ni = i;
        nj = j;
      }
    }
  }
  cin >> s;
  memo[0][0][0][0] = true;
  solve(0, s.length());
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int h, w, ni, nj, nmuki = 0, ans = -1, sum = 0;
string s;
char grid[51][51];
int Y[4] = {-1, 0, 1, 0};
int X[4] = {0, 1, 0, -1};
bool memo[51][51][4][1001] = {}, C = false;

bool check(char c) {
  bool flag = false;
  if (c == 'N' && nmuki == 0)
    flag = true;
  else if (c == 'E' && nmuki == 1)
    flag = true;
  else if (c == 'S' && nmuki == 2)
    flag = true;
  else if (c == 'W' && nmuki == 3)
    flag = true;
  else if (c == 'C' && grid[ni + Y[nmuki]][nj + X[nmuki]] == '#')
    flag = true;
  else if (c == 'T')
    flag = true;
  return flag;
}

void solve(int a, int b) {

  for (int k = a; k < b; k++) {
    // cout<<ni<<" "<<nj<<" "<<nmuki<<" "<<sum<<" "<<s[k]<<endl;
    if (C)
      return;
    if (memo[ni][nj][nmuki][k]) {
      C = true;
      return;
    }
    memo[ni][nj][nmuki][k] = true;
    if (s[k] == '[') {
      bool B;
      int index = k + 1, l = 0, r = 0;
      if (s[index] != '~')
        B = check(s[index]);
      else
        B = !check(s[++index]);
      for (int i = k; i < s.length(); i++) {
        if (s[i] == '[')
          l++;
        if (s[i] == ']')
          r++;
        if (l == r) {
          if (B)
            solve(index + 1, i);
          k = i;
          break;
        }
      }
    } else if (s[k] == '{') {
      int tmp = k;
      while (1) {
        bool B;
        int index = k + 1, l = 0, r = 0;
        if (s[index] != '~')
          B = check(s[index]);
        else
          B = !check(s[++index]);
        for (int i = k; i < s.length(); i++) {
          if (s[i] == '{')
            l++;
          if (s[i] == '}')
            r++;
          if (l == r) {
            tmp = i;
            break;
          }
        }
        if (!B)
          break;
        solve(index + 1, tmp);
        if (C)
          return;
        if (ans != -1)
          return;
      }
      k = tmp;
    } else if (s[k] == '^') {
      int y = ni + Y[nmuki], x = nj + X[nmuki];
      if (grid[y][x] != '#') {
        ni = y;
        nj = x;
      }
      sum++;
    } else if (s[k] == '>') {
      nmuki = (nmuki + 1) % 4;
      sum++;
    } else if (s[k] == 'v') {
      int y = ni + Y[(nmuki + 2) % 4], x = nj + X[(nmuki + 2) % 4];
      if (grid[y][x] != '#') {
        ni = y;
        nj = x;
      }
      sum++;
    } else if (s[k] == '<') {
      nmuki = (nmuki + 3) % 4;
      sum++;
    }
    if (grid[ni][nj] == 'g') {
      ans = sum;
      return;
    }
  }
  return;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 's') {
        ni = i;
        nj = j;
      }
    }
  }
  cin >> s;
  memo[0][0][0][0] = true;
  solve(0, s.length());
  cout << ans << endl;
  return 0;
}