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

#include <bits/stdc++.h>
#define DAU                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define PLEC exit(0);
using namespace std;
const int N(1005);
const int dx[]{0, 0, 1, -1};
const int dy[]{1, -1, 0, 0};
int n, m, a, b, c, d, dp[N][N], x, y;
char mat[N][N];
priority_queue<tuple<int, int, int>> q;
inline bool Inside(int X, int Y) {
  if (X < 1 || X > n || Y < 1 || Y > m)
    return false;
  return true;
}
int32_t main() {
  DAU cin >> n >> m >> a >> b >> c >> d;
  for (int i = 1; i <= n; ++i)
    cin >> (mat[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      dp[i][j] = 1e9;
  dp[a][b] = 0;
  q.emplace(0, a, b);
  while (!q.empty()) {
    tie(ignore, x, y) = q.top();
    q.pop();
    int xv, yv;
    for (int dir = 0; dir < 4; ++dir) {
      xv = x + dx[dir];
      yv = y + dy[dir];
      if (Inside(xv, yv) && mat[xv][yv] == '.' && dp[xv][yv] > dp[x][y]) {
        dp[xv][yv] = dp[x][y];
        q.emplace(-dp[xv][yv], xv, yv);
      }
    }
    for (xv = x - 2; xv <= x + 2; ++xv)
      for (yv = y - 2; yv <= y + 2; ++yv)
        if (Inside(xv, yv) && mat[xv][yv] == '.' && dp[xv][yv] > dp[x][y] + 1) {
          dp[xv][yv] = dp[x][y] + 1;
          q.emplace(-dp[xv][yv], xv, yv);
        }
  }
  if (dp[c][d] == 1e9)
    dp[c][d] = -1;
  cout << dp[c][d];
  PLEC
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define DAU                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define PLEC exit(0);
using namespace std;
const int N(1005);
const int dx[]{0, 0, 1, -1};
const int dy[]{1, -1, 0, 0};
int n, m, a, b, c, d, dp[N][N], x, y;
char mat[N][N];
queue<pair<int, int>> q;
inline bool Inside(int X, int Y) {
  if (X < 1 || X > n || Y < 1 || Y > m)
    return false;
  return true;
}
int32_t main() {
  DAU cin >> n >> m >> a >> b >> c >> d;
  for (int i = 1; i <= n; ++i)
    cin >> (mat[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      dp[i][j] = 1e9;
  dp[a][b] = 0;
  q.emplace(a, b);
  while (!q.empty()) {
    tie(x, y) = q.front();
    q.pop();
    int xv, yv;
    for (int dir = 0; dir < 4; ++dir) {
      xv = x + dx[dir];
      yv = y + dy[dir];
      if (Inside(xv, yv) && mat[xv][yv] == '.' && dp[xv][yv] > dp[x][y]) {
        dp[xv][yv] = dp[x][y];
        q.emplace(xv, yv);
      }
    }
    for (xv = x - 2; xv <= x + 2; ++xv)
      for (yv = y - 2; yv <= y + 2; ++yv)
        if (Inside(xv, yv) && mat[xv][yv] == '.' && dp[xv][yv] > dp[x][y] + 1) {
          dp[xv][yv] = dp[x][y] + 1;
          q.emplace(xv, yv);
        }
  }
  if (dp[c][d] == 1e9)
    dp[c][d] = -1;
  cout << dp[c][d];
  PLEC
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int 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));
  dist[si][sj] = 0;
  deque<P> q;
  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;
}


Error: TLE

Fixed submission:

// D - Wizard in Maze
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

pair<int, int> dhw[] = { // <h, w>
    {1, 0},   {0, -1}, {0, 1},  {-1, 0},  {2, -2},  {2, -1}, {2, 0},  {2, 1},
    {2, 2},   {1, -2}, {1, -1}, {1, 1},   {1, 2},   {0, -2}, {0, 2},  {-1, -2},
    {-1, -1}, {-1, 1}, {-1, 2}, {-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}};

int main() {
  int H, W;
  cin >> H >> W;
  int sh, sw, eh, ew;
  cin >> sh >> sw >> eh >> ew;
  sh--, sw--, eh--;
  ew--;
  string S[H];
  rp(i, 0, H) cin >> S[i];

  vector<vi> dst(H, vi(W, INT_MAX));
  dst[sh][sw] = 0;
  deque<pair<int, int>> dq; // <h, w>
  dq.push_front({sh, sw});

  while (dq.size()) {
    int h, w;
    tie(h, w) = dq.front();
    dq.pop_front();
    int d = dst[h][w];

    rp(i, 0, 4) {
      int nx_h = h + dhw[i].first, nx_w = w + dhw[i].second;
      if (nx_h < 0 || H <= nx_h || nx_w < 0 || W <= nx_w)
        continue;
      if (dst[nx_h][nx_w] <= d || S[nx_h][nx_w] == '#')
        continue;
      dst[nx_h][nx_w] = d;
      dq.push_front({nx_h, nx_w});
    }
    rp(i, 4, 24) {
      int nx_h = h + dhw[i].first, nx_w = w + dhw[i].second;
      if (nx_h < 0 || H <= nx_h || nx_w < 0 || W <= nx_w)
        continue;
      if (dst[nx_h][nx_w] <= d + 1 || S[nx_h][nx_w] == '#')
        continue;
      dst[nx_h][nx_w] = d + 1;
      dq.push_front({nx_h, nx_w});
    }
  }

  cout << (dst[eh][ew] == INT_MAX ? -1 : dst[eh][ew]) << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <deque>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int h, w, i, j, ch, cw, dh, dw, nh, nw, nv;
  cin >> h >> w;
  vector<vector<char>> s(h, vector<char>(w));
  vector<vector<int>> c(h, vector<int>(w, 1e7));
  cin >> ch >> cw;
  --ch;
  --cw;
  cin >> dh >> dw;
  --dh;
  --dw;
  for (i = 0; i < h; ++i) {
    for (j = 0; j < w; ++j) {
      cin >> s.at(i).at(j);
    }
  }
  c.at(ch).at(cw) = 0;
  deque<pair<int, int>> q;
  q.push_front({ch, cw});
  while (q.size()) {
    nh = q.front().first;
    nw = q.front().second;
    nv = c.at(nh).at(nw);
    q.pop_front();
    for (i = -2; i <= 2; ++i) {
      for (j = -2; j <= 2; ++j) {
        if (nh + i < 0 || nh + i >= h)
          continue;
        if (nw + j < 0 || nw + j >= w)
          continue;
        if (s.at(nh + i).at(nw + j) == '#')
          continue;
        if (i == 0 && j == 0)
          continue;
        if (c.at(nh + i).at(nw + j) > nv) {
          if (abs(i) + abs(j) == 1) {
            c.at(nh + i).at(nw + j) = nv;
            q.push_front({nh + i, nw + j});
          } else if (c.at(nh + i).at(nw + j) == 1e7) {
            c.at(nh + i).at(nw + j) = nv + 1;
            q.push_back({nh + i, nw + j});
          }
        }
      }
    }
  }
  if (c.at(dh).at(dw) == 1e7)
    cout << -1 << endl;
  else
    cout << c.at(dh).at(dw) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int h, w, i, j, ch, cw, dh, dw, nh, nw, nv;
  cin >> h >> w;
  vector<vector<char>> s(h, vector<char>(w));
  vector<vector<int>> c(h, vector<int>(w, 1e7));
  cin >> ch >> cw;
  --ch;
  --cw;
  cin >> dh >> dw;
  --dh;
  --dw;
  for (i = 0; i < h; ++i) {
    for (j = 0; j < w; ++j) {
      cin >> s.at(i).at(j);
    }
  }
  c.at(ch).at(cw) = 0;
  deque<pair<int, int>> q;
  q.push_front({ch, cw});
  while (q.size()) {
    nh = q.front().first;
    nw = q.front().second;
    nv = c.at(nh).at(nw);
    q.pop_front();
    for (i = -2; i <= 2; ++i) {
      for (j = -2; j <= 2; ++j) {
        if (nh + i < 0 || nh + i >= h)
          continue;
        if (nw + j < 0 || nw + j >= w)
          continue;
        if (s.at(nh + i).at(nw + j) == '#')
          continue;
        if (i == 0 && j == 0)
          continue;
        if (c.at(nh + i).at(nw + j) > nv) {
          if (abs(i) + abs(j) == 1) {
            c.at(nh + i).at(nw + j) = nv;
          } else {
            c.at(nh + i).at(nw + j) = nv + 1;
          }
          q.push_front({nh + i, nw + j});
        }
      }
    }
  }
  if (c.at(dh).at(dw) == 1e7)
    cout << -1 << endl;
  else
    cout << c.at(dh).at(dw) << endl;
  return 0;
}
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++)
using ll = long long;
typedef pair<int, int> P;
const int INF = 1e9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
  int h, w, sx, sy, gx, gy;
  cin >> h >> w >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<vector<char>> map(h, vector<char>(w));
  vector<vector<int>> dist(h, vector<int>(w, INF));
  rep(i, h) rep(j, w) cin >> map[i][j];
  deque<P> que;
  que.emplace_front(P(sx, sy));
  dist[sx][sy] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop_front();
    if (p.first == gx && p.second == gy)
      break;
    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny ||
          dist[nx][ny] <= dist[p.first][p.second])
        continue;
      if (map[nx][ny] == '.') {
        que.emplace_front(P(nx, ny));
        dist[nx][ny] = dist[p.first][p.second];
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        int nx = p.first + i, ny = p.second + j;
        if (nx < 0 || h <= nx || ny < 0 || w <= ny ||
            dist[nx][ny] <= dist[p.first][p.second] + 1)
          continue;
        if (map[nx][ny] == '.') {
          que.emplace_back(P(nx, ny));
          dist[nx][ny] = dist[p.first][p.second] + 1;
        }
      }
    }
  }
  if (dist[gx][gy] == INF)
    cout << "-1";
  else
    cout << dist[gx][gy];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
typedef pair<int, int> P;
const int INF = 1e9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
  int h, w, sx, sy, gx, gy;
  cin >> h >> w >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<vector<char>> map(h, vector<char>(w));
  vector<vector<int>> dist(h, vector<int>(w, INF));
  rep(i, h) rep(j, w) cin >> map[i][j];
  queue<P> que;
  que.push(P(sx, sy));
  dist[sx][sy] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy)
      break;
    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny ||
          dist[nx][ny] <= dist[p.first][p.second])
        continue;
      if (map[nx][ny] == '.') {
        que.push(P(nx, ny));
        dist[nx][ny] = dist[p.first][p.second];
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        int nx = p.first + i, ny = p.second + j;
        if (nx < 0 || h <= nx || ny < 0 || w <= ny ||
            dist[nx][ny] < dist[p.first][p.second] + 1)
          continue;
        if (map[nx][ny] == '.') {
          que.push(P(nx, ny));
          dist[nx][ny] = dist[p.first][p.second] + 1;
        }
      }
    }
  }
  if (dist[gx][gy] == INF)
    cout << "-1";
  else
    cout << dist[gx][gy];
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(k, i, n) for (ll i = k; i < n; ++i)
#define pb push_back
vector<pair<ll, ll>> check_list, ncheck_list;
ll sx, sy, gx, gy, h, w;
string s[(ll)1e3 + 5];
vector<vector<ll>> d((ll)1e3 + 5, vector<ll>(1e3 + 5, -1));
ll dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
void dfs(ll x, ll y, ll cnt) {
  if (y < 0 || x < 0 || x >= h || y >= w)
    return;
  if (s[x][y] == '#')
    return;
  if (d[x][y] != -1)
    return;
  d[x][y] = cnt;
  check_list.pb({x, y});
  rep(0, i, 4) {
    ll nx = x + dx[i], ny = y + dy[i];
    dfs(nx, ny, cnt);
  }
}
void dfs2(ll x, ll y, ll cnt) {
  rep(-2, i, 3) {
    rep(-2, j, 3) {
      ll nx = x + i, ny = y + j;
      dfs(nx, ny, cnt);
    }
  }
}
int main(void) {
  cin >> h >> w;
  cin >> sx >> sy;
  --sx, --sy;
  cin >> gx >> gy;
  --gx, --gy;
  rep(0, i, h) cin >> s[i];
  ll cnt = 0;
  dfs(sx, sy, cnt);
  while (check_list.size()) {
    cnt++;
    ncheck_list.clear();
    for (auto p : check_list) {
      ncheck_list.pb(p);
    }
    check_list.clear();
    for (auto p : ncheck_list) {
      ll x = p.first, y = p.second;
      dfs2(x, y, cnt);
    }
  }
  cout << d[gx][gy];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(k, i, n) for (ll i = k; i < n; ++i)
ll h, w, sx, sy, fx, fy;
struct P {
  ll pt, px, py;
};
bool operator<(const P &a, const P &b) { return a.pt > b.pt; }
string s[(ll)1e3];
ll dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
ll bfs() {
  priority_queue<P> q;
  if (s[sx][sy] == '.')
    q.push({0, sx, sy});
  while (!q.empty()) {
    auto nq = q.top();
    ll t = nq.pt, x = nq.px, y = nq.py;
    q.pop();
    // cout<<"("<<t<<","<<x<<","<<y<<")"<<endl;
    if (x == fx && y == fy)
      return t;
    s[x][y] = '0' + t;
    rep(0, i, 4) {
      ll nx = x + dx[i], ny = y + dy[i];
      // cout<<nx<<" "<<ny<<endl;
      if ((0 <= nx && nx < h) && (0 <= ny && ny < w)) {
        if (s[nx][ny] == '.') {
          q.push({t, nx, ny});
          // cout<<"("<<t<<","<<nx<<","<<ny<<")"<<endl;
        }
      }
    }
    rep(-2, ndx, 3) {
      rep(-2, ndy, 3) {
        if (ndx == 0 && ndy == 0)
          continue;
        ll nx = x + ndx, ny = y + ndy;
        if ((0 <= nx && nx < h) && (0 <= ny && ny < w)) {
          if (s[nx][ny] == '.') {
            q.push({t + 1, nx, ny});
            // cout<<"("<<t<<","<<nx<<","<<ny<<")"<<endl;
          }
        }
      }
    }
  }
  return -1;
}
int main(void) {
  cin >> h >> w;
  cin >> sx >> sy;
  --sx, --sy;
  cin >> fx >> fy;
  --fx, --fy;
  rep(0, i, h) cin >> s[i];
  cout << bfs() << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<bool>> done(H, vector<bool>(W, false));
  vector<stack<pair<ll, ll>>> diststack(2);
  ll d = 0;
  diststack[0].push(make_pair(sh, sw));
  while (d <= H * W + 2) {
    if (diststack[d % 2].empty()) {
      d++;
      continue;
    }
    auto src = diststack[d % 2].top();
    diststack[d % 2].pop();
    ll h = src.first;
    ll w = src.second;
    if (h == gh && w == gw) {
      cout << d << endl;
      return 0;
    }
    if (done[h][w]) {
      continue;
    }
    done[h][w] = true;
    for (ll y = h - 2; y <= h + 2; y++) {
      for (ll x = w - 2; x <= w + 2; x++) {
        if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
          if (abs(x - w) + abs(y - h) == 1) {
            diststack[d % 2].push(make_pair(y, x));
          } else {
            diststack[1 - (d % 2)].push(make_pair(y, x));
          }
        }
      }
    }
  }
  cout << -1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<bool>> done(H, vector<bool>(W, false));
  vector<stack<pair<ll, ll>>> diststack(H + W + 1);
  ll d = 0;
  diststack[0].push(make_pair(sh, sw));
  while (d < (ll)diststack.size()) {
    if (diststack[d].empty()) {
      d++;
      continue;
    }
    auto src = diststack[d].top();
    diststack[d].pop();
    ll h = src.first;
    ll w = src.second;
    if (h == gh && w == gw) {
      cout << d << endl;
      return 0;
    }
    if (done[h][w]) {
      continue;
    }
    done[h][w] = true;
    for (ll y = h - 2; y <= h + 2; y++) {
      for (ll x = w - 2; x <= w + 2; x++) {
        if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
          if (abs(x - w) + abs(y - h) == 1) {
            diststack[d].push(make_pair(y, x));
          } else {
            diststack[d + 1].push(make_pair(y, x));
          }
        }
      }
    }
  }
  cout << -1 << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<bool>> done(H, vector<bool>(W, false));
  vector<stack<pair<ll, ll>>> diststack(1 + (H * W) / 2);
  ll d = 0;
  diststack[0].push(make_pair(sh, sw));
  while (d < (ll)diststack.size()) {
    if (diststack[d].empty()) {
      d++;
      continue;
    }
    auto src = diststack[d].top();
    diststack[d].pop();
    ll h = src.first;
    ll w = src.second;
    if (h == gh && w == gw) {
      cout << d << endl;
      return 0;
    }
    if (done[h][w]) {
      continue;
    }
    done[h][w] = true;
    for (ll y = h - 2; y <= h + 2; y++) {
      for (ll x = w - 2; x <= w + 2; x++) {
        if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
          if (abs(x - w) + abs(y - h) == 1) {
            diststack[d].push(make_pair(y, x));
          } else {
            diststack[d + 1].push(make_pair(y, x));
          }
        }
      }
    }
  }
  cout << -1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<bool>> done(H, vector<bool>(W, false));
  vector<stack<pair<ll, ll>>> diststack(1 + (H * W) / 4);
  ll d = 0;
  diststack[0].push(make_pair(sh, sw));
  while (d < (ll)diststack.size()) {
    if (diststack[d].empty()) {
      d++;
      continue;
    }
    auto src = diststack[d].top();
    diststack[d].pop();
    ll h = src.first;
    ll w = src.second;
    if (h == gh && w == gw) {
      cout << d << endl;
      return 0;
    }
    if (done[h][w]) {
      continue;
    }
    done[h][w] = true;
    for (ll y = h - 2; y <= h + 2; y++) {
      for (ll x = w - 2; x <= w + 2; x++) {
        if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x] && !done[y][x]) {
          if (abs(x - w) + abs(y - h) == 1) {
            diststack[d].push(make_pair(y, x));
          } else {
            diststack[d + 1].push(make_pair(y, x));
          }
        }
      }
    }
  }
  cout << -1 << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct xy {
  int x;
  int y;
  int d;
  bool operator<(const xy &another) const { return d > another.d; };
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<bool>> f(h, vector<bool>(w, false));
  int cx, cy;
  cin >> cx >> cy;
  cx--;
  cy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  vector<string> s(h);
  REP(i, h) cin >> s[i];
  priority_queue<xy> q;
  q.push({cx, cy, 0});
  VVI dis(h, VI(w, INF));
  while (!q.empty()) {
    int x = q.top().x, y = q.top().y;
    int d = q.top().d;
    q.pop();
    if (dis[x][y] < d)
      continue;
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int tox = x + i, toy = y + j;
        if (i == 0 && j == 0)
          continue;
        if (tox < 0 || tox >= h || toy < 0 || toy >= w)
          continue;
        if (s[tox][toy] == '#')
          continue;
        int pls = 1;
        if (abs(i) + abs(j) <= 1)
          pls = 0;
        if (dis[tox][toy] > d + pls) {
          q.push({tox, toy, d + pls});
          dis[tox][toy] = d + pls;
        }
      }
  }
  if (dis[gx][gy] == INF)
    cout << -1 << endl;
  else
    cout << dis[gx][gy] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct xy {
  int x;
  int y;
  int d;
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<bool>> f(h, vector<bool>(w, false));
  int cx, cy;
  cin >> cx >> cy;
  cx--;
  cy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;
  vector<string> s(h);
  REP(i, h) cin >> s[i];
  queue<xy> q;
  q.push({cx, cy, 0});
  VVI dis(h, VI(w, INF));
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y;
    int d = q.front().d;
    q.pop();
    if (dis[x][y] < d)
      continue;
    REP(k, 4) {
      int tox = x + dx[k], toy = y + dy[k];
      if (tox < 0 || tox >= h || toy < 0 || toy >= w)
        continue;
      if (s[tox][toy] == '#')
        continue;
      if (dis[tox][toy] > d) {
        q.push({tox, toy, d});
        dis[tox][toy] = d;
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int tox = x + i, toy = y + j;
        if (tox < 0 || tox >= h || toy < 0 || toy >= w)
          continue;
        if (s[tox][toy] == '#')
          continue;
        if (dis[tox][toy] > d + 1) {
          q.push({tox, toy, d + 1});
          dis[tox][toy] = d + 1;
        }
      }
  }
  if (dis[gx][gy] == INF)
    cout << -1 << endl;
  else
    cout << dis[gx][gy] << endl;
  return 0;
}