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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ll long long
static const ll MAX = 10000;
static const ll INFTY = 1e12;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

// 入力
// 入力
int H, W;
int sx, sy, gx, gy;
vector<vector<char>> field;
vector<vector<int>> dist;
queue<pair<int, int>> q, visited;
using P = pair<ll, ll>;

int main() {
  ll h, w;
  cin >> h >> w;
  ll startX, startY, endX, endY;
  cin >> startX >> startY >> endX >> endY;
  startX--, startY--, endX--, endY--;

  vector<vector<char>> s(h, vector<char>(w));
  REP(i, h) REP(j, w) cin >> s[i][j];

  vector<vector<ll>> cnts(h, vector<ll>(w, INFTY)); // 魔法使った回数
  queue<P> q, visited;
  q.push(P(startX, startY));
  cnts[startX][startY] = 0;

  while (!q.empty() || !visited.empty()) {
    ll nowX, nowY;
    if (!q.empty()) {
      nowX = q.front().first, nowY = q.front().second;
      visited.push(P(nowX, nowY));
      q.pop();
      REP(i, 4) {
        ll nextX = nowX + dx[i], nextY = nowY + dy[i];
        if (0 <= nextX && nextX <= h - 1 && 0 <= nextY && nextY <= w - 1) {
          if (s[nextX][nextY] == '#')
            continue;
          if (cnts[nowX][nowY] < cnts[nextX][nextY]) {
            cnts[nextX][nextY] = cnts[nowX][nowY];
            q.push(P(nextX, nextY));
          }
        }
      }

    } else { // 今まで通った道から魔法で行ける道を全て調べる
      nowX = visited.front().first, nowY = visited.front().second;
      visited.pop();
      for (ll ix = -2; ix <= 2; ++ix) {
        for (ll jy = -2; jy <= 2; ++jy) {
          ll nextX = nowX + ix, nextY = nowY + jy;
          if (0 <= nextX && nextX <= h - 1 && 0 <= nextY && nextY <= w - 1) {
            if (s[nextX][nextY] == '#')
              continue;
            if (cnts[nowX][nowY] + 1 < cnts[nextX][nextY]) {
              cnts[nextX][nextY] = cnts[nowX][nowY] + 1;
              q.push(P(nextX, nextY));
            }
          }
        }
      }
    }
  }
  if (cnts[endX][endY] == INFTY) {
    cout << -1 << '\\n';
  } else {
    cout << cnts[endX][endY] << '\\n';
  }
  return 0;
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\\  =  /O
               ____/`---'\\____
             .'  \\\\|     |//  `.
            /  \\\\|||  :  |||//  \\
           /  _||||| -:- |||||-  \\
           |   | \\\\\\  -  /// |   |
           | \\_|  ''\\---/''  |   |
           \\  .-\\__  `-`  ___/-. /
         ___`. .'  /--.--\\  `. . __
      ."" '<  `.___\\_<|>_/___.'  >'"".
     | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /
======`-.____`-.___\\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ll long long
static const int MAX = 10000;
static const ll INFTY = 1e12;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

// 入力
// 入力
int H, W;
int sx, sy, gx, gy;
vector<vector<char>> field;
vector<vector<int>> dist;
queue<pair<int, int>> que, visited;

// 探索
void bfs() {

  pair<int, int> now;
  // 初期条件 (頂点 0 を初期ノードとする)
  dist[sy][sx] = 0;
  now.first = sy;
  now.second = sx;
  que.push(now);
  // visited.push(now);

  // 八方向を探索
  while (!que.empty() || !visited.empty()) {

    if (!que.empty()) {
      pair<int, int> v = que.front();
      visited.push(v);
      que.pop();

      for (int i = 0; i <= 4; ++i) {
        int nh = v.first + dy[i], nw = v.second + dx[i];

        // 場外アウトしたり、0 だったりはスルー
        if (nh < 0 || nh >= H || nw < 0 || nw >= W)
          continue;
        if (field[nh][nw] == '#')
          continue;
        if (dist[nh][nw] != -1)
          continue;

        dist[nh][nw] = dist[v.first][v.second];
        pair<int, int> nv;
        nv.first = nh;
        nv.second = nw;
        que.push(nv);
      }
    }
  }
}

int main() {

  cin >> H >> W;
  cin >> sy >> sx;
  sx--;
  sy--;
  cin >> gy >> gx;
  gx--;
  gy--;
  // vector<pair<int, int>> gx(N), gy(N);
  field.assign(H, vector<char>(W));
  dist.assign(H, vector<int>(W, -1));
  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      cin >> field[h][w];
      if (field[h][w] == 'S') {
        sy = h;
        sx = w;
      }
      // REP(i, N){
      //     if( field[h][w] == '1' + i ) { gy[i].first = h; gx[i].first = w; }
      // }
    }
  }

  bfs();
  cout << dist[gy][gx] << endl;

  // if(dist[gx][gy] )

  // REP(i, H){
  //   REP(j, W){
  //     cout << dist[i][j] << " ";
  //   }cout << endl;
  // }
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\\  =  /O
               ____/`---'\\____
             .'  \\\\|     |//  `.
            /  \\\\|||  :  |||//  \\
           /  _||||| -:- |||||-  \\
           |   | \\\\\\  -  /// |   |
           | \\_|  ''\\---/''  |   |
           \\  .-\\__  `-`  ___/-. /
         ___`. .'  /--.--\\  `. . __
      ."" '<  `.___\\_<|>_/___.'  >'"".
     | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |
     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /
======`-.____`-.___\\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/