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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int h, w;
int ch, cw;
int dh, dw;
string s[1000];

int cost[1000][1000][3];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  rep(i, h) cin >> s[i];
  ch--;
  cw--;
  dh--;
  dw--;

  rep(i, h) rep(j, w) rep(k, 3) cost[i][j][k] = INF;
  cost[ch][cw][0] = 0;

  deque<tuple<int, int, int>> q;
  q.emplace_back(ch, cw, 0);

  auto update = [&](int y, int x, int c, int v, bool f1, bool f2) {
    if (0 <= y && y < h && 0 <= x && x < w) {
      if (s[y][x] == '.' || f1) {
        if (v < cost[y][x][c]) {
          cost[y][x][c] = v;
          if (f2)
            q.emplace_back(y, x, c);
          else
            q.emplace_front(y, x, c);
        }
      }
    }
  };

  while (!q.empty()) {
    int y, x, c;
    tie(y, x, c) = q.front();
    q.pop_front();
    int co = cost[y][x][c];

    if (s[y][x] == '.') {
      update(y - 1, x, 0, co, false, false);
      update(y + 1, x, 0, co, false, false);
      update(y, x - 1, 0, co, false, false);
      update(y, x + 1, 0, co, false, false);
    }

    if (0 < c) {
      update(y, x, 0, co, false, false);
    }
    if (c < 2) {
      if (c == 0)
        co++;
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          update(y + i, x + j, c + 1, co, true, c == 0);
        }
      }
    }
  }

  int ans = INF;
  rep(i, 3) ans = min(ans, cost[dh][dw][i]);

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((1 << 30) - 1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int h, w;
int ch, cw;
int dh, dw;
string s[1000];

int cost[1000][1000][3];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  rep(i, h) cin >> s[i];
  ch--;
  cw--;
  dh--;
  dw--;

  rep(i, h) rep(j, w) rep(k, 6) cost[i][j][k] = INF;
  cost[ch][cw][0] = 0;

  deque<tuple<int, int, int>> q;
  q.emplace_back(ch, cw, 0);

  auto update = [&](int y, int x, int c, int v, bool f1, bool f2) {
    if (0 <= y && y < h && 0 <= x && x < w) {
      if (s[y][x] == '.' || f1) {
        if (v < cost[y][x][c]) {
          cost[y][x][c] = v;
          if (f2)
            q.emplace_back(y, x, c);
          else
            q.emplace_front(y, x, c);
        }
      }
    }
  };

  while (!q.empty()) {
    int y, x, c;
    tie(y, x, c) = q.front();
    q.pop_front();
    int co = cost[y][x][c];

    if (s[y][x] == '.') {
      update(y - 1, x, 0, co, false, false);
      update(y + 1, x, 0, co, false, false);
      update(y, x - 1, 0, co, false, false);
      update(y, x + 1, 0, co, false, false);
    }

    if (c < 2) {
      if (c == 0)
        co++;
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          update(y + i, x + j, c + 1, co, true, c == 0);
        }
      }
    }
  }

  int ans = INF;
  rep(i, 3) ans = min(ans, cost[dh][dw][i]);

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;

  return 0;
}