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

#include <bits/stdc++.h>
using namespace std;
// (setq-default c-basic-offset 2)

typedef pair<int, int> ii;

int main() {
  int n, m;
  cin >> n >> m;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  --sx;
  --sy;
  --tx;
  --ty;
  vector<string> s(n);
  for (int i = 0; i < n; ++i)
    cin >> s[i];
  vector<int> d(n * m, INT_MAX);
  priority_queue<ii, vector<ii>, greater<ii>> Q;
  vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  d[sx * m + sy] = 0;
  Q.push({0, sx * m + sy});
  while (!Q.empty()) {
    int dd = Q.top().first;
    int u = Q.top().second;
    Q.pop();
    if (dd != d[u])
      continue;
    int x = u / m, y = u % m;
    for (auto dir : dirs) {
      int nx = x + dir[0];
      int ny = y + dir[1];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m)
        continue;
      if (s[nx][ny] == '#')
        continue;
      int v = nx * m + ny;
      if (d[v] > d[u]) {
        d[v] = d[u];
        Q.push({d[v], v});
      }
    }
    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 >= n || ny < 0 || ny >= m)
          continue;
        if (s[nx][ny] == '#')
          continue;
        int v = nx * m + ny;
        if (d[v] > d[u] + 1) {
          d[v] = d[u] + 1;
          Q.push({d[v], v});
        }
      }
    }
  }
  int ret = d[tx * m + ty];
  if (ret == INT_MAX)
    ret = -1;
  cout << ret << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// (setq-default c-basic-offset 2)

typedef pair<int, int> ii;

int main() {
  int n, m;
  cin >> n >> m;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  --sx;
  --sy;
  --tx;
  --ty;
  vector<string> s(n);
  for (int i = 0; i < n; ++i)
    cin >> s[i];
  vector<int> d(n * m, INT_MAX);
  queue<ii> Q;
  vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  d[sx * m + sy] = 0;
  Q.push({0, sx * m + sy});
  while (!Q.empty()) {
    int old_du = Q.front().first;
    int u = Q.front().second;
    Q.pop();
    if (old_du != d[u])
      continue;
    int x = u / m, y = u % m;
    for (auto dir : dirs) {
      int nx = x + dir[0];
      int ny = y + dir[1];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m)
        continue;
      if (s[nx][ny] == '#')
        continue;
      int v = nx * m + ny;
      if (d[v] > d[u]) {
        d[v] = d[u];
        Q.push({d[v], v});
      }
    }
    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 >= n || ny < 0 || ny >= m)
          continue;
        if (s[nx][ny] == '#')
          continue;
        int v = nx * m + ny;
        if (d[v] > d[u] + 1) {
          d[v] = d[u] + 1;
          Q.push({d[v], v});
        }
      }
    }
  }
  int ret = d[tx * m + ty];
  if (ret == INT_MAX)
    ret = -1;
  cout << ret << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct go {
  int x, y, magic;
  bool operator<(const go &l) const { return magic > l.magic; }
};
priority_queue<go> q;
int h, w, ans = -1;
int ch, cw, dh, dw;
char s[1100][1100];
bool vis[1100][1100];
int fx[] = {1, 0, -1, 0};
int fy[] = {0, 1, 0, -1};
int main() {
  memset(vis, 0, sizeof(vis));
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> s[i][j];
  q.push((go){ch, cw, 0});
  while (!q.empty()) {
    go x = q.top();
    q.pop();
    if (x.x == dh && x.y == dw) {
      ans = x.magic;
      break;
    }
    if (vis[x.x][x.y])
      continue;
    for (int i = 0; i < 4; i++) {
      int tx = x.x + fx[i], ty = x.y + fy[i];
      if (tx < 1 || ty < 1 || tx > h || ty > w)
        continue;
      if (vis[tx][ty] == 0 && s[tx][ty] == '.')
        q.push((go){tx, ty, x.magic});
    }
    vis[x.x][x.y] = 1;
    for (int i = max(1, x.x - 2); i <= min(h, x.x + 2); i++)
      for (int j = max(1, x.y - 2); j <= min(w, x.y + 2); j++) {
        if (vis[i][j] == 0 && s[i][j] == '.')
          q.push((go){i, j, x.magic + 1});
      }
  }
  printf("%d", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct go {
  int x, y, magic;
  bool operator<(const go &l) const { return magic > l.magic; }
};
priority_queue<go> q;
int h, w, ans = -1;
int ch, cw, dh, dw;
char s[1100][1100];
bool vis[1100][1100];
int fx[] = {1, 0, -1, 0};
int fy[] = {0, 1, 0, -1};
int main() {
  memset(vis, 0, sizeof(vis));
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> s[i][j];
  q.push((go){ch, cw, 0});
  while (!q.empty()) {
    go x = q.top();
    q.pop();
    if (x.x == dh && x.y == dw) {
      ans = x.magic;
      break;
    }
    for (int i = 0; i < 4; i++) {
      int tx = x.x + fx[i], ty = x.y + fy[i];
      if (tx < 1 || ty < 1 || tx > h || tx > w)
        continue;
      if (vis[tx][ty] == 0 && s[tx][ty] == '.') {
        q.push((go){tx, ty, x.magic});
        vis[tx][ty] = 1;
      }
    }
    vis[x.x][x.y] = 1;
    for (int i = max(1, x.x - 2); i <= min(h, x.x + 2); i++)
      for (int j = max(1, x.y - 2); j <= min(w, x.y + 2); j++) {
        if (vis[i][j] == 0 && s[i][j] == '.')
          q.push((go){i, j, x.magic + 1});
      }
  }
  printf("%d", ans);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> s(h, vector<char>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> s[i][j];

  vector<vector<ll>> dist(h, vector<ll>(w, 1e9 + 100));

  deque<pair<int, int>> dq;
  dq.push_back(make_pair(ch, cw));
  dist[ch][cw] = 0;

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

  while (!dq.empty()) {
    auto n = dq.front();
    dq.pop_front();

    if (n.first == dh && n.second == dw)
      break;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = n.first + i, nx = n.second + j;
        if (!i && !j)
          continue;
        if (ny < 0 || h <= ny || nx < 0 || w <= nx)
          continue;
        if (s[ny][nx] == '#')
          continue;

        bool find = false;
        for (int k = 0; k < 4; k++) {
          if (i == dy[k] && j == dx[k]) {
            find = true;
            if (dist[n.first][n.second] < dist[ny][nx]) {
              dist[ny][nx] = dist[n.first][n.second];
              dq.push_front(make_pair(ny, nx));
            }
          }
        }
        if (find)
          continue;

        if (dist[n.first][n.second] + 1 < dist[ny][nx]) {
          dist[ny][nx] = dist[n.first][n.second] + 1;
          dq.push_back(make_pair(ny, nx));
        }
      }
    }
  }
  ll ans = dist[dh][dw] == 1e9 + 100 ? -1 : dist[dh][dw];
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> s(h, vector<char>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> s[i][j];

  vector<vector<bool>> visit(h, vector<bool>(w, false));
  vector<vector<ll>> dist(h, vector<ll>(w, 1e9 + 100));

  queue<pair<int, int>> q;
  q.push(make_pair(ch, cw));
  visit[ch][cw] = true;
  dist[ch][cw] = 0;

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

  while (!q.empty()) {
    auto n = q.front();
    q.pop();

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = n.first + i, nx = n.second + j;

        if (!i && !j)
          continue;
        if (ny < 0 || h <= ny || nx < 0 || w <= nx)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (visit[ny][nx])
          continue;

        // 魔法を使わずに行けるマスの処理
        for (int k = 0; k < 4; k++) {
          if (dy[k] == i && dx[k] == j) {
            q.push(make_pair(ny, nx));
            dist[ny][nx] = dist[n.first][n.second];
            visit[ny][nx] = true;
            continue;
          }
        }

        q.push(make_pair(ny, nx));
        dist[ny][nx] = min(dist[ny][nx], dist[n.first][n.second] + 1);
      }
    }
  }

  if (dist[dh][dw] == 1e9 + 100)
    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 ll = long long;
using P = pair<int, int>;

const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    if (dist[i][j] != d)
      continue;
    rep(v, 4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ++ei) {
      for (int ej = -2; ej <= 2; ++ej) {
        int ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int mh[4] = {0, 1, 0, -1};
  int mw[4] = {1, 0, -1, 0};
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string load[w];
  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  for (int i = 0; i < h; i++)
    cin >> load[i];

  deque<pair<int, int>> que;
  dist[ch][cw] = 0;
  que.emplace_back(ch, cw);

  while (!que.empty()) {
    int i = que.front().first;
    int j = que.front().second;
    int d = dist[i][j];
    que.pop_front();
    rep(mi, 4) {
      int ni = i + mh[mi];
      int nj = j + mw[mi];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (load[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      que.emplace_front(ni, nj);
    }
    for (int wi = -2; wi <= 2; wi++) {
      for (int wj = -2; wj <= 2; wj++) {
        int ni = i + wi;
        int nj = j + wj;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (load[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        que.emplace_back(ni, nj);
      }
    }
  }

  if (dist[dh][dw] == INF)
    cout << "-1" << endl;
  else
    cout << dist[dh][dw] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define LL long long
using namespace std;
const int MAXN = 1e3 + 5, inf = 0x3f3f3f3f;
struct Node {
  int X, Y, tmp;
  bool operator<(const Node p) const { return tmp > p.tmp; }
};
priority_queue<Node> que;
Node t, q;
int n, m, S, T, S1, T1, minn = inf, tox[5] = {1, -1, 0, 0},
                        toy[5] = {0, 0, 1, -1};
bool vis[MAXN][MAXN];
char a[MAXN][MAXN], s[MAXN];
int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &S, &T, &S1, &T1);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++) {
      a[i][j] = s[j];
    }
  }
  t.X = S;
  t.Y = T;
  t.tmp = 0;
  que.push(t);
  while (que.size()) {
    t = que.top();
    que.pop();
    if (vis[t.X][t.Y])
      continue;
    vis[t.X][t.Y] = 1;
    if (t.X == S1 && t.Y == T1) {
      minn = min(minn, t.tmp);
      break;
    }
    for (int i = 0; i < 4; i++) {
      int x = t.X + tox[i], y = t.Y + toy[i];
      if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || a[x][y] == '#')
        continue;
      q.X = x;
      q.Y = y;
      q.tmp = t.tmp;
      que.push(q);
    }
    for (int i = t.X - 2; i <= t.X + 2; i++) {
      for (int j = t.Y - 2; j <= t.Y + 2; j++) {
        if (i < 1 || i > n || j < 1 || j > m || vis[i][j] || a[i][j] == '#')
          continue;
        q.X = i;
        q.Y = j;
        q.tmp = t.tmp + 1;
        que.push(q);
      }
    }
  }
  if (minn == inf)
    minn = -1;
  printf("%d", minn);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define LL long long
using namespace std;
const int MAXN = 1e3 + 5, inf = 0x3f3f3f3f;
int n, m, S, T, S1, T1, dp[MAXN][MAXN], minn = inf;
char a[MAXN][MAXN], s[MAXN];
void dfs(int x, int y, int step) {
  if (x == S1 && y == T1) {
    minn = min(minn, step);
    return;
  }
  if (x < 1 || x > n || y < 1 || y > m)
    return;
  if (a[x][y] == '#')
    return;
  if (dp[x][y] <= step)
    return;
  dp[x][y] = step;
  dfs(x + 1, y, step);
  dfs(x, y + 1, step);
  dfs(x - 1, y, step);
  dfs(x, y - 1, step);
  for (int i = x - 2; i <= x + 2; i++) {
    for (int j = y - 2; j <= y + 2; j++) {
      if (i < 1 || i > n || j < 1 || j > m)
        continue;
      if ((x - i) > 2 || (x - i) < -2 || (y - j) > 2 || (y - j) < -2)
        continue;
      dfs(i, j, step + 1);
    }
  }
}
int main() {
  memset(dp, 0x3f, sizeof(dp));
  scanf("%d%d%d%d%d%d", &n, &m, &S, &T, &S1, &T1);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++) {
      a[i][j] = s[j];
    }
  }
  dfs(S, T, 0);
  dp[S1][T1] = minn;
  if (dp[S1][T1] == inf)
    dp[S1][T1] = -1;
  printf("%d", dp[S1][T1]);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1e9;

int main() {
  int h, w, y1, x1, y2, x2;
  cin >> h >> w >> y1 >> x1 >> y2 >> x2;
  y1--, x1--, y2--, x2--;
  vector<string> s(h);
  for (auto &e : s)
    cin >> e;

  vector<vector<int>> d(h, vector<int>(w, INF));
  d[y1][x1] = 0;

  priority_queue<tuple<int, int, int>> q;
  q.push({0, y1, x1});
  while (!q.empty()) {
    auto [dyx, y, x] = q.top();
    q.pop();
    if (dyx > d[y][x])
      continue;

    for (int dy = -2; dy <= 2; dy++) {
      auto yt = y + dy;
      if ((unsigned)yt >= h)
        continue;
      for (int dx = -2; dx <= 2; dx++) {
        auto xt = x + dx;
        if ((unsigned)xt >= w)
          continue;
        if (s[yt][xt] == '#')
          continue;

        int dt = d[y][x] + (abs(dy) + abs(dx) > 1 ? 1 : 0);
        if (dt < d[yt][xt]) {
          d[yt][xt] = dt;
          q.push({-dt, yt, xt});
        }
      }
    }
  }

  cout << (d[y2][x2] != INF ? d[y2][x2] : -1) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1e9;

int main() {
  int h, w, y1, x1, y2, x2;
  cin >> h >> w >> y1 >> x1 >> y2 >> x2;
  y1--, x1--, y2--, x2--;
  vector<string> s(h);
  for (auto &e : s)
    cin >> e;

  vector<vector<int>> d(h, vector<int>(w, INF));
  d[y1][x1] = 0;

  queue<pair<int, int>> q;
  q.push({y1, x1});
  while (!q.empty()) {
    auto [y, x] = q.front();
    q.pop();

    for (int dy = -2; dy <= 2; dy++) {
      auto yt = y + dy;
      if ((unsigned)yt >= h)
        continue;
      for (int dx = -2; dx <= 2; dx++) {
        auto xt = x + dx;
        if ((unsigned)xt >= w)
          continue;
        if (s[yt][xt] == '#')
          continue;

        int dt = d[y][x] + (abs(dy) + abs(dx) > 1 ? 1 : 0);
        if (dt < d[yt][xt]) {
          d[yt][xt] = dt;
          q.push({yt, xt});
        }
      }
    }
  }

  cout << (d[y2][x2] != INF ? d[y2][x2] : -1) << endl;
}
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 < n; i++)
typedef long long ll;
typedef pair<int, int> P;

const int di[] = {0, 0, -1, 1};
const int dj[] = {-1, 1, 0, 0};
const int INF = 1001001001;

int main() {
  ll h, w;
  cin >> h >> w;
  ll si, sj;
  cin >> si >> sj;
  ll gi, gj;
  cin >> gi >> gj;
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> S(h);
  rep(i, h) cin >> S[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    ll i = q.front().first;
    ll j = q.front().second;
    q.pop_front();
    ll d = dist[i][j];
    if (dist[i][j] != d)
      continue;
    rep(v, 4) {
      ll ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (S[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (ll ei = -2; ei <= 2; ei++) {
      for (ll ej = -2; ej <= 2; ej++) {
        ll ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (S[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[gi][gj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

const int di[] = {0, 0, -1, 1};
const int dj[] = {-1, 1, 0, 0};
const int INF = 1001001001;

int main() {
  ll h, w;
  cin >> h >> w;
  ll si, sj;
  cin >> si >> sj;
  ll gi, gj;
  cin >> gi >> gj;
  si--;
  sj--;
  gi--;
  gj--;
  vector<string> S(h);
  rep(i, h) cin >> S[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[si][sj] = 0;
  q.emplace_back(si, sj);
  while (!q.empty()) {
    ll i = q.front().first;
    ll j = q.front().second;
    q.pop_front();
    ll d = dist[i][j];
    if (dist[i][j] != d)
      continue;
    rep(v, 4) {
      ll ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (S[ni][nj] == '#')
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (ll ei = -2; ei <= 2; ei++) {
      for (ll ej = -2; ej <= 2; ej++) {
        ll ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (S[ni][nj] == '#')
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  cout << dist[gi][gj] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N, M;
  cin >> N >> M;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  vector<string> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  deque<pair<pair<int, int>, int>> dq;
  dq.push_back({{sx, sy}, 0});
  vector<vector<int>> dist(N, vector<int>(M, -1));
  dist[sx][sy] = 0;
  while (!dq.empty()) {
    int x = dq.front().first.first;
    int y = dq.front().first.second;
    int d = dq.front().second;
    dq.pop_front();
    if (dist[x][y] != d) {
      continue;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (abs(i) + abs(j) == 1 && 0 <= nx && nx < N && 0 <= ny && ny < M &&
            A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y]) {
            dist[nx][ny] = dist[x][y];
            dq.push_front({{nx, ny}, dist[nx][ny]});
          }
        }
        if (0 <= nx && nx < N && 0 <= ny && ny < M && A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            dq.push_back({{nx, ny}, dist[nx][ny]});
          }
        }
      }
    }
  }
  cout << dist[ex][ey] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N, M;
  cin >> N >> M;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  vector<string> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  deque<pair<pair<int, int>, int>> dq;
  dq.push_back({{sx, sy}, 0});
  vector<vector<int>> dist(N, vector<int>(M, -1));
  dist[sx][sy] = 0;
  while (!dq.empty()) {
    int x = dq.front().first.first;
    int y = dq.front().first.second;
    int d = dq.front().second;
    dq.pop_front();
    if (dist[x][y] != d) {
      continue;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (abs(i) + abs(j) == 1 && 0 <= nx && nx < N && 0 <= ny && ny < M &&
            A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y]) {
            dist[nx][ny] = dist[x][y];
            dq.push_front({{nx, ny}, dist[nx][ny]});
          }
        }
        if (0 <= nx && nx < N && 0 <= ny && ny < M && A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            dq.push_front({{nx, ny}, dist[nx][ny]});
          }
        }
      }
    }
  }
  cout << dist[ex][ey] << "\\n";
  return 0;
}