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

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct State {
  int cur, h, w, dir;
  bool operator<(const State &st) const { return cur > st.cur; }
};

const int NORTH = 0;
const int SIZE = 1001;
const int INF = (1 << 25);
int H, W, N;
int dh[] = {-1, 0, 1, 0};
int dw[] = {0, 1, 0, -1};
string command;
char field[SIZE][SIZE];
int sh, sw, gh, gw;
int T[SIZE][SIZE][4];
vector<int> DIR[4];

bool isInside(int h, int w) { return 0 <= h && h < H && 0 <= w && w < W; }

void input() {
  cin >> command;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
      if (field[i][j] == 'S') {
        sh = i;
        sw = j;
      }
      if (field[i][j] == 'G') {
        gh = i;
        gw = j;
      }
    }
  for (int i = 0; i < 4; i++)
    DIR[i].clear();
  int d = 0;
  for (int i = 0; i < (int)command.length(); i++) {
    if (command[i] == 'R')
      d = (d + 1) % 4;
    else
      d = (d + 3) % 4;
    DIR[d].push_back(i + 1);
  }
}

bool solve() {
  fill(T[0][0], T[0][0] + SIZE * SIZE * 4, INF);
  priority_queue<State> Q;
  Q.push((State){0, sh, sw, NORTH});
  T[sh][sw][NORTH] = 0;

  while (!Q.empty()) {
    const State now = Q.top();
    Q.pop();

    if (now.h == gh && now.w == gw)
      return true;
    if (T[now.h][now.w][now.dir] < now.cur)
      continue;

    // turn around
    State nex = now;
    for (int i = 0; i < 4; i++) {
      if (now.dir == i)
        continue;

      vector<int>::iterator ite =
          lower_bound(DIR[i].begin(), DIR[i].end(), now.cur);
      if (ite == DIR[i].end())
        continue;

      nex.cur = *ite;
      nex.dir = i;
      if (T[nex.h][nex.w][nex.dir] > nex.cur) {
        T[nex.h][nex.w][nex.dir] = nex.cur;
        Q.push(nex);
      }
    }

    // go forward
    nex = now;
    nex.h += dh[nex.dir];
    nex.w += dw[nex.dir];
    if (isInside(nex.h, nex.w) && field[nex.h][nex.w] != '#') {
      if (T[nex.h][nex.w][nex.dir] > nex.cur) {
        T[nex.h][nex.w][nex.dir] = nex.cur;
        Q.push(nex);
      }
    }
  }
  return false;
}

int main() {
  while (cin >> H >> W >> N && (H | N | W)) {
    input();
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

struct State {
  int cur, h, w, dir;
  bool operator<(const State &st) const { return cur > st.cur; }
};
const int NORTH = 0;
const int EAST = 1;
const int SOUTH = 2;
const int WEST = 3;
const int SIZE = 1001;
const int INF = (1 << 25);
int H, W, N;
int dh[] = {-1, 0, 1, 0};
int dw[] = {0, 1, 0, -1};
string command;
char field[SIZE][SIZE];
int sh, sw, gh, gw;
int T[SIZE][SIZE][4];

bool isInside(int h, int w) { return 0 <= h && h < H && 0 <= w && w < W; }

void input() {
  cin >> command;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
      if (field[i][j] == 'S') {
        sh = i;
        sw = j;
      }
      if (field[i][j] == 'G') {
        gh = i;
        gw = j;
      }
    }
}

void solve() {
  fill(T[0][0], T[0][0] + SIZE * SIZE * 4, INF);
  priority_queue<State> Q;
  Q.push((State){0, sh, sw, NORTH});
  T[sh][sw][NORTH] = 0;

  while (!Q.empty()) {
    const State now = Q.top();
    Q.pop();

    if (T[now.h][now.w][now.dir] > now.cur)
      continue;

    // turn around
    if (now.cur < (int)command.length()) {
      State nex = now;
      if (command[now.cur] == 'R')
        nex.dir = (nex.dir + 1) % 4;
      else
        nex.dir = (nex.dir + 3) % 4;
      nex.cur++;
      if (T[nex.h][nex.w][nex.dir] > nex.cur && field[nex.h][nex.w] != '#') {
        T[nex.h][nex.w][nex.dir] = nex.cur;
        Q.push(nex);
      }
    }

    // go forward
    State nex = now;
    while (isInside(nex.h, nex.w)) {
      if (T[nex.h][nex.w][nex.dir] > nex.cur && field[nex.h][nex.w] != '#') {
        T[nex.h][nex.w][nex.dir] = nex.cur;
        Q.push(nex);
      }
      nex.h += dh[nex.dir];
      nex.w += dw[nex.dir];
    }
  }
  int ans = INF;
  for (int i = 0; i < 4; i++)
    ans = min(ans, T[gh][gw][i]);
  cout << (ans == INF ? "No" : "Yes") << endl;
}

int main() {
  while (cin >> H >> W >> N && (H | N | W)) {
    input();
    solve();
  }
  return 0;
}