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

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

ll N[1001][1001];
int H, W, Ch, Cw, Dh, Dw;
char S[1001][1001];
deque<pair<int, int>> q;

void visit(int h, int w, int n, bool front) {
  if (h <= 0 || h > H || w <= 0 || w > W) {
    return;
  }
  if (S[h][w] == '#') {
    return;
  }

  if (N[h][w] <= n) {
    return;
  }
  N[h][w] = n;

  // printf("  %d, %d: %ld\\n", h, w, n);

  if (h == Dh && w == Dw) {
    return;
  }

  if (front) {
    q.push_front(pair<int, int>(h, w));
  }
  q.push_back(pair<int, int>(h, w));
}

int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;

  for (int h = 1; h <= H; h++) {
    for (int w = 1; w <= W; w++) {
      char s;
      cin >> s;
      S[h][w] = s;
      N[h][w] = 99999999999999;
    }
  }

  N[Ch][Cw] = 0;

  q.push_back(pair<int, int>(Ch, Cw));
  while (q.size() > 0) {
    pair<int, int> nxt = q.front();
    q.pop_front();
    int h = nxt.first;
    int w = nxt.second;
    int n = N[h][w];

    // printf("at %d, %d\\n", h, w);

    if (h == Dh && w == Dw) {
      continue;
    }

    visit(h - 1, w, n, true);
    visit(h + 1, w, n, true);
    visit(h, w - 1, n, true);
    visit(h, w + 1, n, true);

    for (int nh = h - 2; nh <= h + 2; nh++) {
      for (int nw = w - 2; nw <= w + 2; nw++) {
        visit(nh, nw, n + 1, false);
      }
    }

    // cout << "-----" << endl;
  }

  if (N[Dh][Dw] == 99999999999999) {
    cout << -1 << endl;
  } else {
    cout << N[Dh][Dw] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

ll N[1001][1001];
int H, W, Ch, Cw, Dh, Dw;
char S[1001][1001];
queue<pair<int, int>> q;

void visit(int h, int w, int n) {
  if (h <= 0 || h > H || w <= 0 || w > W) {
    return;
  }
  if (S[h][w] == '#') {
    return;
  }

  if (N[h][w] <= n) {
    return;
  }
  N[h][w] = n;

  // printf("  %d, %d: %ld\\n", h, w, n);

  if (h == Dh && w == Dw) {
    return;
  }

  q.push(pair<int, int>(h, w));
}

int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;

  for (int h = 1; h <= H; h++) {
    for (int w = 1; w <= W; w++) {
      char s;
      cin >> s;
      S[h][w] = s;
      N[h][w] = 99999999999999;
    }
  }

  N[Ch][Cw] = 0;

  q.push(pair<int, int>(Ch, Cw));
  while (q.size() > 0) {
    pair<int, int> nxt = q.front();
    q.pop();
    int h = nxt.first;
    int w = nxt.second;
    int n = N[h][w];

    // printf("at %d, %d\\n", h, w);

    if (h == Dh && w == Dw) {
      continue;
    }

    visit(h - 1, w, n);
    visit(h + 1, w, n);
    visit(h, w - 1, n);
    visit(h, w + 1, n);

    for (int nh = h - 2; nh <= h + 2; nh++) {
      for (int nw = w - 2; nw <= w + 2; nw++) {
        visit(nh, nw, n + 1);
      }
    }

    // cout << "-----" << endl;
  }

  if (N[Dh][Dw] == 99999999999999) {
    cout << -1 << endl;
  } else {
    cout << N[Dh][Dw] << endl;
  }
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main() {
  s64 H, W;
  cin >> H >> W;

  u64 Ch, Cw;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;

  u64 Dh, Dw;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;

  vector<vector<bool>> HW(H, vector<bool>(W));

  for (s64 i = 0; i < H; i++) {
    for (s64 j = 0; j < W; j++) {
      char c;
      cin >> c;
      HW[i][j] = (c == '.');
    }
  }

  vector<vector<u64>> M(H, vector<u64>(W, UINT64_MAX));
  M[Ch][Cw] = 0;

  deque<pair<u64, u64>> F;
  F.push_back(make_pair(Ch, Cw));

  while (!F.empty()) {
    s64 Fh = F.front().first;
    s64 Fw = F.front().second;
    F.pop_front();

    for (s64 i = -2; i < 3; i++) {
      for (s64 j = -2; j < 3; j++) {
        s64 h = Fh + i;
        s64 w = Fw + j;

        if (h < 0 || w < 0)
          continue;
        if (h >= H || w >= W)
          break;
        if (!HW[h][w])
          continue;

        if (abs(i) + abs(j) > 1) { // warp
          if (M[h][w] > M[Fh][Fw] + 1) {
            M[h][w] = M[Fh][Fw] + 1;
            F.push_back(make_pair(h, w));
          }
        } else { // walk
          if (M[h][w] > M[Fh][Fw]) {
            M[h][w] = M[Fh][Fw];
            F.push_front(make_pair(h, w));
          }
        }
      }
    }
  }

  if (M[Dh][Dw] == UINT64_MAX) {
    cout << -1 << endl;
  } else {
    cout << M[Dh][Dw] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main() {
  s64 H, W;
  cin >> H >> W;

  u64 Ch, Cw;
  cin >> Ch >> Cw;
  Ch--;
  Cw--;

  u64 Dh, Dw;
  cin >> Dh >> Dw;
  Dh--;
  Dw--;

  vector<vector<bool>> HW(H, vector<bool>(W));

  for (s64 i = 0; i < H; i++) {
    for (s64 j = 0; j < W; j++) {
      char c;
      cin >> c;
      HW[i][j] = (c == '.');
    }
  }

  vector<vector<u64>> M(H, vector<u64>(W, UINT64_MAX));
  M[Ch][Cw] = 0;

  deque<pair<u64, u64>> F;
  F.push_back(make_pair(Ch, Cw));

  while (!F.empty()) {
    s64 Fh = F.front().first;
    s64 Fw = F.front().second;
    F.pop_front();

    for (s64 i = -2; i < 3; i++) {
      for (s64 j = -2; j < 3; j++) {
        s64 h = Fh - i;
        s64 w = Fw - j;

        if (h < 0 || w < 0)
          continue;
        if (h >= H || w >= W)
          break;
        if (!HW[h][w])
          continue;

        if (abs(i) + abs(j) > 1) { // warp
          if (M[h][w] > M[Fh][Fw] + 1) {
            M[h][w] = M[Fh][Fw] + 1;
            F.push_back(make_pair(h, w));
          }
        } else { // walk
          if (M[h][w] > M[Fh][Fw]) {
            M[h][w] = M[Fh][Fw];
            F.push_front(make_pair(h, w));
          }
        }
      }
    }
  }

  if (M[Dh - 1][Dw - 1] == UINT64_MAX) {
    cout << -1 << endl;
  } else {
    cout << M[Dh][Dw] << endl;
  }

  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main() {
  u64 H, W;
  cin >> H >> W;

  u64 Ch, Cw;
  cin >> Ch >> Cw;

  u64 Dh, Dw;
  cin >> Dh >> Dw;

  vector<vector<bool>> HW(H, vector<bool>(W));

  for (u64 i = 0; i < H; i++) {
    for (u64 j = 0; j < W; j++) {
      char c;
      cin >> c;
      HW[i][j] = (c == '.');
    }
  }

  vector<vector<u64>> M(H, vector<u64>(W, UINT64_MAX));
  M[Ch - 1][Cw - 1] = 0;

  deque<pair<u64, u64>> F;
  F.push_back(make_pair(Ch - 1, Cw - 1));

  while (!F.empty()) {
    s64 Fh = F.front().first;
    s64 Fw = F.front().second;
    F.pop_front();

    for (s64 h = max(0l, Fh - 2); h < min((s64)H, Fh + 3); h++) {
      for (s64 w = max(0l, Fw - 2); w < min((s64)W, Fw + 3); w++) {
        if (!HW[h][w])
          continue;

        if (abs(h - Fh) + abs(w - Fw) > 1) { // warp
          if (M[h][w] > M[Fh][Fw] + 1) {
            M[h][w] = M[Fh][Fw] + 1;
            F.push_back(make_pair(h, w));
          }
        } else { // walk
          if (M[h][w] > M[Fh][Fw]) {
            M[h][w] = M[Fh][Fw];
            F.push_front(make_pair(h, w));
          }
        }
      }
    }
  }

  if (M[Dh - 1][Dw - 1] == UINT64_MAX) {
    cout << -1 << endl;
  } else {
    cout << M[Dh - 1][Dw - 1] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main() {
  u64 H, W;
  cin >> H >> W;

  u64 Ch, Cw;
  cin >> Ch >> Cw;

  u64 Dh, Dw;
  cin >> Dh >> Dw;

  vector<vector<bool>> HW(H, vector<bool>(W));

  for (u64 i = 0; i < H; i++) {
    for (u64 j = 0; j < W; j++) {
      char c;
      cin >> c;
      HW[i][j] = (c == '.');
    }
  }

  vector<vector<u64>> M(H, vector<u64>(W, UINT64_MAX));
  M[Ch - 1][Cw - 1] = 0;

  deque<pair<u64, u64>> F;
  F.push_back(make_pair(Ch - 1, Cw - 1));

  while (!F.empty()) {
    s64 Fh = F.front().first;
    s64 Fw = F.front().second;
    F.pop_front();

    for (s64 h = max(0l, Fh - 2); h < min((s64)H, Fh + 3); h++) {
      for (s64 w = max(0l, Fw - 2); w < min((s64)W, Fw + 3); w++) {
        if (!HW[h][w])
          continue;

        if (abs(h - Fh) + abs(w - Fw) > 1) { // warp
          if (M[h][w] > M[Fh][Fw] + 1) {
            M[h][w] = M[Fh][Fw] + 1;
            F.push_back(make_pair(h, w));
          }
        } else { // walk
          if (M[h][w] > M[Fh][Fw]) {
            M[h][w] = M[Fh][Fw];
            F.push_back(make_pair(h, w));
          }
        }
      }
    }
  }

  if (M[Dh - 1][Dw - 1] == UINT64_MAX) {
    cout << -1 << endl;
  } else {
    cout << M[Dh - 1][Dw - 1] << endl;
  }

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

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

int main() {
  int INF = 1000000000;
  int h, w;
  int sy, sx, gy, gx;
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vvi dis(h, vi(w, INF));
  dis[sy][sx] = 0;
  deque<pair<int, int>> que;
  que.push_front({sy, sx});
  vector<int> v = {1, 0, -1, 0, 1};
  while (!que.empty()) {
    int cy = que.front().first;
    int cx = que.front().second;
    que.pop_front();
    rep(i, 4) {
      rep(j, 4) {
        int ny = cy + v[i];
        int nx = cx + v[i + 1];
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (dis[cy][cx] < dis[ny][nx]) {
          dis[ny][nx] = dis[cy][cx];
          que.push_front({ny, nx});
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = cy + i;
        int nx = cx + j;
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (dis[cy][cx] + 1 < dis[ny][nx]) {
          dis[ny][nx] = dis[cy][cx] + 1;
          que.push_back({ny, nx});
        }
      }
    }
  }
  if (dis[gy][gx] == INF)
    dis[gy][gx] = -1;
  cout << dis[gy][gx] << endl;
}


Error: -11

Fixed submission:

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

int main() {
  int w, h;
  int sx, sy, gx, gy;
  cin >> h >> w >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> g(h);
  rep(i, h) cin >> g[i];
  vvi dp(h, vi(w, -1));
  dp[sx][sy] = 0;
  int x = sx;
  int y = sy;
  queue<pair<int, int>> que;
  que.push(make_pair(x, y));
  while (dp[gx][gy] == -1) {
    x = que.front().first;
    y = que.front().second;
    cout << x << " " << y << endl;
    ;
    que.pop();
    if (x != h && g[x + 1][y] == '.' && dp[x + 1][y] < dp[x][y]) {
      dp[x + 1][y] = dp[x][y];
      que.push(make_pair(x + 1, y));
    }
    if (y != w && g[x][y + 1] == '.' && dp[x][y + 1] < dp[x][y]) {
      dp[x][y + 1] = dp[x][y];
      que.push(make_pair(x, y + 1));
    }
    if (x != 0 && g[x - 1][y] == '.' && dp[x - 1][y] < dp[x][y]) {
      dp[x - 1][y] = dp[x][y];
      que.push(make_pair(x - 1, y));
    }
    if (y != 0 && g[x][y - 1] == '.' && dp[x][y - 1] < dp[x][y]) {
      dp[x][y - 1] = dp[x][y];
      que.push(make_pair(x, y - 1));
    }
  }
  cout << dp[gx][sy] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, 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>> d(h, vector<int>(w, -1));
  deque<int> deq;
  deq.push_back(ch);
  deq.push_back(cw);
  d[ch][cw] = 0;
  int curr_h, curr_w;
  while (deq.size() > 0) {
    curr_h = deq[0];
    curr_w = deq[1];
    deq.pop_front();
    deq.pop_front();
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if (curr_h - 2 + i >= 0 && curr_h - 2 + i < h && curr_w - 2 + j >= 0 &&
            curr_w - 2 + j < w && s[curr_h - 2 + i][curr_w - 2 + j] == '.') {
          if ((i == 2 && abs(j - 2) == 1) || (j == 2 && abs(i - 2) == 1)) {
            if (d[curr_h - 2 + i][curr_w - 2 + j] > d[curr_h][curr_w] ||
                d[curr_h - 2 + i][curr_w - 2 + j] == -1) {
              d[curr_h - 2 + i][curr_w - 2 + j] = d[curr_h][curr_w];
              deq.push_front(curr_w - 2 + j);
              deq.push_front(curr_h - 2 + i);
            }
          } else {
            if (d[curr_h - 2 + i][curr_w - 2 + j] > d[curr_h][curr_w] + 1 ||
                d[curr_h - 2 + i][curr_w - 2 + j] == -1) {
              d[curr_h - 2 + i][curr_w - 2 + j] = d[curr_h][curr_w] + 1;
              deq.push_back(curr_h - 2 + i);
              deq.push_back(curr_w - 2 + j);
            }
          }
        }
      }
    }
  }
  cout << d[dh][dw] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, 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>> d(h, vector<int>(w, -1));
  deque<int> deq;
  deq.push_back(ch);
  deq.push_back(cw);
  d[ch][cw] = 0;
  int curr_h, curr_w;
  while (deq.size() > 0) {
    curr_h = deq[0];
    curr_w = deq[1];
    deq.pop_front();
    deq.pop_front();
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if (curr_h - 2 + i >= 0 && curr_h - 2 + i < h && curr_w - 2 + j >= 0 &&
            curr_w - 2 + j < w && s[curr_h - 2 + i][curr_w - 2 + j] == '.') {
          if ((i == 2 && abs(j - 2) == 1) || (j == 2 && abs(i - 2) == 1)) {
            if (d[curr_h - 2 + i][curr_w - 2 + j] <= d[curr_h][curr_w] ||
                d[curr_h - 2 + i][curr_w - 2 + j] == -1) {
              d[curr_h - 2 + i][curr_w - 2 + j] = d[curr_h][curr_w];
              deq.push_front(curr_w - 2 + j);
              deq.push_front(curr_h - 2 + i);
            }
          } else {
            if (d[curr_h - 2 + i][curr_w - 2 + j] <= d[curr_h][curr_w] + 1 ||
                d[curr_h - 2 + i][curr_w - 2 + j] == -1) {
              d[curr_h - 2 + i][curr_w - 2 + j] = d[curr_h][curr_w] + 1;
              deq.push_back(curr_h - 2 + i);
              deq.push_back(curr_w - 2 + j);
            }
          }
        }
      }
    }
  }
  cout << d[dh][dw] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int SIZE_LIMIT = 1e3 + 5;
char maze[SIZE_LIMIT][SIZE_LIMIT];

int dp[SIZE_LIMIT][SIZE_LIMIT];

int main() {
  memset(maze, 0, sizeof(maze));
  memset(dp, 0x3f, sizeof(dp));

  int h = 0, w = 0, ch, cw, dh, dw;
  scanf("%d%d%d%d%d%d", &h, &w, &ch, &cw, &dh, &dw);

  for (int i = 1; i <= h; ++i) {
    scanf("%s", maze[i] + 1);
  }

  deque<pair<int, int>> movable;
  movable.push_back({ch, cw});
  dp[ch][cw] = 0;

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

  while (!movable.empty()) {
    auto p = movable.front();
    movable.pop_front();

    if (p.first == dh && p.second == dw) {
      break;
    }

    // move
    for (int i = 0; i < 4; ++i) {
      int nx = dx[i] + p.first, ny = dy[i] + p.second;
      if (maze[nx][ny] == '.' && dp[nx][ny] > dp[p.first][p.second]) {
        dp[nx][ny] = dp[p.first][p.second];
        movable.push_front({nx, ny});
      }
    }
    // jump
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = p.first + i, ny = p.second + j;
        if (nx > 0 && nx <= h && ny > 0 && ny <= w && maze[nx][ny] == '.' &&
            dp[nx][ny] > dp[p.first][p.second] + 1) {
          dp[nx][ny] = dp[p.first][p.second] + 1;
          movable.push_back({nx, ny});
        }
      }
    }
  }

  if (dp[dh][dw] == 0x3f3f3f3f)
    printf("-1\\n");
  else
    printf("%d\\n", dp[dh][dw]);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int SIZE_LIMIT = 1e3 + 5;
char maze[SIZE_LIMIT][SIZE_LIMIT];

int dp[SIZE_LIMIT][SIZE_LIMIT];

int main() {
  memset(maze, 0, sizeof(maze));
  memset(dp, 0x3f, sizeof(dp));

  int h = 0, w = 0, ch, cw, dh, dw;
  scanf("%d%d%d%d%d%d", &h, &w, &ch, &cw, &dh, &dw);

  for (int i = 1; i <= h; ++i) {
    scanf("%s", maze[i] + 1);
  }

  deque<pair<int, int>> movable;
  movable.push_back({ch, cw});
  dp[ch][cw] = 0;

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

  while (!movable.empty()) {
    auto p = movable.front();
    movable.pop_front();

    // move
    for (int i = 0; i < 4; ++i) {
      int nx = dx[i] + p.first, ny = dy[i] + p.second;
      if (maze[nx][ny] == '.' && dp[nx][ny] > dp[p.first][p.second]) {
        dp[nx][ny] = dp[p.first][p.second];
        movable.push_back({nx, ny});
      }
    }
    // jump
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = p.first + i, ny = p.second + j;
        if (nx > 0 && nx <= h && ny > 0 && ny <= w && maze[nx][ny] == '.' &&
            dp[nx][ny] > dp[p.first][p.second] + 1) {
          dp[nx][ny] = dp[p.first][p.second] + 1;
          movable.push_back({nx, ny});
        }
      }
    }
  }

  if (dp[dh][dw] == 0x3f3f3f3f)
    printf("-1\\n");
  else
    printf("%d\\n", dp[dh][dw]);

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

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

int H, W;

bool outOfBound(int i, int j) { return i < 0 || i >= H || j < 0 || j >= W; }

int main() {
  int CH, CW, DH, DW;
  cin >> H >> W >> CH >> CW >> DH >> DW;
  CH--, CW--, DH--, DW--;

  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  int cost[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cost[i][j] = 1e7;
    }
  }

  map<pair<int, int>, bool> MAP_MOVE_A;
  MAP_MOVE_A[{-1, 0}] = true;
  MAP_MOVE_A[{0, -1}] = true;
  MAP_MOVE_A[{1, 0}] = true;
  MAP_MOVE_A[{0, 1}] = true;

  deque<pair<int, int>> dq;
  cost[CH][CW] = 0;
  dq.push_back({CH, CW});
  while (!dq.empty()) {
    pair<int, int> cur = dq.front();
    dq.pop_front();
    if (cur.first == DH && cur.second == DW) {
      break;
    }

    for (int di = -2; di <= 2; di++) {
      for (int dj = -2; dj <= 2; dj++) {
        int i = cur.first + di;
        int j = cur.second + dj;

        if (outOfBound(i, j) || S[i][j] == '#' || (di == 0 && dj == 0))
          continue;

        int magic = 1;
        if (MAP_MOVE_A.find({di, dj}) != MAP_MOVE_A.end()) {
          magic = 0;
        }

        if (cost[cur.first][cur.second] + magic < cost[i][j]) {
          cost[i][j] = cost[cur.first][cur.second] + magic;
          if (magic == 0) {
            dq.push_front({i, j});
          } else {
            dq.push_back({i, j});
          }
        }
      }
    }
  }

  if (cost[DH][DW] == 1e7) {
    cout << -1 << endl;
  } else {
    cout << cost[DH][DW] << endl;
  }
}

Error: WA

Fixed submission:

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

int H, W;

bool outOfBound(int i, int j) { return i < 0 || i >= H || j < 0 || j >= W; }

int main() {
  int CH, CW, DH, DW;
  cin >> H >> W >> CH >> CW >> DH >> DW;
  CH--, CW--, DH--, DW--;

  vector<string> S(W);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  int cost[H][W];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cost[i][j] = 1e7;
    }
  }

  map<pair<int, int>, bool> MAP_MOVE_A;
  MAP_MOVE_A[{-1, 0}] = true;
  MAP_MOVE_A[{0, -1}] = true;
  MAP_MOVE_A[{1, 0}] = true;
  MAP_MOVE_A[{0, 1}] = true;

  deque<pair<int, int>> dq;
  cost[CH][CW] = 0;
  dq.push_back({CH, CW});
  while (!dq.empty()) {
    pair<int, int> cur = dq.front();
    dq.pop_front();
    if (cur.first == DH && cur.second == DW) {
      break;
    }

    for (int di = -2; di <= 2; di++) {
      for (int dj = -2; dj <= 2; dj++) {
        int magic = 1;
        int i = cur.first + di;
        int j = cur.second + dj;

        if (outOfBound(i, j) || S[i][j] == '#' || (di == 0 && dj == 0))
          continue;

        if (MAP_MOVE_A.find({di, dj}) != MAP_MOVE_A.end()) {
          magic = 0;
        }

        if (cost[cur.first][cur.second] + magic < cost[i][j]) {
          cost[i][j] = cost[cur.first][cur.second] + magic;
          if (magic == 0) {
            dq.push_front({i, j});
          } else {
            dq.push_back({i, j});
          }
        }
      }
    }
  }

  if (cost[DH][DW] == 1e7) {
    cout << -1;
  } else {
    cout << cost[DH][DW];
  }
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long
#define forn(i, a, n) for (int i = a; i < n; i++)

int n, m;
int stx, sty, edx, edy;
const int INF = 1e9;
char c[1001][1001];
int d[1001][1001];
char vis[1001][1001];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
deque<int> q;

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

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  cin >> n >> m >> stx >> sty >> edx >> edy;
  stx--, sty--, edx--, edy--;
  forn(i, 0, n) forn(j, 0, m) cin >> c[i][j];
  forn(i, 0, n) forn(j, 0, m) d[i][j] = INF;
  q.push_back(stx);
  q.push_back(sty);
  d[stx][sty] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop_front();
    int y = q.front();
    q.pop_front();
    if (vis[x][y])
      continue;
    vis[x][y] = 1;
    forn(i, 0, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || c[nx][ny] == '#')
        continue;
      if (d[nx][ny] > d[x][y]) {
        q.push_front(ny);
        q.push_front(nx);
        d[nx][ny] = d[x][y];
      }
    }
    forn(i, x - 2, x + 3) {
      forn(j, y - 2, y + 3) {
        if (i < 0 || i >= n || j < 0 || j >= m || c[i][j] == '#')
          continue;
        if (d[i][j] > d[x][y] + 1) {
          q.push_back(i);
          q.push_back(j);
          d[i][j] = d[x][y] + 1;
        }
      }
    }
  }
  cout << (d[edx][edy] == INF ? -1 : d[edx][edy]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define forn(i, a, n) for (int i = a; i < n; i++)

char c[1001][1001];
int d[1001][1001];

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

int main() {

  int n, m, s1, s2, e1, e2;
  scanf("%d%d%d%d%d%d", &n, &m, &s1, &s2, &e1, &e2);
  s1--, s2--, e1--, e2--;
  forn(i, 0, n) scanf("%s", c[i]);

  queue<int> q;
  memset(d, -1, sizeof(d));
  q.push(e1);
  q.push(e2);
  d[e1][e2] = 0;

  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    queue<int> que;
    que.push(x);
    que.push(y);
    while (!que.empty()) {
      int x2 = que.front();
      que.pop();
      int y2 = que.front();
      que.pop();
      forn(i, 0, 4) {
        int nx2 = dx[i] + x;
        int ny2 = dy[i] + y;
        if (nx2 < 0 || nx2 >= n || ny2 < 0 || ny2 >= m || c[nx2][ny2] == '#')
          continue;
        if (d[nx2][ny2] == -1 || d[nx2][ny2] > d[x][y]) {
          d[nx2][ny2] = d[x][y];
          que.push(nx2);
          que.push(ny2);
          q.push(nx2);
          q.push(ny2);
        }
      }
    }
    forn(i, x - 2, x + 3) {
      forn(j, y - 2, y + 3) {
        if (i < 0 || i >= n || j < 0 || j >= m || (i == x && j == y) ||
            c[i][j] == '#')
          continue;
        if (d[i][j] == -1 || d[i][j] > d[x][y] + 1) {
          d[i][j] = d[x][y] + 1;
          q.push(i);
          q.push(j);
        }
      }
    }
  }
  printf("%d\\n", d[s1][s2]);
  return 0;
}