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

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

using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 100100100;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> ss(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> ss[i][j];

  deque<P> deq;
  deq.push_front(P(ch, cw));
  vector<vector<int>> magics(h, vector<int>(w, INF));
  vector<P> walks = {P(1, 0), P(0, 1), P(-1, 0), P(0, -1)};
  magics[ch][cw] = 0;
  while (!deq.empty()) {
    int i = deq.front().first;
    int j = deq.front().second;
    int mag = magics[i][j];
    deq.pop_front();
    for (auto p : walks) {
      int a = i + p.first;
      int b = j + p.second;
      if (0 <= a && a < h && 0 <= b && b < w) {
        if (ss[a][b] == '.' && mag < magics[a][b]) {
          magics[a][b] = mag;
          deq.push_front(P(a, b));
        }
      }
    }
    for (int a = -2; a <= 2; a++) {
      for (int b = -2; b <= 2; b++) {
        if (0 <= a + i && a + i < h && 0 <= b + j && b + j < w) {
          if (ss[a + i][b + j] == '.' && mag + 1 < magics[a + i][b + j]) {
            magics[a + i][b + j] = mag + 1;
            deq.push_back(P(a + i, b + j));
          }
        }
      }
    }
  }
  if (magics[dh][dw] != INF) {
    cout << magics[dh][dw] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 100100100;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> ss(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> ss[i][j];

  vector<vector<int>> warp(h, vector<int>(w, INF));
  queue<P> que;
  int ans = 0;
  que.push(P(dh, dw));
  warp[dh][dw] = 0;
  vector<P> walks = {P(1, 0), P(0, 1), P(-1, 0), P(0, -1)};
  while (!que.empty()) {
    int i = que.front().first;
    int j = que.front().second;
    que.pop();
    bool isWalk = false;
    for (auto p : walks) {
      int a = i + p.first;
      int b = j + p.second;
      if (0 <= a && a < h && 0 <= b && b < w) {
        if (ss[a][b] != '#') {
          if (warp[i][j] < warp[a][b]) {
            isWalk = true;
            warp[a][b] = min(warp[a][b], warp[i][j]);
            que.push(P(a, b));
          }
        }
      }
    }
    // warp
    if (!isWalk) {
      rep(s, 5) rep(t, 5) {
        int a = i + s - 2;
        int b = j + t - 2;
        if (0 <= a && a < h && 0 <= b && b < w) {
          if (ss[a][b] != '#') {
            if (warp[i][j] < warp[a][b]) {
              que.push(P(a, b));
              warp[a][b] = warp[i][j] + 1;
            }
          }
        }
      }
    }
  }

  if (warp[ch][cw] != INF) {
    cout << warp[ch][cw] << endl;
  } else {
    cout << -1 << endl;
  }

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

#include <cstdio>
#include <cstring>
#include <vector>
#define fo(x, a, b) for (int x = (a), _e = (b); x <= _e; x++)
#define fd(x, a, b) for (int x = (a), _e = (b); x >= _e; x--)
using namespace std;
typedef long long ll;
const int N = 1005;
int d[N * N], z[N][N], q[N * N], n, m, sx, sy, tx, ty, cnt = 0;
char c[N][N];
vector<int> v[N * N];
void dfs(int x, int y, int col) {
  z[x][y] = col;
  fo(i, -1, 1) fo(j, -1, 1) if (i * j == 0 && (i != 0 || j != 0)) {
    int dx = x + i, dy = y + j;
    if (dx < 1 || dx > n || dy < 1 || dy > m || c[dx][dy] == '#')
      continue;
    if (!z[dx][dy])
      dfs(dx, dy, col);
  }
}
void flood(int x, int y, int col) {
  int dx, dy;
  fo(i, -2, 2) fo(j, -2, 2) {
    dx = x + i, dy = y + j;
    if (dx < 1 || dx > n || dy < 1 || dy > m || c[dx][dy] == '#')
      continue;
    if (z[dx][dy] != col)
      v[col].push_back(z[dx][dy]);
  }
}
int main() {
  //	freopen("1.in","r",stdin);
  //	freopen("1.out","w",stdout);
  int l = 0, r = 1, x;
  scanf("%d %d", &n, &m);
  scanf("%d %d %d %d", &sx, &sy, &tx, &ty);
  fo(i, 1, n) scanf("%s", c[i] + 1);

  fo(i, 1, n) fo(j, 1, m) if (c[i][j] == '.' && !z[i][j]) dfs(i, j, ++cnt);
  /*	fo(i,1,n) {
                  fo(j,1,m) printf("%d ",z[i][j]);puts("");
          }
  */
  fo(i, 1, n) fo(j, 1, m) if (c[i][j] == '.') flood(i, j, z[i][j]);

  memset(d, 0x3f, sizeof(d));
  d[z[sx][sy]] = 0;
  q[1] = z[sx][sy];
  while (l < r) {
    x = q[++l];
    for (int go : v[x])
      if (d[go] > d[x] + 1) {
        d[go] = d[x] + 1;
        q[++r] = go;
      }
  }
  printf("%d", d[z[tx][ty]] > cnt ? -1 : d[z[tx][ty]]);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#define fo(x, a, b) for (int x = (a), _e = (b); x <= _e; x++)
#define fd(x, a, b) for (int x = (a), _e = (b); x >= _e; x--)
typedef long long ll;
const int N = 1005;
int d[N][N], q[N * N * 50], n, m, sx, sy, tx, ty;
bool bz[N][N];
char c[N][N];
int main() {
  //	freopen("1.in","r",stdin);
  //	freopen("1.out","w",stdout);
  int l = 0, r = 1, x, y, dx, dy, ct;
  scanf("%d %d", &n, &m);
  scanf("%d %d %d %d", &sx, &sy, &tx, &ty);
  fo(i, 1, n) scanf("%s", c[i] + 1);

  memset(d, 20, sizeof(d));
  d[sx][sy] = 0;
  q[1] = sx * 10000 + sy;
  while (l < r) {
    x = q[++l] / 10000, y = q[l] % 10000;
    fo(i, -2, 2) fo(j, -2, 2) {
      dx = x + i, dy = y + j;
      if (dx > 0 && dx <= n && dy > 0 && dy <= m && c[dx][dy] == '.') {
        ct = (-2 < i && i < 2 && -2 < j && j < 2 && i * j == 0) ? 0 : 1;
        if (d[dx][dy] > d[x][y] + ct) {
          d[dx][dy] = d[x][y] + ct;
          if (!bz[dx][dy])
            q[++r] = dx * 10000 + dy, bz[dx][dy] = true;
        }
      }
    }
    bz[x][y] = false;
  }
  printf("%d", d[tx][ty] > n * m ? -1 : d[tx][ty]);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int h, w, sy, sx, gy, gx;
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int dis[h][w];
  rep(i, h) rep(j, w) dis[i][j] = 1e9;
  dis[sy][sx] = 0;
  deque<pair<int, int>> q;
  q.push_back({sy, sx});
  int dy[] = {0, 0, -1, 1}, dx[] = {-1, 1, 0, 0};
  while (q.size()) {
    auto a = q.front();
    q.pop_front();
    int y = a.first, x = a.second;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
          dis[y][x] < dis[ny][nx]) {
        dis[ny][nx] = dis[y][x];
        q.push_front({ny, nx});
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int ny = y + i, nx = x + j;
        if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
            dis[y][x] + 1 < dis[ny][nx]) {
          dis[ny][nx] = dis[y][x] + 1;
          q.push_back({ny, nx});
        }
      }
  }
  if (dis[gy][gx] == 1e9)
    cout << -1 << endl;
  else
    cout << dis[gy][gx] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int h, w, sy, sx, gy, gx;
  cin >> h >> w >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  string s[h];
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int dis[h][w];
  rep(i, h) rep(j, w) dis[i][j] = 1e9;
  dis[sy][sx] = 0;
  deque<pair<int, int>> q;
  q.push_back({sy, sx});
  int dy[] = {0, 0, -1, 1}, dx[] = {-1, 1, 0, 0};
  while (q.size()) {
    auto a = q.back();
    q.pop_back();
    int y = a.first, x = a.second;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
          dis[y][x] < dis[ny][nx]) {
        dis[ny][nx] = dis[y][x];
        q.push_front({ny, nx});
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int ny = y + i, nx = x + j;
        if (0 <= ny && ny < h && 0 <= nx && nx < w && s[ny][nx] == '.' &&
            dis[y][x] + 1 < dis[ny][nx]) {
          dis[ny][nx] = dis[y][x] + 1;
          q.push_back({ny, nx});
        }
      }
  }
  if (dis[gy][gx] == 1e9)
    cout << -1 << endl;
  else
    cout << dis[gy][gx] << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};
  int n, m, si, sj, di, dj;
  const int INF = 1e9;
  cin >> n >> m;
  cin >> si >> sj;
  cin >> di >> dj;
  --si, --sj, --di, --dj;
  vector<string> grid(n);
  vector<vector<int>> moves(n, vector<int>(m, INF));
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  deque<pair<int, int>> q;
  q.emplace_back(si, sj);
  moves[si][sj] = 0;
  while (!q.empty()) {
    int f = q.front().first;
    int s = q.front().second;
    int d = moves[f][s];
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int ni = dx[i] + f;
      int nj = dy[i] + s;
      if (ni < 0 || ni >= n || nj < 0 || nj >= m)
        continue;
      if (grid[ni][nj] == '#')
        continue;
      if (moves[ni][nj] <= d)
        continue;
      moves[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ai = -2; ai <= 2; ai++) {
      for (int aj = -2; aj <= 2; aj++) {
        int bi = f + ai;
        int bj = s + aj;
        if (bi < 0 || bi >= n || bj < 0 || bj >= m)
          continue;
        if (grid[bi][bj] == '#')
          continue;
        if (moves[bi][bj] <= d)
          continue;
        moves[bi][bj] = d + 1;
        q.emplace_back(bi, bj);
      }
    }
  }
  int ans = moves[di][dj];
  if (ans == INF)
    ans = -1;
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, -1, 0, 1};
  int n, m, si, sj, di, dj;
  const int INF = 1e9;
  cin >> n >> m;
  cin >> si >> sj;
  cin >> di >> dj;
  --si, --sj, --di, --dj;
  vector<string> grid(n);
  vector<vector<int>> moves(n, vector<int>(m, INF));
  for (int i = 0; i < n; i++)
    cin >> grid[i];
  deque<pair<int, int>> q;
  q.push_back({si, sj});
  moves[si][sj] = 0;
  while (!q.empty()) {
    int f = q.front().first;
    int s = q.front().second;
    int d = moves[f][s];
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int ni = dx[i] + f;
      int nj = dy[i] + s;
      if (ni < 0 || ni >= n || nj < 0 || nj >= m)
        continue;
      if (grid[ni][nj] == '#')
        continue;
      if (moves[ni][nj] <= d)
        continue;
      moves[ni][nj] = d;
      q.push_back({ni, nj});
    }
    for (int ai = -2; ai <= 2; ai++) {
      for (int aj = -2; aj <= 2; aj++) {
        int bi = f + ai;
        int bj = s + aj;
        if (bi < 0 || bi >= n || bj < 0 || bj >= m)
          continue;
        if (grid[bi][bj] == '#')
          continue;
        if (moves[bi][bj] <= d)
          continue;
        moves[bi][bj] = d + 1;
        q.push_back({bi, bj});
      }
    }
  }
  int ans = moves[di][dj];
  if (ans == INF)
    ans = -1;
  cout << ans << "\\n";
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;

int h, w;
int sx, sy, ex, ey;
int d[N][N];
string s[N];

int check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
    return 1;
  return 0;
}
void bfs() {
  deque<pair<int, int>> q;
  q.push_back({sx, sy});
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) + abs(j) == 1) {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y]) {
            d[tx][ty] = d[x][y];
            q.push_front({tx, ty});
          }
        } else {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y] + 1) {
            d[tx][ty] = d[x][y] + 1;
            q.push_back({tx, ty});
          }
        }
      }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  sx--;
  sy--;
  ex--;
  ey--;
  memset(d, 0x3f, sizeof(d));
  bfs();
  if (d[ex][ey] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << d[ex][ey] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;

int h, w;
int sx, sy, ex, ey;
int d[N][N];
string s[N];

int check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
    return 1;
  return 0;
}
int bfs() {
  deque<pair<int, int>> q;
  q.push_back({sx, sy});
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) + abs(j) == 1) {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y]) {
            d[tx][ty] = d[x][y];
            q.push_front({tx, ty});
          }
        } else {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y] + 1) {
            d[tx][ty] = d[x][y] + 1;
            q.push_back({tx, ty});
          }
        }
      }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  sx--;
  sy--;
  ex--;
  ey--;
  memset(d, 0x3f, sizeof(d));
  bfs();
  if (d[ex][ey] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << d[ex][ey] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define mp(x, y) make_pair(x, y)

const int maxn = 5e5 + 10;

int nex1[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}},
    nex2[20][2] = {{1, 1},   {1, -1}, {-1, 1}, {-1, -1}, {-2, -2},
                   {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},  {-1, 2},
                   {-1, -2}, {0, 2},  {0, -2}, {1, 2},   {1, -2},
                   {2, 2},   {2, 1},  {2, 0},  {2, -1},  {2, -2}};
int d[1005][1005];
char Map[10005][1005];
int n, m;
int check(int x, int y) {
  if (x <= 0 || x > n || y <= 0 || y > m || Map[x][y] == '#')
    return 0;
  return 1;
}
void bfs(int a, int b) {
  d[a][b] = 0;
  deque<pair<int, int>> que;
  que.push_back(mp(a, b));
  while (!que.empty()) {
    int x, y;
    x = que.front().first, y = que.front().second;
    que.pop_front();
    for (int i = 0; i < 4; i++) {
      int _x = x + nex1[i][0], _y = y + nex1[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y]) {
        que.push_front(mp(_x, _y));
        d[_x][_y] = d[x][y];
      }
    }
    for (int i = 0; i < 20; i++) {
      int _x = x + nex2[i][0], _y = y + nex2[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y] + 1) {
        d[_x][_y] = d[x][y] + 1;
        que.push_back(mp(_x, _y));
      }
    }
  }
}
void init() {
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      d[i][j] = 1e9;
}
void solve() {
  cin >> n >> m;
  int x, y, tox, toy;
  cin >> x >> y >> tox >> toy;
  init();
  getchar();
  for (int i = 1; i <= n; i++)
    scanf("%s", Map[i] + 1);
  bfs(x, y);
  if (d[tox][toy] == 1e9)
    cout << -1 << endl;
  else
    cout << d[tox][toy] << endl;
}
signed main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;

int h, w;
int sx, sy, ex, ey;
int d[N][N];
string s[N];

int check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && s[x][y] == '.')
    return 1;
  return 0;
}
int bfs() {
  deque<pair<int, int>> q;
  q.push_back({sx, sy});
  d[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        if (abs(i) + abs(j) == 1) {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y]) {
            d[tx][ty] = d[x][y];
            q.push_front({tx, ty});
          }
        } else {
          int tx = x + i;
          int ty = y + j;
          if (check(tx, ty) && d[tx][ty] > d[x][y] + 1) {
            d[tx][ty] = d[x][y] + 1;
            q.push_back({tx, ty});
          }
        }
      }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w;
  cin >> sx >> sy >> ex >> ey;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  sx--;
  sy--;
  ex--;
  ey--;
  memset(d, 0x3f, sizeof(d));
  bfs();
  if (d[ex][ey] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << d[ex][ey] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define p pair<int, int>
using namespace std;

bool is_safe(vector<vector<char>> &a, int x, int y, int h, int w) {
  return x >= 0 && x < h && y >= 0 && y < w && a[x][y] != '#';
}

int32_t main() {
  int h, w, sx, sy, dx, dy;
  cin >> h >> w >> sx >> sy >> dx >> dy;

  vector<vector<char>> a(h, vector<char>(w, '.'));
  vector<vector<int>> dp(h, vector<int>(w, LONG_LONG_MAX));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  deque<p> q;
  q.push_front({sx - 1, sy - 1});
  dp[sx - 1][sy - 1] = 0;

  vector<p> move({{-1, 0}, {1, 0}, {0, 1}, {0, -1}});

  while (!q.empty()) {
    p curr = q.front();
    q.pop_front();
    int x = curr.first, y = curr.second;

    for (auto i : move) {
      int X = x + i.first;
      int Y = y + i.second;
      if (is_safe(a, X, Y, h, w) && dp[X][Y] > dp[x][y]) {
        dp[X][Y] = dp[x][y];
        q.push_front({X, Y});
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int X = x + i;
        int Y = y + j;
        if (is_safe(a, X, Y, h, w) && dp[X][Y] > dp[x][y] + 1) {
          dp[X][Y] = dp[x][y] + 1;
          q.push_back({X, Y});
        }
      }
    }
  }
  int res = dp[dx - 1][dy - 1];
  res == LONG_LONG_MAX ? cout << -1 : cout << res;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define p pair<int, int>
using namespace std;

bool is_safe(vector<vector<char>> &a, int x, int y, int h, int w) {
  return x >= 0 && x < h && y >= 0 && y < w && a[x][y] != '#';
}

int32_t main() {
  int h, w, sx, sy, dx, dy;
  cin >> h >> w >> sx >> sy >> dx >> dy;

  vector<vector<char>> a(h, vector<char>(w, '.'));
  vector<vector<int>> dp(h, vector<int>(w, LONG_LONG_MAX));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
    }
  }

  queue<p> q;
  q.push({sx - 1, sy - 1});
  dp[sx - 1][sy - 1] = 0;

  vector<p> move({{-1, 0}, {1, 0}, {0, 1}, {0, -1}});

  while (!q.empty()) {
    p curr = q.front();
    q.pop();
    int x = curr.first, y = curr.second;

    for (auto i : move) {
      int X = x + i.first;
      int Y = y + i.second;
      if (is_safe(a, X, Y, h, w) && dp[X][Y] > dp[x][y]) {
        dp[X][Y] = dp[x][y];
        q.push({X, Y});
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int X = x + i;
        int Y = y + j;
        if (is_safe(a, X, Y, h, w) && dp[X][Y] > dp[x][y] + 1) {
          dp[X][Y] = dp[x][y] + 1;
          q.push({X, Y});
        }
      }
    }
  }
  int res = dp[dx - 1][dy - 1];
  res == LONG_LONG_MAX ? cout << -1 : cout << res;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int dd[5] = {-2, -1, 0, 1, 2};

int h, w, sx, sy, gx, gy;
string s[1010];
int cost[1010][1010];

int main() {
  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gx--, gy--;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) cost[i][j] = INF;

  deque<pair<int, int>> que;
  cost[sy][sx] = 0;

  que.push_front({sx, sy});

  while (!que.empty()) {
    auto p = que.front();
    que.pop_front();
    int x = p.first, y = p.second;
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (s[ny][nx] == '#' || cost[ny][nx] <= cost[y][x])
        continue;
      cost[ny][nx] = min(cost[y][x], cost[ny][nx]);
      que.push_front({nx, ny});
    }
    rep(i, 5) rep(j, 5) {
      int nx = x + dd[i];
      int ny = y + dd[j];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (s[ny][nx] == '#' || cost[ny][nx] <= cost[y][x])
        continue;
      cost[ny][nx] = min(cost[y][x] + 1, cost[ny][nx]);
      que.push_back({nx, ny});
    }
  }

  if (cost[gy][gx] == INF)
    cout << -1 << endl;
  else
    cout << cost[gy][gx] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

int d[5] = {-2, -1, 0, 1, 2};

int h, w, sx, sy, gx, gy;
string s[1010];
int cost[1010][1010];
bool seen[1010][1010];

int main() {
  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gx--, gy--;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) cost[i][j] = INF;

  queue<pair<int, int>> que;
  cost[sy][sx] = 0;

  que.push({sx, sy});

  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    rep(i, 5) rep(j, 5) {
      if (d[i] == 0 && d[j] == 0)
        continue;
      int nx = x + d[i];
      int ny = y + d[j];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny || s[ny][nx] == '#')
        continue;
      if (cost[ny][nx] <= cost[y][x])
        continue;
      if (abs(d[i]) + abs(d[j]) == 1)
        cost[ny][nx] = min(cost[y][x], cost[ny][nx]);
      else
        cost[ny][nx] = min(cost[y][x] + 1, cost[ny][nx]);
      que.push({nx, ny});
    }
  }

  // rep(i, h){
  //     rep(j, w) cout << (cost[i][j]==LINF? -1:cost[i][j]) <<" ";
  //     cout << endl;
  // }

  if (cost[gy][gx] == INF)
    cout << -1 << endl;
  else
    cout << cost[gy][gx] << endl;

  return 0;
}