Problem p01755 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef string::const_iterator state;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int drc = 0;
vector<string> v;
string s;
pair<int, int> here, start, goal;
int cnt = 0;
bool breakflag = 0;
bool flag = 0;
int ans = 0;
void prog(state &begin);

void sent(state &begin);

void dousa(state &begin);

void ifbun(state &begin);

void whilebun(state &begin);

bool cond(state &begin);

void prog(state &begin) {
  // cout<< begin-s.begin()<< *begin<<" " << here.first << " " << here.second <<
  // " " << drc << endl;
  if (begin == s.end())
    return;
  cnt++;
  if (cnt > 5000000) {
    breakflag = 1;
    return;
  }
  while (1) {
    if (begin == s.end())
      break;
    if (*begin == ']')
      break;
    if (*begin == '}')
      break;
    sent(begin);
  }
  return;
}

void sent(state &begin) {
  if (begin == s.end())
    return;
  if (*begin == '[') {
    ifbun(begin);
  } else if (*begin == '{') {
    whilebun(begin);
  } else {
    dousa(begin);
  }
  return;
}

bool cond(state &begin) {
  // cout<< *begin<<" " << here.first << " " << here.second << endl;
  if (*begin == '~') {
    begin++;
    return !cond(begin);
  }
  if (*begin == 'N') {
    begin++;
    if (drc == 0) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'E') {
    begin++;
    if (drc == 1) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'S') {
    begin++;
    if (drc == 2) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'W') {
    begin++;
    if (drc == 3) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'C') {
    begin++;
    int x = here.first;
    int y = here.second;
    if (v[x + dx[drc]][y + dy[drc]] == '#') {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'T') {
    begin++;
    return 1;
  }
}

void whilebun(state &begin) {
  if (begin == s.end())
    return;
  begin++;
  state while_begin = begin;
  while (1) {
    // cout<<begin-s.begin() << " "  << *begin<<" " << here.first << " " <<
    // here.second << " " << drc << endl;
    if (breakflag) {
      begin = s.end();
      return;
    }
    if (cond(begin) == 1) {
      prog(begin);
    } else {
      int depth = 0;
      while (*begin != '}' || depth != 0) {
        if (*begin == '{') {
          depth++;
        }
        if (*begin == '}') {
          depth--;
        }
        begin++;
      }
      begin++;
      return;
    }
    if (begin == s.end())
      return;

    begin = while_begin;
  }
}

void ifbun(state &begin) {
  // cout<<begin-s.begin() << " "<< *begin<<" " << here.first << " " <<
  // here.second << " " << drc << endl;

  if (begin == s.end())
    return;
  begin++;
  if (cond(begin)) {
    prog(begin);
    if (begin == s.end())
      return;
    begin++;
  } else {
    int depth = 0;
    while (*begin != ']' || depth != 0) {
      if (*begin == '[') {
        depth++;
      }
      if (*begin == ']') {
        depth--;
      }
      begin++;
    }
    begin++;
  }
  return;
}

void dousa(state &begin) {
  if (begin == s.end())
    return;
  ans++;
  cnt++;
  if (cnt > 5000000) {
    begin = s.end();
    breakflag = 1;
    return;
  }
  int x = here.first;
  int y = here.second;
  if (*begin == '^') {
    if (v[x + dx[drc]][y + dy[drc]] == 'g') {
      begin = s.end();
      flag = 1;
      begin--;
    } else if (v[x + dx[drc]][y + dy[drc]] != '#') {
      here = make_pair(x + dx[drc], y + dy[drc]);
    }
    begin++;
  } else if (*begin == 'v') {
    if (v[x - dx[drc]][y - dy[drc]] == 'g') {
      begin = s.end();
      flag = 1;
      begin--;
    } else if (v[x - dx[drc]][y - dy[drc]] != '#') {
      here = make_pair(x - dx[drc], y - dy[drc]);
    }
    begin++;
  } else if (*begin == '<') {
    drc = (4 + drc - 1) % 4;
    begin++;
  } else {
    drc = (drc + 1) % 4;
    begin++;
  }
  // cout << ans << " " << here.first << " " << here.second << endl;
  return;
}

int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    string tmp;
    cin >> tmp;
    v.push_back(tmp);
  }

  cin >> s;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] == 's') {
        start = make_pair(i, j);
      }
      if (v[i][j] == 'g') {
        goal = make_pair(i, j);
      }
    }
  }
  here = start;
  state begin = s.begin();
  prog(begin);
  if (flag) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cctype>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef string::const_iterator state;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int drc = 0;
vector<string> v;
string s;
pair<int, int> here, start, goal;
int cnt = 0;
bool breakflag = 0;
bool flag = 0;
int ans = 0;
void prog(state &begin);

void sent(state &begin);

void dousa(state &begin);

void ifbun(state &begin);

void whilebun(state &begin);

bool cond(state &begin);

void prog(state &begin) {
  // cout<< *begin<<" " << here.first << " " << here.second << endl;
  cnt++;
  if (cnt > 5000000) {
    breakflag = 1;
    return;
  }
  while (1) {
    if (begin == s.end())
      break;
    if (*begin == ']')
      break;
    if (*begin == '}')
      break;
    sent(begin);
  }
  return;
}

void sent(state &begin) {
  if (begin == s.end())
    return;
  if (*begin == '[') {
    ifbun(begin);
  } else if (*begin == '{') {
    whilebun(begin);
  } else {
    dousa(begin);
  }
  return;
}

bool cond(state &begin) {
  // cout<< *begin<<" " << here.first << " " << here.second << endl;
  if (*begin == '~') {
    begin++;
    return !cond(begin);
  }
  if (*begin == 'N') {
    begin++;
    if (drc == 0) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'E') {
    begin++;
    if (drc == 1) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'S') {
    begin++;
    if (drc == 2) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'W') {
    begin++;
    if (drc == 3) {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'C') {
    begin++;
    int x = here.first;
    int y = here.second;
    if (v[x + dx[drc]][y + dy[drc]] == '#') {
      return 1;
    } else {
      return 0;
    }
  }
  if (*begin == 'T') {
    begin++;
    return 1;
  }
}

void whilebun(state &begin) {
  if (begin == s.end())
    return;
  begin++;
  state while_begin = begin;
  while (1) {
    // cout<< *begin<<" " << here.first << " " << here.second << " " << drc <<
    // endl;
    if (breakflag) {
      begin = s.end();
      return;
    }
    if (cond(begin) == 1) {
      prog(begin);
    } else {
      int depth = 0;
      while (*begin != '}' || depth != 0) {
        if (*begin == '{') {
          depth++;
        }
        if (*begin == '}') {
          depth--;
        }
        begin++;
      }
      begin++;
      return;
    }
    if (begin == s.end())
      return;

    begin = while_begin;
  }
}

void ifbun(state &begin) {
  if (begin == s.end())
    return;
  begin++;
  if (cond(begin)) {
    prog(begin);
    begin++;
  } else {
    int depth = 0;
    while (*begin != ']' || depth != 0) {
      if (*begin == '[') {
        depth++;
      }
      if (*begin == ']') {
        depth--;
      }
      begin++;
    }
  }
  return;
}

void dousa(state &begin) {
  ans++;
  cnt++;
  if (cnt > 5000000) {
    begin = s.end();
    breakflag = 1;
    return;
  }
  int x = here.first;
  int y = here.second;
  if (*begin == '^') {
    if (v[x + dx[drc]][y + dy[drc]] == 'g') {
      begin = s.end();
      flag = 1;
      begin--;
    } else if (v[x + dx[drc]][y + dy[drc]] != '#') {
      here = make_pair(x + dx[drc], y + dy[drc]);
    }
    begin++;
  } else if (*begin == 'v') {
    if (v[x - dx[drc]][y - dy[drc]] == 'g') {
      begin = s.end();
      flag = 1;
      begin--;
    } else if (v[x - dx[drc]][y - dy[drc]] != '#') {
      here = make_pair(x - dx[drc], y - dy[drc]);
    }
    begin++;
  } else if (*begin == '<') {
    drc = (4 + drc - 1) % 4;
    begin++;
  } else {
    drc = (drc + 1) % 4;
    begin++;
  }
  // cout << ans << " " << here.first << " " << here.second << endl;
  return;
}

int main() {
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    string tmp;
    cin >> tmp;
    v.push_back(tmp);
  }

  cin >> s;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] == 's') {
        start = make_pair(i, j);
      }
      if (v[i][j] == 'g') {
        goal = make_pair(i, j);
      }
    }
  }
  here = start;

  while (1) {
    state begin = s.begin();
    prog(begin);
    if (breakflag) {
      cout << -1 << endl;
      return 0;
    }
    if (flag)
      break;
  }
  cout << ans << endl;
  return 0;
}