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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int mod = 1000000007;
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); };
// fixed << setprecision(2)
bool Array_check(ll y, ll x, ll d, ll w) {
  if (x < 0 || x >= w || y < 0 || y >= d) {
    return false;
  } else {
    return true;
  }
}

// 小文字=大文字+32
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
  queue<int> q_x;
  queue<int> q_y;
  queue<int> wap_x;
  queue<int> wap_y;
  int d, w;
  int sx, sy;
  int gx, gy;
  char c[1000][1000];
  cin >> d >> w;
  cin >> sy >> sx;
  cin >> gy >> gx;
  sy--;
  sx--;
  gx--;
  gy--;
  ll x;
  ll y;
  ll tmpx;
  ll tmpy;
  ll wap = 0;
  ll precontain = 0;
  ll wapcount = 0;
  rep(i, d) {
    rep(j, w) { cin >> c[i][j]; }
  }
  q_x.push(sx);
  q_y.push(sy);
  wap_x.push(sx);
  wap_y.push(sy);
  precontain = q_x.size();
  c[sy][sx] = 'X';
  wapcount = 1;
  while (1) {
    for (ll ct = 0; ct < precontain; ct++) {
      x = q_x.front();
      q_x.pop();
      y = q_y.front();
      q_y.pop();

      rep(i, 4) {
        tmpx = x + dx[i];
        tmpy = y + dy[i];
        if (Array_check(tmpy, tmpx, d, w) && c[tmpy][tmpx] == '.') {
          c[tmpy][tmpx] = 'X';
          if (tmpx == gx && tmpy == gy) {
            cout << wap << endl;

            return 0;
          }
          wap_x.push(tmpx);
          wap_y.push(tmpy);
          q_x.push(tmpx);
          q_y.push(tmpy);
          wapcount++;
        }
      }
    }

    int save = wapcount;
    if (q_x.empty()) {
      wapcount = 0;
      wap++;
      for (ll k = 0; k < save; k++) {
        x = wap_x.front();
        wap_x.pop();
        y = wap_y.front();
        wap_y.pop();
        for (int i = -2; i < 3; i++) {
          for (int j = -2; j < 3; j++) {
            tmpy = y + i;
            tmpx = x + j;
            if (Array_check(tmpy, tmpx, d, w) && c[tmpy][tmpx] == '.') {
              c[tmpy][tmpx] = 'X';
              if (tmpx == gx && tmpy == gy) {
                cout << wap << endl;

                return 0;
              }
              q_x.push(tmpx);
              q_y.push(tmpy);
              wap_x.push(tmpx);
              wap_y.push(tmpy);
              wapcount++;
            }
          }
        }
      }
    }
    if (q_x.empty()) {
      cout << -1;

      return 0;
    }
    precontain = q_x.size();
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const int mod = 1000000007;
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); };
// fixed << setprecision(2)
bool Array_check(ll y, ll x, ll d, ll w) {
  if (x < 0 || x >= w || y < 0 || y >= d) {
    return false;
  } else {
    return true;
  }
}

// 小文字=大文字+32
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
  queue<int> q_x;
  queue<int> q_y;
  queue<int> wap_x;
  queue<int> wap_y;
  int d, w;
  int sx, sy;
  int gx, gy;
  char c[50][50];
  bool a[50][50] = {};
  cin >> d >> w;
  cin >> sy >> sx;
  cin >> gy >> gx;
  sy--;
  sx--;
  gx--;
  gy--;
  int x = sx;
  int y = sy;
  int tmpx;
  int tmpy;
  int wap = 0;
  int precontain = 0;
  rep(i, d) {
    rep(j, w) { cin >> c[i][j]; }
  }
  q_x.push(sx);
  q_y.push(sy);
  precontain = q_x.size();
  while (1) {
    for (int ct = 0; ct < precontain; ct++) {
      x = q_x.front();
      q_x.pop();
      y = q_y.front();
      q_y.pop();

      rep(i, 4) {
        tmpx = x + dx[i];
        tmpy = y + dy[i];
        if (Array_check(tmpy, tmpx, d, w) && a[tmpy][tmpx] == false &&
            c[tmpy][tmpx] == '.') {
          a[tmpy][tmpx] = true;
          if (tmpx == gx && tmpy == gy) {
            cout << wap << endl;
            return 0;
          }
          wap_x.push(tmpx);
          wap_y.push(tmpy);
          q_x.push(tmpx);
          q_y.push(tmpy);
        }
      }
    }

    precontain = q_x.size();
    if (q_x.empty()) {
      wap++;
      for (int k = 0; k < wap_x.size(); k++) {
        x = wap_x.front();
        wap_x.pop();
        y = wap_y.front();
        wap_y.pop();
        for (int i = -2; i < 3; i++) {
          for (int j = -2; j < 3; j++) {
            tmpx = x + j;
            tmpy = y + i;
            if (Array_check(tmpy, tmpx, d, w) && a[tmpy][tmpx] == false &&
                c[tmpy][tmpx] == '.') {
              a[tmpy][tmpx] = true;
              if (tmpx == gx && tmpy == gy) {
                cout << wap << endl;
                return 0;
              }
              q_x.push(tmpx);
              q_y.push(tmpy);
            }
          }
        }
      }
    }
    if (q_x.empty()) {

      cout << -1;
      return 0;
    }
  }
}