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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
#define PUTS(x) cout << (x) << endl;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(x) ((int)(x).size())

#define INF 1e12

using Coordinate = pair<Int, Int>;

int main() {
  Int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  auto s = vector<string>();
  rep(i, h) {
    string x;
    cin >> x;
    s.PB(x);
  }
  auto t = vector<vector<Int>>(h, vector<Int>(w, INF));

  auto compare = [&](const Coordinate &p1, const Coordinate &p2) -> bool {
    return t[p1.F][p1.S] > t[p2.F][p2.S];
  };

  priority_queue<Coordinate, vector<Coordinate>, decltype(compare)> q(compare);

  q.push(MP(ch, cw));

  t[ch][cw] = 0;

  auto test = [&](Int x, Int y, Int new_v) {
    if (0 <= x && x < h && 0 <= y && y < w && s[x][y] == '.') {
      auto old_v = t[x][y];
      if (new_v < old_v) {
        t[x][y] = new_v;
        q.push(MP(x, y));
      }
    }
  };

  while (SZ(q) > 0) {
    auto p = q.top();
    q.pop();
    auto x = p.F;
    auto y = p.S;
    auto v = t[x][y];
    test(x - 1, y, v);
    test(x + 1, y, v);
    test(x, y - 1, v);
    test(x, y + 1, v);

    rep(i, 5) {
      rep(j, 5) { test(x - 2 + i, y - 2 + j, v + 1); }
    }
  }

  auto ans = t[dh][dw];
  PUTS(ans == INF ? -1 : ans)
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
#define PUTS(x) cout << (x) << endl;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(x) ((int)(x).size())

#define INF 1e12

int main() {
  Int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  auto s = vector<string>();
  rep(i, h) {
    string x;
    cin >> x;
    s.PB(x);
  }
  auto t = vector<vector<Int>>(h, vector<Int>(w, INF));
  auto q = queue<pair<Int, Int>>();
  q.push(MP(ch, cw));
  t[ch][cw] = 0;

  auto test = [&](Int x, Int y, Int new_v) {
    if (0 <= x && x < h && 0 <= y && y < w && s[x][y] == '.') {
      auto old_v = t[x][y];
      if (new_v < old_v) {
        t[x][y] = new_v;
        q.push(MP(x, y));
      }
    }
  };

  while (SZ(q) > 0) {
    auto p = q.front();
    q.pop();
    auto x = p.F;
    auto y = p.S;
    auto v = t[x][y];
    test(x - 1, y, v);
    test(x + 1, y, v);
    test(x, y - 1, v);
    test(x, y + 1, v);

    rep(i, 5) {
      rep(j, 5) { test(x - 2 + i, y - 2 + j, v + 1); }
    }
  }

  auto ans = t[dh][dw];
  PUTS(ans == INF ? -1 : ans)
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;

  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> s(h);

  rep(i, h) { cin >> s[i]; }

  deque<pair<int, pair<int, int>>> deq;
  vector<vector<int>> dist(h, vector<int>(w, h * w));

  deq.push_front(make_pair(0, make_pair(ch, cw)));

  while (!deq.empty()) {
    int tmpd = deq.front().first;
    int tmpy = deq.front().second.first;
    int tmpx = deq.front().second.second;

    deq.pop_front();

    if (tmpd < dist[tmpy][tmpx]) {
      dist[tmpy][tmpx] = tmpd;

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int nexty = tmpy + i;
          int nextx = tmpx + j;

          if (0 <= nexty && nexty < h && 0 <= nextx && nextx < w &&
              s[nexty][nextx] == '.') {
            if (abs(i) + abs(j) == 0) {

            } else if (abs(i) + abs(j) == 1) {
              deq.push_front(make_pair(tmpd, make_pair(nexty, nextx)));
            } else {
              deq.push_back(make_pair(tmpd + 1, make_pair(nexty, nextx)));
            }
          }
        }
      }
    }
  }

  cout << (dist[dh][dw] == h * w ? -1 : dist[dh][dw]) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;

  ch--;
  cw--;
  dh--;
  dw--;

  vector<string> s(h);

  rep(i, h) { cin >> s[i]; }

  deque<pair<int, pair<int, int>>> deq;
  vector<vector<int>> dist(h, vector<int>(w, h * w));

  deq.push_front(make_pair(0, make_pair(ch, cw)));

  while (!deq.empty()) {
    int tmpd = deq.front().first;
    int tmpy = deq.front().second.first;
    int tmpx = deq.front().second.second;

    deq.pop_front();

    if (tmpd < dist[tmpy][tmpx]) {
      cerr << tmpy << " " << tmpx << "  " << tmpd << endl;
      dist[tmpy][tmpx] = tmpd;

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int nexty = tmpy + i;
          int nextx = tmpx + j;

          if (0 <= nexty && nexty < h && 0 <= nextx && nextx < w &&
              s[nexty][nextx] == '.') {
            if (abs(i) + abs(j) == 0) {

            } else if (abs(i) + abs(j) == 1) {
              deq.push_front(make_pair(tmpd, make_pair(nexty, nextx)));
            } else {
              deq.push_back(make_pair(tmpd + 1, make_pair(nexty, nextx)));
            }
          }
        }
      }
    }
  }

  cout << (dist[dh][dw] == h * w ? -1 : dist[dh][dw]) << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vll = vector<ll>;
#define all(v) v.begin(), v.end()

#define P pair<int, int>
#define INF 1e9

int H, W;
char c[1010][1010];
int dx[4]{0, 0, 1, -1};
int dy[4]{1, -1, 0, 0};
vector<vector<int>> dist;

void bfs(P s) {
  deque<P> que;
  que.push_back(s);
  while (!que.empty()) {
    P q = que.front();
    que.pop_front();
    int x = q.first, y = q.second;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (dist[nx][ny] <= dist[x][y])
        continue;
      else {
        if (c[nx][ny] == '.' && dist[nx][ny] > dist[x][y]) {
          dist[nx][ny] = dist[x][y];
          que.push_front(make_pair(nx, ny));
        }
      }
    }
    for (int a = x - 2; a <= x + 2; ++a) {
      for (int b = y - 2; b <= y + 2; ++b) {
        if (a < 0 || a >= H || b < 0 || b >= W)
          continue;
        if (c[a][b] == '.' && dist[a][b] > dist[x][y] + 1) {
          dist[a][b] = dist[x][y] + 1;
          que.push_back(make_pair(a, b));
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;
  dist.resize(H);
  rep(i, H) dist[i].resize(W);
  rep(i, H) rep(j, W) dist[i][j] = INF;
  int sh, sw;
  cin >> sh >> sw;
  sh--, sw--;
  int gh, gw;
  cin >> gh >> gw;
  gh--, gw--;
  rep(i, H) rep(j, W) cin >> c[i][j];
  P s = make_pair(sh, sw);
  dist[sh][sw] = 0;
  bfs(s);
  cout << (dist[gh][gw] == INF ? -1 : dist[gh][gw]) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using vll = vector<ll>;
#define all(v) v.begin(), v.end()

#define P pair<int, int>
#define INF 1e9

int H, W;
char c[1010][1010];
int dx[4]{0, 0, 1, -1};
int dy[4]{1, -1, 0, 0};
vector<vector<int>> dist;

void bfs(P s) {
  queue<P> que;
  que.push(s);
  while (!que.empty()) {
    P q = que.front();
    que.pop();
    int x = q.first, y = q.second;
    rep(i, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (dist[nx][ny] <= dist[x][y])
        continue;
      else {
        if (c[nx][ny] == '.' && dist[nx][ny] > dist[x][y]) {
          dist[nx][ny] = dist[x][y];
          que.push(make_pair(nx, ny));
        }
      }
    }
    for (int a = x - 2; a <= x + 2; ++a) {
      for (int b = y - 2; b <= y + 2; ++b) {
        if (a < 0 || a >= H || b < 0 || b >= W)
          continue;
        if (c[a][b] == '.' && dist[a][b] > dist[x][y] + 1) {
          dist[a][b] = dist[x][y] + 1;
          que.push(make_pair(a, b));
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;
  dist.resize(H);
  rep(i, H) dist[i].resize(W);
  rep(i, H) rep(j, W) dist[i][j] = INF;
  int sh, sw;
  cin >> sh >> sw;
  sh--, sw--;
  int gh, gw;
  cin >> gh >> gw;
  gh--, gw--;
  rep(i, H) rep(j, W) cin >> c[i][j];
  P s = make_pair(sh, sw);
  dist[sh][sw] = 0;
  bfs(s);
  cout << (dist[gh][gw] == INF ? -1 : dist[gh][gw]) << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m;
char s[maxn][maxn];
int cnt, col[maxn][maxn];
void dfs(int x, int y) {
  col[x][y] = cnt;
  for (int i = 0; i < 4; i++) {
    int p = x + dx[i], q = y + dy[i];
    if (0 <= p && p < n && 0 <= q && q < m && s[p][q] == '.' && !col[p][q])
      dfs(p, q);
  }
  return;
}
set<int> nei[maxn * maxn];
int dis[maxn * maxn], que[maxn * maxn];
int main() {
  scanf("%d%d", &n, &m);
  int sx, sy, ex, ey;
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < n; i++)
    scanf("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == '.' && !col[i][j]) {
        cnt++;
        dfs(i, j);
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      for (int r = -2; r <= 2; r++)
        for (int c = -2; c <= 2; c++) {
          int p = i + r, q = j + c;
          if (0 <= p && p < n && 0 <= q && q < m && col[p][q] &&
              col[p][q] != col[i][j])
            nei[col[i][j]].insert(col[p][q]);
        }
  int head = 0, tail = 0;
  que[tail++] = col[sx][sy];
  memset(dis, 0x3f, sizeof(dis));
  dis[col[sx][sy]] = 0;
  while (head < tail) {
    int cur = que[head++];
    for (set<int>::iterator it = nei[cur].begin(); it != nei[cur].end(); it++) {
      int next = *it;
      if (dis[cur] + 1 < dis[next]) {
        dis[next] = dis[cur] + 1;
        que[tail++] = next;
      }
    }
  }
  printf("%d\\n", dis[col[ex][ey]] == INF ? -1 : dis[col[ex][ey]]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 1005;
const int INF = 0x3f3f3f3f;
char s[maxn][maxn];
int dis[maxn][maxn];
queue<pair<int, int>> que;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int sx, sy, ex, ey;
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < n; i++)
    scanf("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      dis[i][j] = INF;
  dis[sx][sy] = 0;
  que.push(make_pair(sx, sy));
  while (que.size()) {
    int x = que.front().first, y = que.front().second;
    que.pop();
    for (int r = -2; r <= 2; r++)
      for (int c = -2; c <= 2; c++) {
        int p = x + r, q = y + c,
            cur = !((r == 0 && c == 1) || (r == 0 && c == -1) ||
                    (r == 1 && c == 0) || (r == -1 && c == 0));
        if (0 <= p && p < n && 0 <= q && q < m && s[p][q] == '.' &&
            dis[x][y] + cur < dis[p][q]) {
          dis[p][q] = dis[x][y] + cur;
          que.push(make_pair(p, q));
        }
      }
  }
  printf("%d\\n", dis[ex][ey] == INF ? -1 : dis[ex][ey]);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int mat[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int h, w, sI, sJ, eI, eJ;
  cin >> h >> w >> sI >> sJ >> eI >> eJ;
  sI--;
  sJ--;
  eI--;
  eJ--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#')
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
    }
  }
  deque<pair<int, int>> q;
  vector<vector<int>> d(h, vector<int>(w, 1e9 + 7));
  q.push_front({sI, sJ});
  d[sI][sJ] = 0;
  int dir[] = {1, 0, -1, 0, 1};
  while (!q.empty()) {
    int cI, cJ;
    tie(cI, cJ) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nI = cI + dir[i];
      int nJ = cJ + dir[i + 1];
      if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
        continue;
      if (mat[nI][nJ])
        continue;
      if (d[nI][nJ] > d[cI][cJ]) {
        d[nI][nJ] = d[cI][cJ];
        q.push_front({nI, nJ});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nI = cI + i;
        int nJ = cJ + j;
        if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
          continue;
        if (mat[nI][nJ])
          continue;
        if (d[nI][nJ] > d[cI][cJ] + 1) {
          d[nI][nJ] = d[cI][cJ] + 1;
          q.push_back({nI, nJ});
        }
      }
    }
  }
  if (d[eI][eJ] == 1e9 + 7)
    cout << -1 << endl;
  else
    cout << d[eI][eJ] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 7;
int mat[N][N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int h, w, sI, sJ, eI, eJ;
  cin >> h >> w >> sI >> sJ >> eI >> eJ;
  sI--;
  sJ--;
  eI--;
  eJ--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#')
        mat[i][j] = 1;
      else
        mat[i][j] = 0;
    }
  }
  deque<pair<int, int>> q;
  vector<vector<int>> d(h, vector<int>(w, 1e9 + 7));
  q.push_front({sI, sJ});
  d[sI][sJ] = 0;
  int dir[] = {1, 0, -1, 0, 1};
  while (!q.empty()) {
    int cI, cJ;
    tie(cI, cJ) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nI = cI + dir[i];
      int nJ = cJ + dir[i + 1];
      if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
        continue;
      if (mat[nI][nJ])
        continue;
      if (d[nI][nJ] > d[cI][cJ]) {
        d[nI][nJ] = d[cI][cJ];
        q.push_front({nI, nJ});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nI = cI + i;
        int nJ = cJ + j;
        if (nI >= h || nI < 0 || nJ >= w || nJ < 0)
          continue;
        if (mat[nI][nJ])
          continue;
        if (d[nI][nJ] > d[cI][cJ] + 1) {
          d[nI][nJ] = d[cI][cJ] + 1;
          q.push_front({nI, nJ});
        }
      }
    }
  }
  if (d[eI][eJ] == 1e9 + 7)
    cout << -1 << endl;
  else
    cout << d[eI][eJ] << endl;

  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int INF = 1001001001;

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));

  deque<pair<int, pair<int, int>>> q;
  q.emplace_back(0, make_pair(cw, ch));
  dist[ch][cw] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    if (dist[p.second.second][p.second.first] < p.first)
      continue;
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        int x = p.second.first;
        int y = p.second.second;
        int nx = x + i;
        int ny = y + j;
        if (nx >= 0 && nx < w && ny >= 0 && ny < h && s[ny][nx] == '.') {
          int cost = 0;
          if (abs(i) + abs(j) > 1)
            cost = 1;
          if (dist[ny][nx] > dist[y][x] + cost) {
            dist[ny][nx] = dist[y][x] + cost;
            if (cost == 0)
              q.emplace_front(dist[ny][nx], make_pair(nx, ny));
            else
              q.emplace_back(dist[ny][nx], make_pair(nx, ny));
          }
        }
      }
    }
  }
  if (dist[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int INF = 1001001001;

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));

  deque<pair<int, pair<int, int>>> q;
  q.emplace_back(0, make_pair(cw, ch));
  dist[ch][cw] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    if (dist[p.second.second][p.second.first] < p.first)
      continue;
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        int x = p.second.first;
        int y = p.second.second;
        int nx = x + i;
        int ny = y + j;
        if (nx >= 0 && nx < w && ny >= 0 && ny < h && s[ny][nx] != '#') {
          int cost = 0;
          if (abs(i) + abs(j) > 1)
            cost = 1;
          if (dist[ny][nx] > dist[y][x] + cost) {
            dist[ny][nx] = dist[y][x] + cost;
            if (cost == 0)
              q.emplace_front(dist[nx][ny], make_pair(nx, ny));
            else
              q.emplace_back(dist[ny][nx], make_pair(nx, ny));
          }
        }
      }
    }
  }
  if (dist[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dist[dh][dw] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

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

const int mi[] = {1, 0, -1, 0};
const int mj[] = {0, 1, 0, -1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  for (int k = 0; k < h; k++)
    cin >> s[k];
  const int INF = 1e6;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> deq;
  dist[ch][cw] = 0;          // init
  deq.emplace_front(ch, cw); // init
  while (!deq.empty()) {
    int i0 = deq.front().first;
    int j0 = deq.front().second;
    deq.pop_front();
    int d = dist[i0][j0];
    for (int k = 0; k < 4; k++) {
      int i = i0 + mi[k], j = j0 + mj[k];
      if (i < 0 || i >= h || j < 0 || j >= w)
        continue;
      if (s[i][j] == '#')
        continue;
      if (dist[i][j] <= d)
        continue;
      dist[i][j] = d;
      deq.emplace_front(i, j);
    }
    for (int l = -2; l <= 2; l++) {
      for (int m = -2; m <= 2; m++) {
        int i = i0 + l, j = j0 + m;
        if (i < 0 || i >= h || j < 0 || j >= w)
          continue;
        if (s[i][j] == '#')
          continue;
        if (dist[i][j] <= d + 1)
          continue;
        dist[i][j] = d + 1;
        deq.emplace_back(i, j);
      }
    }
  }
  int ans = dist[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

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

const int mi[] = {1, 0, -1, 0};
const int mj[] = {0, 1, 0, -1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  const int INF = 1e6;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> deq;
  deq.emplace_front(ch, cw); // init
  dist[ch][cw] = 0;          // init
  while (!deq.empty()) {
    int i0 = deq.front().first;
    int j0 = deq.front().second;
    deq.pop_front();
    int d = dist[i0][j0];
    rep(k, 4) {
      int i = i0 + mi[k], j = j0 + mj[k];
      if (s[i][j] == '#')
        continue;
      if (i < 0 || i >= h || j < 0 || j >= w)
        continue;
      if (dist[i][i] <= d)
        continue;
      dist[i][j] = d;
      deq.emplace_front(i, j);
    }
    for (int l = -2; l <= 2; l++) {
      for (int m = -2; m <= 2; m++) {
        int i = i0 + l, j = j0 + m;
        if (s[i][j] == '#')
          continue;
        if (i < 0 || i >= h || j < 0 || j >= w)
          continue;
        if (dist[i][j] <= d + 1)
          continue;
        dist[i][j] = d + 1;
        deq.emplace_back(i, j);
      }
    }
  }
  int ans = dist[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define endl '\\n'
#define pb push_back
#define mp make_pair
#define LL long long
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int n, m;
char a[1010][1010];
int d[1010][1010];
bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; }
void dijkstra(int sx, int sy) {
  set<piii> pq;
  memset(d, INF, sizeof d);
  d[sx][sy] = 0;
  //	d[s]=0;
  pq.insert(mp(0, mp(sx, sy)));
  while (!pq.empty()) {
    pii v = pq.begin()->S;
    pq.erase(pq.begin());
    int x = v.F, y = v.S;
    for (int i = x - 2; i <= x + 2; i++)
      for (int j = y - 2; j <= y + 2; j++) {
        if (outboard(i, j) || a[i][j] == '#')
          continue;
        int tp;
        if (abs(i - x) + abs(j - y) == 1)
          tp = 0;
        else
          tp = 1;
        if (d[i][j] > d[x][y] + tp) {
          d[i][j] = d[x][y] + tp;
          pq.insert(mp(d[i][j], mp(i, j)));
        }
      }
    //		for(int i=0;i<G[v].size();i++){
    //			pii e=G[v][i];
    //			int p=e.S,to=e.F;
    //			if(d[to]>d[v]+p){
    //				d[to]=d[v]+p;
    //				pq.insert(mp(d[to],to));
    //			}
    //		}
  }
}
int main() {
  cin >> n >> m;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  //	dfs(sx,sy,0);
  dijkstra(sx, sy);
  if (d[ex][ey] == INF)
    puts("-1");
  else
    cout << d[ex][ey] << endl;
  //	for(int i=1;i<=n;i++){
  //		for(int j=1;j<=m;j++)cout<<dis[i][j]<<' ';
  //		puts("");
  //	}
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define pb push_back
#define mp make_pair
#define LL long long
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
int n, m;
char a[1010][1010];
int dis[1010][1010];
bool outboard(int x, int y) { return x < 1 || x > n || y < 1 || y > m; }
void dfs(int x, int y, int step) {
  //	cout<<x<<' '<<y<<' '<<step<<endl;
  if (dis[x][y] <= step)
    return;
  dis[x][y] = step;
  for (int i = x - 2; i <= x + 2; i++)
    for (int j = y - 2; j <= y + 2; j++) {
      if (outboard(i, j) || a[i][j] == '#')
        continue;
      if (abs(i - x) + abs(j - y) == 1)
        dfs(i, j, step);
      else
        dfs(i, j, step + 1);
    }
}
int main() {
  memset(dis, INF, sizeof dis);
  cin >> n >> m;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  dfs(sx, sy, 0);
  if (dis[ex][ey] == INF)
    puts("-1");
  else
    cout << dis[ex][ey] << endl;
  //	for(int i=1;i<=n;i++){
  //		for(int j=1;j<=m;j++)cout<<dis[i][j]<<' ';
  //		puts("");
  //	}
  return 0;
}