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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int h, w, n;
char ss[1000001];
char field[1001][1001];
int sx, sy;
int gx, gy;

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

int d[4][1001][1001];
int toAng[4][4][1000001];
const int INF = 1000000000;

typedef pair<int, int> pii;

int dfs(int cang, int oang, int pos) {
  if (cang == oang)
    return pos;
  else if (pos == n)
    return INF;
  else if (toAng[cang][oang][pos] != -1)
    return toAng[cang][oang][pos];
  int nang;
  if (ss[pos] == 'L')
    nang = (cang - 1 + 4) % 4;
  else
    nang = (cang + 1) % 4;
  return toAng[cang][oang][pos] = dfs(nang, oang, pos + 1);
}

bool bfs() {
  memset(toAng, -1, sizeof(toAng));

  // »ÝÌü«©çÚIÌü«ÖÚ®·éÌÉÇ±ÜÅ^[ðg¤Kvª é©vZ
  // ìêÈ¢ÈçÎINF
  // cang==oagn
  //	for(int i=0;i<4;i++)for(int j=0;j<n;j++)toAng[i][i][j]=i;
  //	// cang!=oang&&pos==n
  //	for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(i!=j)toAng[i][j][n]=INF;
  //	for(int k=n-1;k>=0;k--){
  //		for(int i=0;i<4;i++){
  //			for(int j=0;j<4;j++){
  //				if(j==i)continue;
  //				int nang;
  //				int cang=i;
  //				int oang=j;
  //				int pos=k;
  //				if(ss[pos]=='L')nang=(cang-1+4)%4;
  //				else nang=(cang+1)%4;
  //				toAng[cang][oang][pos]=toAng[nang][oang][pos+1];
  //			}
  //		}
  //	}
  //  éêÖB·éÌÉKvÈÅ¬^[
  priority_queue<pair<pii, pii>, vector<pair<pii, pii>>,
                 greater<pair<pii, pii>>>
      pq;
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 1001; j++)
      for (int k = 0; k < 1001; k++)
        d[i][j][k] = INF;
  pq.push(make_pair(pii(0, 0), pii(sy, sx)));
  d[0][sy][sx] = 0;
  while (pq.size()) {
    pair<pii, pii> p = pq.top();
    pq.pop();
    int cx = p.second.second;
    int cy = p.second.first;
    int cang = p.first.second;
    int cpos = p.first.first;
    // àµ»ÝÌRXgæè¬³¢àÌª·ÅÉ¶ÝµÄ¢½ÌÈçÎ
    if (d[cang][cy][cx] < cpos)
      continue;
    // 4ûüÖÚ®
    for (int i = 0; i < 4; i++) {
      int ny = cy + dy[i];
      int nx = cx + dx[i];
      int nang = i;
      // ÌûüðìêéÜÅposðißé
      // int npos=toAng[cang][nang][cpos];
      int npos = dfs(cang, nang, cpos);
      if (npos != INF && ny >= 0 && nx >= 0 && ny < h && nx < w &&
          field[ny][nx] == '.' && d[nang][ny][nx] > npos) {
        d[nang][ny][nx] = npos;
        pq.push(make_pair(pii(npos, nang), pii(ny, nx)));
      }
    }
  }
  for (int i = 0; i < 4; i++)
    if (d[i][gy][gx] != INF)
      return true;
  return false;
}

int main() {
  while (cin >> h >> w >> n && (h | w | n)) {
    for (int i = 0; i < n; i++)
      cin >> ss[i];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
        if (field[i][j] == 'S') {
          sx = j;
          sy = i;
          field[i][j] = '.';
        } else if (field[i][j] == 'G') {
          gx = j;
          gy = i;
          field[i][j] = '.';
        }
      }
    if (bfs())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int h, w, n;
char ss[1000001];
char field[1001][1001];
int sx, sy;
int gx, gy;

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

int d[4][1001][1001];
int toAng[4][4][1000001];
const int INF = 1000000000;

typedef pair<int, int> pii;

// int dfs(int cang,int oang,int pos){
//	if(cang==oang)return pos;
//	else if(pos==n)return INF;
//	else if(toAng[cang][oang][pos]!=-1)return toAng[cang][oang][pos];
//	int nang;
//	if(ss[pos]=='L')nang=(cang-1+4)%4;
//	else nang=(cang+1)%4;
//	return toAng[cang][oang][pos]=dfs(nang,oang,pos+1);
// }

bool bfs() {
  // »ÝÌü«©çÚIÌü«ÖÚ®·éÌÉÇ±ÜÅ^[ðg¤Kvª é©vZ
  // ìêÈ¢ÈçÎINF
  // cang==oagn
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < n; j++)
      toAng[i][i][j] = i;
  // cang!=oang&&pos==n
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      if (i != j)
        toAng[i][j][n] = INF;
  for (int k = n - 1; k >= 0; k--) {
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        if (j == i)
          continue;
        int nang;
        int cang = i;
        int oang = j;
        int pos = k;
        if (ss[pos] == 'L')
          nang = (cang - 1 + 4) % 4;
        else
          nang = (cang + 1) % 4;
        toAng[cang][oang][pos] = toAng[nang][oang][pos + 1];
      }
    }
  }
  //  éêÖB·éÌÉKvÈÅ¬^[
  priority_queue<pair<pii, pii>> pq;
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 1001; j++)
      for (int k = 0; k < 1001; k++)
        d[i][j][k] = INF;
  pq.push(make_pair(pii(0, 0), pii(sy, sx)));
  d[sy][sx][0] = 0;
  while (pq.size()) {
    pair<pii, pii> p = pq.top();
    pq.pop();
    int cx = p.second.second;
    int cy = p.second.first;
    int cang = p.first.second;
    int cpos = p.first.first;
    // àµ»ÝÌRXgæè¬³¢àÌª·ÅÉ¶ÝµÄ¢½ÌÈçÎ
    if (d[cy][cx][cang] < cpos)
      continue;
    // 4ûüÖÚ®
    for (int i = 0; i < 4; i++) {
      int ny = cy + dy[i];
      int nx = cx + dx[i];
      int nang = i;
      // ÌûüðìêéÜÅposðißé
      int npos = toAng[cang][nang][cpos];
      if (npos != INF && ny >= 0 && nx >= 0 && ny < h && nx < w &&
          field[ny][nx] == '.' && d[ny][nx][nang] > npos) {
        d[ny][nx][nang] = npos;
        pq.push(make_pair(pii(npos, nang), pii(ny, nx)));
      }
    }
  }
  for (int i = 0; i < 4; i++)
    if (d[gy][gx][i] != INF)
      return true;
  return false;
}

int main() {
  while (cin >> h >> w >> n && (h | w | n)) {
    for (int i = 0; i < n; i++)
      cin >> ss[i];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
        if (field[i][j] == 'S') {
          sx = j;
          sy = i;
          field[i][j] = '.';
        } else if (field[i][j] == 'G') {
          gx = j;
          gy = i;
          field[i][j] = '.';
        }
      }
    if (bfs())
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}