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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007; // 998244353;

template <class _T> static void get(_T &a) { std::cin >> a; }
template <class _T> static void get(_T &a, _T &b) { std::cin >> a >> b; }
template <class _T> static void get(_T &a, _T &b, _T &c) {
  std::cin >> a >> b >> c;
}
template <class _T> static _T tp_abs(_T a) {
  if (a < (_T)0) {
    a *= (_T)-1;
  }
  return a;
}

static void A();

int main() {
  A();
  fflush(stdout);
  return 0;
}
void dbg(std::vector<std::vector<int>> &flag) {
  for (auto it = flag.begin(); it != flag.end(); it++) {
    for (auto it2 = it->begin(); it2 != it->end(); it2++) {
      if (*it2 == (int)MOD_NUM)
        printf("F");
      else if (*it2 == -1)
        printf("W");
      else
        printf("%d", *it2);
    }
    putchar('\\n');
  }
}
static void A() {
  int H, W;
  get(H, W);

  int sh, sw;
  get(sh, sw);
  sh--;
  sw--;

  int gh, gw;
  get(gh, gw);
  gh--;
  gw--;

  std::vector<std::string> grid(H);
  for (int h = 0; h < H; h++) {
    get(grid[h]);
  }

  std::vector<std::vector<int>> flag(H, std::vector<int>(W, (int)MOD_NUM));
  std::deque<std::pair<int, int>> deq;
  int warp = 0;
  flag[sh][sw] = warp;
  deq.emplace_front(sh, sw);

  int dh[] = {1, 0, -1, 0, 1, 1, -1, -1};
  int dw[] = {0, 1, 0, -1, 1, -1, -1, 1};
  while (!deq.empty()) {
    std::pair<int, int> pos = deq.front();
    deq.pop_front();
    warp = flag[pos.first][pos.second];

    if (pos.first == gh && pos.second == gw) {
      printf("%d\\n", warp);
      // dbg(flag);
      return;
    }

    for (int i = 0; i < 8; i++) {
      int nh = pos.first + dh[i];
      int nw = pos.second + dw[i];

      if ((nh >= 0) && (nh < H) && (nw >= 0) && (nw < W)) {
        if (i < 4) {
          if (grid[nh][nw] == '.' && flag[nh][nw] > warp) {
            deq.emplace_front(nh, nw);
            flag[nh][nw] = warp;
          }
        }
        if (flag[nh][nw] > warp) {
          if (grid[nh][nw] == '#') {
            flag[nh][nw] = -1;
          } else {
            flag[nh][nw] = warp + 1;
            deq.emplace_back(nh, nw);
          }
          for (int j = 0; j < 8; j++) {
            int nnh = nh + dh[j];
            int nnw = nw + dw[j];

            if ((nnh >= 0) && (nnh < H) && (nnw >= 0) && (nnw < W)) {
              if (grid[nnh][nnw] == '.' && flag[nnh][nnw] > (warp + 1)) {
                deq.emplace_back(nnh, nnw);
                flag[nnh][nnw] = warp + 1;
              }
            }
          }
        }
      }
    }
  }
  //	dbg(flag);
  printf("-1\\n");
}


Error: 0

Fixed submission:


#include <algorithm>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007; // 998244353;

template <class _T> static void get(_T &a) { std::cin >> a; }
template <class _T> static void get(_T &a, _T &b) { std::cin >> a >> b; }
template <class _T> static void get(_T &a, _T &b, _T &c) {
  std::cin >> a >> b >> c;
}
template <class _T> static _T tp_abs(_T a) {
  if (a < (_T)0) {
    a *= (_T)-1;
  }
  return a;
}

static void D();

int main() {
  D();
  fflush(stdout);
  return 0;
}

static void D() {
  int H, W;
  get(H, W);

  int Ch, Cw;
  get(Ch, Cw);
  Ch--;
  Cw--;

  int Dh, Dw;
  get(Dh, Dw);
  Dh--;
  Dw--;

  std::vector<std::string> grid(H);
  for (int i = 0; i < H; i++) {
    get(grid[i]);
  }

  std::queue<std::pair<int, int>> que, nextque;
  que.push(std::pair<int, int>(Dh, Dw));

  std::vector<std::vector<int>> flag(H, std::vector<int>(W, -1));
  int dh[] = {1, 0, -1, 0};
  int dw[] = {0, 1, 0, -1};

  int ddh[] = {1, 1, 1, 0, -1, -1, -1, 0};
  int ddw[] = {1, 0, -1, -1, -1, 0, 1, 1};

  int warpCnt = 0;
  while (!que.empty()) {
    std::pair<int, int> pos = que.front();
    que.pop();

    if (pos.first == Ch && pos.second == Cw) {
      printf("%d\\n", warpCnt);
      return;
    }

    if (flag[pos.first][pos.second] == -1) {
      flag[pos.first][pos.second] = warpCnt;
    }

    for (int i = 0; i < 4; i++) {
      int nh = pos.first + dh[i];
      int nw = pos.second + dw[i];

      if ((nh >= 0) && (nh < H) && (nw >= 0) && (nw < W)) {
        if (nh == Ch && nw == Cw) {
          printf("%d\\n", warpCnt);
          return;
        }
        if ((grid[nh][nw] == '.') && (flag[nh][nw] == -1)) {
          que.emplace(nh, nw);
        } else if (grid[nh][nw] == '#') {

          for (int j = 0; j < 9; j++) {
            int nnh = nh + ddh[j];
            int nnw = nw + ddw[j];

            if ((nnh >= 0) && (nnh < H) && (nnw >= 0) && (nnw < W)) {
              if ((grid[nnh][nnw] == '.') && (flag[nnh][nnw] == -1)) {
                nextque.emplace(nnh, nnw);
              }
            }
          }
        }
      }
    }
    if (que.empty()) {
      std::swap(que, nextque);
      warpCnt++;
    }
  }
  printf("-1\\n");
}