Problem p01752 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}

int H, W;
vector<string> F;
void input() {
  cin >> H >> W;
  H += 2;
  W += 2;
  F.resize(H);
  F[0] = string(W, '#');
  for (int i = 1; i < H - 1; i++) {
    string s;
    cin >> s;
    F[i] = string("#") + s + "#";
  }
  F[H - 1] = string(W, '#');
}

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

int sy, sx, sdir;
void init() {
  string s = "^>v<";
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (find(s.begin(), s.end(), F[i][j]) != s.end()) {
        sy = i;
        sx = j;
        sdir = find(s.begin(), s.end(), F[i][j]) - s.begin();
        return;
      }
    }
  }
}

struct State {
  int y, x;
  int dir;
  State(int y, int x, int dir) : y(y), x(x), dir(dir) {}
};

const int INF = 1 << 28;

void solve() {
  init();
  bool V[H][W];
  bool D[H][W][4];
  memset(V, false, sizeof(V));
  memset(D, false, sizeof(D));
  queue<State> Q;
  Q.push(State(sy, sx, sdir));
  V[sy][sx] = true;
  D[sy][sx][sdir] = true;
  while (not Q.empty()) {
    State c = Q.front();
    Q.pop();
    if (F[c.y][c.x] == 'G') {
      break;
    }
    for (int i = 0; i < 4; i++) {
      int ndir = (c.dir + 5 - i) % 4;
      int ny = c.y + dy[ndir];
      int nx = c.x + dx[ndir];
      if (F[ny][nx] == '#')
        continue;
      if (D[ny][nx][ndir]) {
        cout << -1 << endl;
        return;
      }
      V[ny][nx] = true;
      Q.push(State(ny, nx, ndir));
      break;
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (V[i][j])
        ans++;
  // for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << V[i][j]
  // << " "; } cout << endl; }
  cout << ans << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

namespace {

typedef double real;
typedef long long ll;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}

int H, W;
vector<string> F;
void input() {
  cin >> H >> W;
  H += 2;
  W += 2;
  F.resize(H);
  F[0] = string(W, '#');
  for (int i = 1; i < H - 1; i++) {
    string s;
    cin >> s;
    F[i] = string("#") + s + "#";
  }
  F[H - 1] = string(W, '#');
}

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

int sy, sx, sdir;
void init() {
  string s = "^>v<";
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (find(s.begin(), s.end(), F[i][j]) != s.end()) {
        sy = i;
        sx = j;
        sdir = find(s.begin(), s.end(), F[i][j]) - s.begin();
        return;
      }
    }
  }
}

struct State {
  int y, x;
  int dir;
  State(int y, int x, int dir) : y(y), x(x), dir(dir) {}
};

const int INF = 1 << 28;

void solve() {
  init();
  bool V[H][W];
  bool D[H][W][4];
  memset(V, false, sizeof(V));
  memset(D, false, sizeof(D));
  queue<State> Q;
  Q.push(State(sy, sx, sdir));
  V[sy][sx] = true;
  D[sy][sx][sdir] = true;
  while (not Q.empty()) {
    State c = Q.front();
    Q.pop();
    if (F[c.y][c.x] == 'G') {
      break;
    }
    for (int i = 0; i < 4; i++) {
      int ndir = (c.dir + 5 - i) % 4;
      int ny = c.y + dy[ndir];
      int nx = c.x + dx[ndir];
      if (F[ny][nx] == '#')
        continue;
      if (D[ny][nx][ndir]) {
        cout << -1 << endl;
        return;
      }
      V[ny][nx] = true;
      D[ny][nx][ndir] = true;
      Q.push(State(ny, nx, ndir));
      break;
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (V[i][j])
        ans++;
  // for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << V[i][j]
  // << " "; } cout << endl; }
  cout << ans << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}