alexjercan/bugnet huggingface dataset:
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

char c[1010][1010];
int d[1010][1010];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < 1010; ++i)
    for (int j = 0; j < 1010; ++j)
      d[i][j] = 1e9;

  int n, m;
  cin >> n >> m;

  int a, b, s, t;
  cin >> a >> b >> s >> t;
  a--, b--, s--, t--;

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> c[i][j];

  deque<pair<int, int>> q;

  q.push_front({a, b});
  d[a][b] = 0;
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop_front();

    if (x == s and y == t)
      break;

    for (int i = 0; i < 4; ++i) {
      int u = dx[i] + x;
      int v = dy[i] + y;
      if (u >= 0 and u < n and v >= 0 and v < m and c[u][v] == '.') {
        if (d[x][y] < d[u][v]) {
          d[u][v] = d[x][y];
          q.push_front({u, v});
        }
      }
    }

    for (int i = x - 2; i <= x + 2; ++i) {
      for (int j = y - 2; j <= y + 2; ++j) {
        int u = i, v = j;
        if (u >= 0 and u < n and v >= 0 and v < m and c[u][v] == '.') {
          if (d[x][y] + 1 < d[u][v]) {
            d[u][v] = d[x][y] + 1;
            q.push_back({u, v});
          }
        }
      }
    }
  }
  if (d[s][t] == 1e9)
    cout << "-1\\n";
  else
    cout << d[s][t] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

char c[1010][1010];
int d[1010][1010];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int main() {

  for (int i = 0; i < 1010; ++i)
    for (int j = 0; j < 1010; ++j)
      d[i][j] = 1e9;

  int n, m;
  cin >> n >> m;

  int a, b, s, t;
  cin >> a >> b >> s >> t;
  a--, b--, s--, t--;

  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> c[i][j];

  priority_queue<tuple<int, int, int>> q;

  q.push({0, a, b});
  d[a][b] = 0;

  while (!q.empty()) {
    auto [w, x, y] = q.top();
    q.pop();

    for (int i = 0; i < 4; ++i) {
      int u = dx[i] + x;
      int v = dy[i] + y;
      if (u >= 0 and u < n and v >= 0 and v < m and c[u][v] == '.') {
        if (d[x][y] < d[u][v]) {
          d[u][v] = d[x][y];
          q.push({d[u][v], u, v});
        }
      }
    }

    for (int i = x - 2; i <= x + 2; ++i) {
      for (int j = y - 2; j <= y + 2; ++j) {
        int u = i, v = j;
        if (u >= 0 and u < n and v >= 0 and v < m and c[u][v] == '.') {
          if (d[x][y] + 1 < d[u][v]) {
            d[u][v] = d[x][y] + 1;
            q.push({d[u][v], u, v});
          }
        }
      }
    }
  }
  if (d[s][t] == 1e9)
    cout << "-1\\n";
  else
    cout << d[s][t] << "\\n";
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

int H, W, Ch, Cw, Dh, Dw;
string S;
bool b[1001][1001];
int t[1001][1001];

pair<int, int> d[4]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int main() {
  fill((int *)t, (int *)t + 1001 * 1001, -1);

  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  for (int i = 1; i <= H; ++i) {
    cin >> S;
    for (int j = 1; j <= W; ++j) {
      b[i][j] = S[j - 1] == '.';
    }
  }

  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      q;
  q.emplace(0, Ch, Cw);
  t[Ch][Cw] = 0;
  while (!q.empty()) {
    auto [T, h, w] = q.top();
    q.pop();
    if (t[h][w] < T)
      continue;
    for (auto [dh, dw] : d) {
      int nh = h + dh, nw = w + dw;
      if (nh < 1 || nh > H || nw < 1 || nw > W || !b[nh][nw])
        continue;
      int nt = t[nh][nw];
      if (nt != -1 && nt <= T)
        continue;
      t[nh][nw] = T;
      q.emplace(T, nh, nw);
    }

    for (int dw = -2; dw != 3; ++dw) {
      for (int dh = -2; dh != 3; ++dh) {
        int nh = h + dh, nw = w + dw;
        if (nh < 1 || nh > H || nw < 1 || nw > W || !b[nh][nw])
          continue;
        int nt = t[nh][nw];
        if (nt != -1 && nt <= T + 1)
          continue;
        t[nh][nw] = T + 1;
        q.emplace(T + 1, nh, nw);
      }
    }
  }

  cout << t[Dh][Dw] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

int H, W, Ch, Cw, Dh, Dw;
string S;
bool b[1001][1001];
int t[1001][1001];

pair<int, int> d[4]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int main() {
  fill((int *)t, (int *)t + 1001 * 1001, -1);

  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  for (int i = 1; i <= H; ++i) {
    cin >> S;
    for (int j = 1; j <= W; ++j) {
      b[i][j] = S[j - 1] == '.';
    }
  }

  queue<pair<int, int>> q;
  q.emplace(Ch, Cw);
  t[Ch][Cw] = 0;
  while (!q.empty()) {
    auto [h, w] = q.front();
    int T = t[h][w];
    q.pop();
    for (auto [dh, dw] : d) {
      int nh = h + dh, nw = w + dw;
      if (nh < 1 || nh > H || nw < 1 || nw > W || !b[nh][nw])
        continue;
      int nt = t[nh][nw];
      if (nt != -1 && nt <= T)
        continue;
      t[nh][nw] = T;
      q.emplace(nh, nw);
    }

    for (int dw = -2; dw != 3; ++dw) {
      for (int dh = -2; dh != 3; ++dh) {
        int nh = h + dh, nw = w + dw;
        if (nh < 1 || nh > H || nw < 1 || nw > W || !b[nh][nw])
          continue;
        int nt = t[nh][nw];
        if (nt != -1 && nt <= T + 1)
          continue;
        t[nh][nw] = T + 1;
        q.emplace(nh, nw);
      }
    }
  }

  cout << t[Dh][Dw] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-10
using namespace std;
int H, W, ch, dh, cw, dw, dis, h, w;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
tuple<int, int, int> now;
bool gone[1005][1005];
char s[1005][1005];
deque<tuple<int, int, int>> bfs;
int main() {
  scanf("%d%d", &H, &W);
  scanf("%d%d%d%d", &ch, &cw, &dh, &dw);
  for (int i = 1; i <= H; i++)
    scanf("%s", s[i] + 1);
  bfs.push_back({ch, cw, 0});
  while (!bfs.empty()) {
    now = bfs.front();
    tie(h, w, dis) = now;
    bfs.pop_front();
    if (h == dh && w == dw) {
      printf("%d", dis);
      return 0;
    }
    if (gone[h][w])
      continue;
    gone[h][w] = 1;
    for (int i = 0; i <= 3; i++) {
      int tx = h + dx[i], ty = w + dy[i];
      if (s[tx][ty] == '#' || tx < 1 || tx > H || ty < 1 || ty > W)
        continue;
      if (!gone[tx][ty])
        bfs.push_front({tx, ty, dis});
      ;
    }
    for (int tx = h - 2; tx <= h + 2; tx++)
      for (int ty = w - 2; ty <= w + 2; ty++) {
        if (s[tx][ty] == '#' || tx < 1 || tx > H || ty < 1 || ty > W)
          continue;
        if (!gone[tx][ty])
          bfs.push_back({tx, ty, dis + 1});
      }
  }
  printf("-1");
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-10
using namespace std;
int H, W, ch, dh, cw, dw, dis, h, w;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
tuple<int, int, int> now;
map<pair<int, int>, bool> gone;
char s[1005][1005];
deque<tuple<int, int, int>> bfs;
int main() {
  scanf("%d%d", &H, &W);
  scanf("%d%d%d%d", &ch, &cw, &dh, &dw);
  for (int i = 1; i <= H; i++)
    scanf("%s", s[i] + 1);
  bfs.push_back({ch, cw, 0});
  while (!bfs.empty()) {
    now = bfs.front();
    h = get<0>(now);
    w = get<1>(now);
    dis = get<2>(now);
    /*printf("h=%d w=%d dis=%d\\n",h,w,dis);*/
    if (h == dh && w == dw) {
      printf("%d", dis);
      return 0;
    }
    gone[{h, w}] = 1;
    bfs.pop_front();
    for (int i = 0; i <= 3; i++) {
      int tx = h + dx[i], ty = w + dy[i];
      if (s[tx][ty] == '#' || tx < 1 || tx > H || ty < 1 || ty > W)
        continue;
      if (!gone[{tx, ty}]) {
        bfs.push_front({tx, ty, dis});
        ;
        gone[{tx, ty}] = 1;
      }
    }
    for (int tx = h - 2; tx <= h + 2; tx++)
      for (int ty = w - 2; ty <= w + 2; ty++) {
        if (s[tx][ty] == '#' || tx < 1 || tx > H || ty < 1 || ty > W)
          continue;
        if (!gone[{tx, ty}]) {
          bfs.push_back({tx, ty, dis + 1});
        }
      }
  }
  printf("-1");
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int INF = 1001001001;

char mp[1005][1005];
int dist[1005][1005];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(y, h) rep(x, w) {
    cin >> mp[x][y];
    dist[x][y] = INF;
  }
  priority_queue<PP, vector<PP>, greater<PP>> q;
  q.emplace(0, make_pair(cw, ch));
  while (!q.empty()) {
    int x = q.top().second.first;
    int y = q.top().second.second;
    int d = q.top().first;
    q.pop();
    if (d >= dist[x][y])
      continue;
    dist[x][y] = d;
    rep(v, 4) {
      int nx = x + dx[v];
      int ny = y + dy[v];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (mp[nx][ny] == '#')
        continue;
      q.emplace(d, make_pair(nx, ny));
    }
    for (int ex = -2; ex <= 2; ex++) {
      for (int ey = -2; ey <= 2; ey++) {
        int nx = x + ex;
        int ny = y + ey;
        if (nx < 0 || w <= nx || ny < 0 || h <= ny)
          continue;
        if (mp[nx][ny] == '#')
          continue;
        q.emplace(d + 1, make_pair(nx, ny));
      }
    }
  }
  int ans = dist[dw][dh];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;

char mp[1005][1005];
int dist[1005][1005];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

class state {
public:
  int x;
  int y;
  int d;
  state(int x, int y, int d) : x(x), y(y), d(d){};
};

struct Compare {
  bool operator()(const state &lhs, const state &rhs) { return lhs.d < rhs.d; }
};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(y, h) rep(x, w) {
    cin >> mp[x][y];
    dist[x][y] = INF;
  }
  priority_queue<state, vector<state>, Compare> q;
  q.emplace(cw, ch, 0);
  while (!q.empty()) {
    int x = q.top().x;
    int y = q.top().y;
    int d = q.top().d;
    q.pop();
    if (d >= dist[x][y])
      continue;
    dist[x][y] = d;
    rep(v, 4) {
      int nx = x + dx[v];
      int ny = y + dy[v];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (mp[nx][ny] == '#')
        continue;
      q.emplace(nx, ny, d);
    }
    for (int ex = -2; ex <= 2; ex++) {
      for (int ey = -2; ey <= 2; ey++) {
        int nx = x + ex;
        int ny = y + ey;
        if (nx < 0 || w <= nx || ny < 0 || h <= ny)
          continue;
        if (mp[nx][ny] == '#')
          continue;
        q.emplace(nx, ny, d + 1);
      }
    }
  }
  int ans = dist[dw][dh];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define Int int64_t
#define ll long long
#define pb push_back
using namespace std;
const int MAXN = 500005;

int main() {
  Int N, M;
  cin >> N >> M;
  Int Sa, Sb;
  cin >> Sa >> Sb;
  --Sa;
  --Sb;
  Int Da, Db;
  cin >> Da >> Db;
  --Da;
  --Db;

  vector<vector<int>> board(N, vector<int>(M));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      char x;
      cin >> x;
      board[i][j] = (x == '.' ? 1 : 0);
    }
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({Sa, Sb});
  vector<vector<int>> dis(N, vector<int>(M, 1e9));
  dis[Sa][Sb] = 0;
  vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

  while (!q.empty()) {

    auto x = q.top();
    q.pop();

    for (auto itr : dir) {
      int i = x.first + itr.first;
      int j = x.second + itr.second;
      if (i < 0 || j < 0 || i >= N || j >= M || !board[i][j])
        continue;
      if (dis[i][j] > dis[x.first][x.second]) {
        dis[i][j] = dis[x.first][x.second];
        q.push({i, j});
      }
    }

    for (int i = x.first - 2; i <= x.first + 2; ++i) {
      for (int j = x.second - 2; j <= x.second + 2; ++j) {
        if (i < 0 || j < 0 || i >= N || j >= M)
          continue;
        if (board[i][j] == 0)
          continue;
        if (dis[i][j] > dis[x.first][x.second] + 1) {
          dis[i][j] = 1 + dis[x.first][x.second];
          q.push({i, j});
        }
      }
    }
  }
  cout << (dis[Da][Db] == 1e9 ? -1 : dis[Da][Db]) << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Int int64_t
#define ll long long
#define pb push_back
using namespace std;
const int MAXN = 500005;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  Int N, M;
  cin >> N >> M;
  Int Sa, Sb;
  cin >> Sa >> Sb;
  --Sa;
  --Sb;
  Int Da, Db;
  cin >> Da >> Db;
  --Da;
  --Db;

  vector<vector<int>> board(N, vector<int>(M));
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      char x;
      cin >> x;
      board[i][j] = (x == '.' ? 1 : 0);
    }
  }

  queue<pair<int, int>> q;
  q.push({Sa, Sb});
  vector<vector<int>> dis(N, vector<int>(M, 1e9));
  dis[Sa][Sb] = 0;
  vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};

  while (!q.empty()) {

    auto x = q.front();
    q.pop();

    for (auto itr : dir) {
      int i = x.first + itr.first;
      int j = x.second + itr.second;
      if (i < 0 || j < 0 || i >= N || j >= M || !board[i][j])
        continue;
      if (dis[i][j] > dis[x.first][x.second]) {
        dis[i][j] = dis[x.first][x.second];
        q.push({i, j});
      }
    }

    for (int i = x.first - 2; i <= x.first + 2; ++i) {
      for (int j = x.second - 2; j <= x.second + 2; ++j) {
        if (i < 0 || j < 0 || i >= N || j >= M)
          continue;
        if (board[i][j] == 0)
          continue;
        if (dis[i][j] > dis[x.first][x.second] + 1) {
          dis[i][j] = 1 + dis[x.first][x.second];
          q.push({i, j});
        }
      }
    }
  }
  cout << (dis[Da][Db] == 1e9 ? -1 : dis[Da][Db]) << "\\n";
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {

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

  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[ch][cw] = 0;

  deque<P> que;
  que.push_front(P(ch, cw));

  while (!que.empty()) {
    auto pos = que.front();
    int x = pos.first;
    int y = pos.second;
    que.pop_front();
    if (x == dh && y == dw) {
      cout << dist[x][y] << endl;
      return 0;
    }

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];

      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= dist[x][y] && dist[nx][ny] != -1)
        continue;

      que.push_front(P(nx, ny));
      dist[nx][ny] = dist[x][y];
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;

        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= dist[x][y] + 1 && dist[nx][ny] != -1)
          continue;

        dist[nx][ny] = dist[x][y] + 1;
        que.push_back(P(nx, ny));
      }
    }
  }
  cout << dist[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

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

  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[ch][cw] = 0;

  queue<P> que;
  que.push(P(ch, cw));

  while (!que.empty()) {
    auto pos = que.front();
    int x = pos.first;
    int y = pos.second;
    que.pop();

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];

      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= dist[x][y] && dist[nx][ny] != -1)
        continue;

      que.push(P(nx, ny));
      dist[nx][ny] = dist[x][y];
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i;
        int ny = y + j;

        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= dist[x][y] + 1 && dist[nx][ny] != -1)
          continue;

        dist[nx][ny] = dist[x][y] + 1;
        que.push(P(nx, ny));
      }
    }
  }
  /*    rep(i,h){
          rep(j,w) cout << dist[i][j] << ' ';
          cout << endl;
      }
  */
  cout << dist[dh][dw];
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define maxn 1003
#define mod 1000000007
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pi;

struct mydata {
  int r, c, d;
};

mydata newData(int r, int c, int d) {
  mydata s;
  s.r = r;
  s.c = c;
  s.d = d;
  return s;
}

bool operator<(const mydata &a, const mydata &b) { return a.d > b.d; }

int n, m, ax, ay, bx, by;
int dist[maxn][maxn];
string s[maxn];
int dir[4] = {0, 1, 0, -1};
priority_queue<mydata> Q;

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &ax, &ay, &bx, &by);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  ax--, ay--, bx--, by--;
  memset(dist, -1, sizeof(dist));
  Q.push(newData(ax, ay, 0));
  while (!Q.empty()) {
    mydata temp = Q.top();
    Q.pop();
    int r = temp.r;
    int c = temp.c;
    int d = temp.d;
    dist[r][c] = d;
    if (r == bx && c == by)
      break;
    for (int i = 0; i < 4; i++) {
      int x = r + dir[i];
      int y = c + dir[i ^ 1];
      if (x < 0 || x >= n || y < 0 || y >= m)
        continue;
      if (dist[x][y] != -1 || s[x][y] == '#')
        continue;
      Q.push(newData(x, y, d));
    }
    for (int x = r - 2; x <= r + 2; x++)
      for (int y = c - 2; y <= c + 2; y++) {
        if (x < 0 || x >= n || y < 0 || y >= m)
          continue;
        if (dist[x][y] != -1 || s[x][y] == '#')
          continue;
        Q.push(newData(x, y, d + 1));
      }
  }
  printf("%d\\n", dist[bx][by]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define maxn 1000
#define mod 1000000007
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pi;

struct mydata {
  int r, c, d;
};

mydata newData(int r, int c, int d) {
  mydata s;
  s.r = r;
  s.c = c;
  s.d = d;
  return s;
}

bool operator<(const mydata &a, const mydata &b) { return a.d > b.d; }

int n, m, ax, ay, bx, by;
int dist[maxn][maxn];
string s[maxn];
int dir[4] = {0, 1, 0, -1};
priority_queue<mydata> Q;

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &ax, &ay, &bx, &by);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  memset(dist, -1, sizeof(dist));
  Q.push(newData(ax, ay, 0));
  while (!Q.empty()) {
    mydata temp = Q.top();
    Q.pop();
    int r = temp.r;
    int c = temp.c;
    int d = temp.d;
    dist[r][c] = d;
    if (r == bx && c == by)
      break;
    for (int i = 0; i < 4; i++) {
      int x = r + dir[i];
      int y = c + dir[i ^ 1];
      if (!x || x > n || !y || y > m)
        continue;
      if (dist[x][y] != -1 || s[x][y] == '#')
        continue;
      Q.push(newData(x, y, d));
    }
    for (int x = r - 2; x <= r + 2; x++)
      for (int y = c - 2; y <= c + 2; y++) {
        if (x < 1 || x > n || y < 1 || y > m)
          continue;
        if (dist[x][y] != -1 || s[x][y] == '#')
          continue;
        Q.push(newData(x, y, d + 1));
      }
  }
  printf("%d\\n", dist[bx][by]);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <queue>
using namespace std;
const int N = 1205;
const int INF = 0x3f3f3f3f;
struct Node {
  int x, y, s;
};
char c[N][N];
bool vis[N][N];
int dx[5] = {0, 1, -1, 2, -2};
int sx[4] = {0, 0, -1, 1};
int sy[4] = {1, -1, 0, 0};
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  int ch, cw, dh, dw;
  scanf("%d %d %d %d", &ch, &cw, &dh, &dw);
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < h; i++)
    scanf("%s", c[i]);
  if (c[ch][cw] == '#' || c[dh][dw] == '#') {
    puts("-1");
    return 0;
  }
  deque<Node> q;
  q.push_front({ch, cw, 0});
  int ans = 0x3f3f3f3f;
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, s = q.front().s;
    q.pop_front();
    if (x == dh && y == dw) {
      // cout<<x<<" "<<y<<" "<<s<<endl;
      ans = min(ans, s);
    }
    if (vis[x][y] == 1)
      continue;
    vis[x][y] = 1;
    for (int i = 0; i < 5; i++) {
      int px = 0, py = 0;
      if (i < 4) {
        px = x + sx[i];
        py = y + sy[i];
        if (px >= 0 && px < h && py >= 0 && py < w && vis[px][py] == 0 &&
            c[px][py] != '#')
          q.push_front({px, py, s});
      }
      for (int j = 0; j < 5; j++) {
        int tx = x + dx[i], ty = y + dx[j];
        if (abs(dx[i]) + abs(dx[j]) > 1 && tx >= 0 && tx < h && ty >= 0 &&
            ty < w && vis[tx][ty] == 0 && c[tx][ty] != '#')
          q.push_back({tx, ty, s + 1});
      }
    }
  }
  printf("%d\\n", ans == INF ? -1 : ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;
const int N = 1205;
const int INF = 0x3f3f3f3f;
struct Node {
  int x, y, s;
};
char c[N][N];
bool vis[N][N];
int dx[5] = {0, 1, -1, 2, -2};
int dy[5] = {0, 1, -1, 2, -2};
int sx[4] = {0, 0, -1, 1};
int sy[4] = {1, -1, 0, 0};
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  int ch, cw, dh, dw;
  scanf("%d %d %d %d", &ch, &cw, &dh, &dw);
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < h; i++)
    scanf("%s", c[i]);
  if (c[ch][cw] == '#' || c[dh][dw] == '#') {
    puts("-1");
    return 0;
  }
  deque<Node> q;
  q.push_front({ch, cw, 0});
  int ans = 0x3f3f3f3f;
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, s = q.front().s;
    q.pop_front();
    if (x == dh && y == dw) {
      ans = min(ans, s);
    }
    if (vis[x][y] == 1)
      continue;
    vis[x][y] = 1;
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        int tx = x + dx[i], ty = y + dy[j], px = x + sx[i], py = y + sy[i];
        if (i < 4 && px >= 0 && px < h && py >= 0 && py < w &&
            vis[px][py] == 0 && c[px][py] != '#')
          q.push_back({px, py, s});
        if (tx >= 0 && tx < h && ty >= 0 && ty < w && vis[tx][ty] == 0 &&
            c[tx][ty] != '#')
          q.push_back({tx, ty, s + 1});
      }
    }
  }
  printf("%d\\n", ans == INF ? -1 : ans);
  return 0;
}