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

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<int, int> P;
typedef pair<int, P> P2;
typedef pair<int, P2> P3;
const int MAX = 1000000007;

int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, -1, 0, 1};
bool check(int n, int m, int x, int y) {
  if (x < 0 || x >= n)
    return false;
  if (y < 0 || y >= m)
    return false;
  return true;
}
int d[1001][1001][4];
priority_queue<P3, vector<P3>, greater<P3>> que;

int main() {
  int n, m, k;
  while (cin >> n >> m >> k && (n || m || k)) {
    while (!que.empty())
      que.pop();

    string c;
    string s[n];
    cin >> c;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int l = 0; l < 4; l++)
          d[i][j][l] = MAX;
      }
    }

    int sx, sy, tx, ty;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == 'S') {
          sx = i;
          sy = j;
        }
        if (s[i][j] == 'G') {
          tx = i;
          ty = j;
        }
      }
    }

    que.push(P3(0, P2(1, P(sx, sy))));
    d[sx][sy][1] = 0;
    bool ck = false;
    while (!que.empty()) {
      P3 p = que.top();
      que.pop();
      int j = p.F, z = p.S.F, nx = p.S.S.F, ny = p.S.S.S;
      if (nx == tx && ny == ty) {
        ck = true;
        break;
      }

      int x = nx + dx[z], y = ny + dy[z];
      if (check(n, m, x, y) && s[x][y] != '#' && d[x][y][z] > j) {
        d[x][y][z] = j;
        que.push(P3(j, P2(z, P(x, y))));
      }

      x = nx;
      y = ny;
      int z2 = z;
      bool b[4];
      memset(b, false, sizeof(b));
      b[z] = true;
      for (int i = j; i < k; i++) {
        if (c[i] == 'L')
          z2--;
        else
          z2++;
        if (z2 < 0)
          z2 += 4;
        else if (z2 >= 4)
          z2 -= 4;
        if (b[z2])
          continue;
        b[z2] = true;
        if (d[x][y][z2] > i + 1) {
          d[x][y][z2] = i + 1;
          que.push(P3(i + 1, P2(z2, P(x, y))));
        }
      }
    }
    if (ck)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<int, int> P;
typedef pair<int, P> P2;
typedef pair<int, P2> P3;
const int MAX = 1000000007;

int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, -1, 0, 1};
bool check(int n, int m, int x, int y) {
  if (x < 0 || x >= n)
    return false;
  if (y < 0 || y >= m)
    return false;
  return true;
}

int main() {
  int n, m, k;
  while (cin >> n >> m >> k && (n || m || k)) {
    string c;
    string s[n];
    cin >> c;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    int d[n][m][4];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int l = 0; l < 4; l++)
          d[i][j][l] = MAX;
      }
    }

    int sx, sy, tx, ty;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i][j] == 'S') {
          sx = i;
          sy = j;
        }
        if (s[i][j] == 'G') {
          tx = i;
          ty = j;
        }
      }
    }

    priority_queue<P3, vector<P3>, greater<P3>> que;
    que.push(P3(0, P2(1, P(sx, sy))));
    d[sx][sy][1] = 0;
    bool ck = false;
    while (!que.empty()) {
      P3 p = que.top();
      que.pop();
      int j = p.F, z = p.S.F, nx = p.S.S.F, ny = p.S.S.S;
      if (nx == tx && ny == ty) {
        ck = true;
        break;
      }
      int x = nx + dx[z], y = ny + dy[z];
      if (check(n, m, x, y) && s[x][y] != '#' && d[x][y][z] > j) {
        d[x][y][z] = j;
        que.push(P3(j, P2(z, P(x, y))));
      }

      if (j < k) {
        x = nx;
        y = ny;
        int z2 = z;
        if (c[j] == 'L')
          z2--;
        else
          z2++;
        if (z2 < 0)
          z2 += 4;
        else if (z2 >= 4)
          z2 -= 4;

        if (d[x][y][z2] > j + 1) {
          d[x][y][z2] = j + 1;
          que.push(P3(j + 1, P2(z2, P(x, y))));
        }
      }
    }
    if (ck)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}