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

#include <iostream>
#include <string>
using namespace std;

int x[50][50], cx, cy, cz, sum, sum2, chain, h, w, gx, gy, pass, line;
char a;
string S, T;
bool y;

void WHILE();
void PASS();
void ugoku(char W);
void IF();
bool hantei();

void ugoku(char W) {
  if (W == '^') {
    if (cz == 0 && x[cy - 1][cx] == 0) {
      cy--;
    }
    if (cz == 1 && x[cy][cx + 1] == 0) {
      cx++;
    }
    if (cz == 2 && x[cy + 1][cx] == 0) {
      cy++;
    }
    if (cz == 3 && x[cy][cx - 1] == 0) {
      cx--;
    }
    sum++;
  }
  if (W == 'v') {
    if (cz == 2 && x[cy - 1][cx] == 0) {
      cy--;
    }
    if (cz == 3 && x[cy][cx + 1] == 0) {
      cx++;
    }
    if (cz == 0 && x[cy + 1][cx] == 0) {
      cy++;
    }
    if (cz == 1 && x[cy][cx - 1] == 0) {
      cx--;
    }
    sum++;
  }
  if (W == '>') {
    cz += 1;
    cz %= 4;
    sum++;
  }
  if (W == '<') {
    cz += 3;
    cz %= 4;
    sum++;
  }
  if (W == '[') {
    IF();
  }
  if (W == '}') {
    PASS();
  }
  if (W == '{') {
    WHILE();
  }
}

void IF() {
  T = "";
  chain = 0;
  for (int i = line + 1; i < line + 3; i++) {
    T += S[i];
  }
  y = hantei();
  if (y == false) {
    for (int i = line; i < S.size(); i++) {
      if (S[i] == '[') {
        chain++;
      }
      if (S[i] == ']') {
        chain--;
      }
      if (chain == 0) {
        line = i;
        break;
      }
    }
  }
}

void WHILE() {
  T = "";
  chain = 0;
  for (int i = line + 1; i < line + 3; i++) {
    T += S[i];
  }
  y = hantei();
  if (y == false) {
    for (int i = line; i < S.size(); i++) {
      if (S[i] == '{') {
        chain++;
      }
      if (S[i] == '}') {
        chain--;
      }
      if (chain == 0) {
        line = i;
        break;
      }
    }
  }
}

void PASS() {
  chain = 0;
  for (int i = line; i >= 0; i--) {
    if (S[i] == '}') {
      chain++;
    }
    if (S[i] == '{') {
      chain--;
    }
    if (chain == 0) {
      pass = i;
      break;
    }
  }
  line = pass - 1;
}

bool hantei() {
  if (T[0] == 'N' && cz == 0) {
    return true;
  }
  if (T[0] == 'E' && cz == 1) {
    return true;
  }
  if (T[0] == 'S' && cz == 2) {
    return true;
  }
  if (T[0] == 'W' && cz == 3) {
    return true;
  }
  if (T[0] == 'C') {
    if (cz == 0 && x[cy - 1][cx] == 1) {
      return true;
    }
    if (cz == 1 && x[cy][cx + 1] == 1) {
      return true;
    }
    if (cz == 2 && x[cy + 1][cx] == 1) {
      return true;
    }
    if (cz == 3 && x[cy][cx - 1] == 1) {
      return true;
    }
  }
  if (T[0] == 'T') {
    return true;
  }
  if (T[0] == '~') {
    if (T[1] == 'N' && cz != 0) {
      return true;
    }
    if (T[1] == 'E' && cz != 1) {
      return true;
    }
    if (T[1] == 'S' && cz != 2) {
      return true;
    }
    if (T[1] == 'W' && cz != 3) {
      return true;
    }
    if (T[1] == 'C') {
      if (cz == 0 && x[cy - 1][cx] != 1) {
        return true;
      }
      if (cz == 1 && x[cy][cx + 1] != 1) {
        return true;
      }
      if (cz == 2 && x[cy + 1][cx] != 1) {
        return true;
      }
      if (cz == 3 && x[cy][cx - 1] != 1) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a;
      if (a == '#') {
        x[i][j] = 1;
      }
      if (a == 'g') {
        gx = j;
        gy = i;
      }
      if (a == 's') {
        cx = j;
        cy = i;
      }
    }
  }
  cin >> S;
  line = 0;
  while (line < S.size()) {
    ugoku(S[line]);
    line++;
    if (cx == gx && cy == gy) {
      goto F;
    }
    sum2++;
    if (sum2 > 20000000) {
      sum = -1;
      break;
    }
  }
  sum = -1;
F:;
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int x[50][50], cx, cy, cz, sum, chain, h, w, gx, gy, pass, line;
char a;
string S, T;
bool y;

void WHILE();
void PASS();
void ugoku(char W);
void IF();
bool hantei();

void ugoku(char W) {
  if (W == '^') {
    if (cz == 0 && x[cy - 1][cx] == 0) {
      cy--;
    }
    if (cz == 1 && x[cy][cx + 1] == 0) {
      cx++;
    }
    if (cz == 2 && x[cy + 1][cx] == 0) {
      cy++;
    }
    if (cz == 3 && x[cy][cx - 1] == 0) {
      cx--;
    }
    sum++;
  }
  if (W == 'v') {
    if (cz == 2 && x[cy - 1][cx] == 0) {
      cy--;
    }
    if (cz == 3 && x[cy][cx + 1] == 0) {
      cx++;
    }
    if (cz == 0 && x[cy + 1][cx] == 0) {
      cy++;
    }
    if (cz == 1 && x[cy][cx - 1] == 0) {
      cx--;
    }
    sum++;
  }
  if (W == '>') {
    cz += 1;
    cz %= 4;
    sum++;
  }
  if (W == '<') {
    cz += 3;
    cz %= 4;
    sum++;
  }
  if (W == '[') {
    IF();
  }
  if (W == '}') {
    PASS();
  }
  if (W == '{') {
    WHILE();
  }
}

void IF() {
  T = "";
  chain = 0;
  for (int i = line + 1; i < line + 3; i++) {
    T += S[i];
  }
  y = hantei();
  if (y == false) {
    for (int i = line; i < S.size(); i++) {
      if (S[i] == '[') {
        chain++;
      }
      if (S[i] == ']') {
        chain--;
      }
      if (chain == 0) {
        line = i;
        break;
      }
    }
  }
}

void WHILE() {
  T = "";
  chain = 0;
  for (int i = line + 1; i < line + 3; i++) {
    T += S[i];
  }
  y = hantei();
  if (y == false) {
    for (int i = line; i < S.size(); i++) {
      if (S[i] == '{') {
        chain++;
      }
      if (S[i] == '}') {
        chain--;
      }
      if (chain == 0) {
        line = i;
        break;
      }
    }
  }
}

void PASS() {
  chain = 0;
  for (int i = line; i >= 0; i--) {
    if (S[i] == '}') {
      chain++;
    }
    if (S[i] == '{') {
      chain--;
    }
    if (chain == 0) {
      pass = i;
      break;
    }
  }
  line = pass - 1;
}

bool hantei() {
  if (T[0] == 'N' && cz == 0) {
    return true;
  }
  if (T[0] == 'E' && cz == 1) {
    return true;
  }
  if (T[0] == 'S' && cz == 2) {
    return true;
  }
  if (T[0] == 'W' && cz == 3) {
    return true;
  }
  if (T[0] == 'C') {
    if (cz == 0 && x[cy - 1][cx] == 1) {
      return true;
    }
    if (cz == 1 && x[cy][cx + 1] == 1) {
      return true;
    }
    if (cz == 2 && x[cy + 1][cx] == 1) {
      return true;
    }
    if (cz == 3 && x[cy][cx - 1] == 1) {
      return true;
    }
  }
  if (T[0] == 'T') {
    return true;
  }
  if (T[0] == '~') {
    if (T[1] == 'N' && cz != 0) {
      return true;
    }
    if (T[1] == 'E' && cz != 1) {
      return true;
    }
    if (T[1] == 'S' && cz != 2) {
      return true;
    }
    if (T[1] == 'W' && cz != 3) {
      return true;
    }
    if (T[1] == 'C') {
      if (cz == 0 && x[cy - 1][cx] != 1) {
        return true;
      }
      if (cz == 1 && x[cy][cx + 1] != 1) {
        return true;
      }
      if (cz == 2 && x[cy + 1][cx] != 1) {
        return true;
      }
      if (cz == 3 && x[cy][cx - 1] != 1) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a;
      if (a == '#') {
        x[i][j] = 1;
      }
      if (a == 'g') {
        gx = j;
        gy = i;
      }
      if (a == 's') {
        cx = j;
        cy = i;
      }
    }
  }
  cin >> S;
  line = 0;
  while (line < S.size()) {
    ugoku(S[line]);
    line++;
    if (cx == gx && cy == gy) {
      break;
    }
  }
  cout << sum << endl;
  return 0;
}