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

#include <bits/stdc++.h>
using namespace std;
#define INF 1e9 + 7
typedef long long int ll;
const int maxn = 1000;
ll mod = INF;

char g[maxn + 5][maxn + 5];
bool used[maxn + 5][maxn + 5];
int dis[maxn + 5][maxn + 5];
int h, w;
ll ans = 0;
ll ch, cw;
ll dh, dw;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

int main() {

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> g[i][j];
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dis[i][j] = -1;
    }
  }

  queue<pair<int, int>> que;
  queue<pair<int, int>> searched;
  queue<pair<int, int>> souko;
  que.push(make_pair(ch, cw));
  souko.push(make_pair(ch, cw));
  used[ch][cw] = true;
  int cost = 0;
  dis[ch][cw] = cost;

  while (true) {

    while (!souko.empty()) {
      searched.push(souko.front());
      souko.pop();
    }

    while (!que.empty()) {
      pair<int, int> arg = que.front();
      que.pop();
      for (int i = 0; i < 4; i++) {
        int ny = arg.first + dy[i], nx = arg.second + dx[i];
        if (0 <= ny && ny < h && 0 <= nx && nx < w && !used[ny][nx] &&
            g[ny][nx] == '.') {
          dis[ny][nx] = cost;
          used[ny][nx] = true;
          que.push(pair<int, int>(ny, nx));
          searched.push(pair<int, int>(ny, nx));
        }
      }
    }

    if (searched.empty())
      break;

    cost++;

    while (!searched.empty()) {
      pair<int, int> arg = searched.front();
      searched.pop();
      for (int ny = arg.first - 2; ny <= arg.first + 2; ny++) {
        for (int nx = arg.second - 2; nx <= arg.second + 2; nx++) {
          if (0 <= ny && ny < h && 0 <= nx && nx < w && !used[ny][nx] &&
              g[ny][nx] == '.') {
            dis[ny][nx] = cost;
            used[ny][nx] = true;
            que.push(make_pair(ny, nx));
            souko.push(make_pair(ny, nx));
          }
        }
      }
    }
  }

  // for(int i=0;i<h;i++){
  //     for(int j=0;j<w;j++){
  //         cout << dis[i][j]<<" ";
  //     }
  //     cout << endl;
  // }

  cout << dis[dh][dw] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1e9 + 7
typedef long long int ll;
const int maxn = 1000;
ll mod = INF;

char g[maxn + 5][maxn + 5];
bool used[maxn + 5][maxn + 5];
int dis[maxn + 5][maxn + 5];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int h, w;
ll ans = 0;
ll ch, cw;
ll dh, dw;

int main() {

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> g[i][j];
    }
  }

  queue<pair<int, int>> que;
  int group = 0;

  while (true) {
    bool key = false;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!used[i][j] && g[i][j] == '.') {
          group++;
          que.push(make_pair(i, j));
          dis[i][j] = group;
          key = true;
          break;
        }
      }
      if (key)
        break;
    }

    if (!key)
      break;

    while (!que.empty()) {
      pair<int, int> arg = que.front();
      que.pop();
      used[arg.first][arg.second] = true;
      for (int i = 0; i < 4; i++) {
        int ny = arg.first + dy[i];
        int nx = arg.second + dx[i];
        if (0 <= ny && ny < h && 0 <= nx && nx < w) {
          if (!used[ny][nx] && g[ny][nx] == '.') {
            dis[ny][nx] = group;
            used[ny][nx] = used;
            que.push(make_pair(ny, nx));
          }
        }
      }
    }
  }

  vector<vector<int>> path(maxn);
  bool added[maxn][maxn] = {0};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (g[i][j] == '#')
        continue;
      int from = dis[i][j];
      for (int ny = i - 2; ny <= i + 2; ny++) {
        for (int nx = j - 2; nx <= j + 2; nx++) {
          if (g[ny][nx] == '#')
            continue;
          int to = dis[ny][nx];
          if (0 <= nx && nx < w && 0 <= ny && ny < h) {
            if (from != to) {
              if (!added[from][to]) {
                path[from].push_back(to);
                added[from][to] = true;
              }
            }
          }
        }
      }
    }
  }
  // cout << "group"<<endl;
  // for(int i=0;i<h;i++){
  //     for(int j=0;j<w;j++){
  //         cout << dis[i][j]<<" ";
  //     }
  //     cout << endl;
  // }
  // cout << "path"<<endl;
  // for(int i=0;i<4;i++){
  //     cout << "size "<<path[i].size()<<" : ";
  //     for(int j=0;j<path[i].size();j++){
  //         cout << path[i][j]<<" ";
  //     }
  //     cout << endl;
  // }

  bool gused[maxn + 5] = {0};
  int gdis[maxn + 5] = {0};
  int goal = dis[dh][dw];
  int start = dis[ch][cw];

  for (int i = 0; i <= group; i++)
    gdis[i] = INF;
  gdis[start] = 0;
  queue<int> gque;
  gque.push(start);
  gused[start] = true;

  // cout << "start "<<start<<" goal "<<goal<<endl;
  while (!gque.empty()) {
    int arg = gque.front();
    gque.pop();
    // cout << "arg "<<arg<<endl;
    for (int i = 0; i < (int)path[arg].size(); i++) {
      if (!gused[path[arg][i]]) {
        gque.push(path[arg][i]);
        gdis[path[arg][i]] = min(gdis[path[arg][i]], gdis[arg] + 1);
        gused[path[arg][i]] = true;
      }
    }
  }

  // cout << "gdis"<<endl;
  // for(int i=1;i<=group;i++){
  //     cout << gdis[i]<<" ";
  // }
  // cout << endl;

  if (gdis[goal] == INF)
    cout << -1 << endl;
  else
    cout << gdis[goal] << endl;

  return 0;
}