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

/*
 * “The difference between a master and a novice - is that the master has failed
 * more times, than the novice has tried.”
 */
#include <bits/stdc++.h>
#define pvar(x) clog << "[" << #x << ":" << x << "]\\n"
using namespace std;
using ll = long long int;
using tiii = tuple<int, int, int>;
const int inf = 1e6 + 1;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int main() {
  int h, w, ix, iy, dtx, dty;
  cin >> h >> w >> ix >> iy >> dtx >> dty;
  char v[h + 1][w + 1];
  int dist[h + 1][w + 1];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf(" %c", &v[i][j]);
      dist[i][j] = inf;
    }
  }
  set<tiii> q;
  dist[ix][iy] = 0;
  q.insert({dist[ix][iy], ix, iy});
  while (!q.empty()) {
    int d, cx, cy;
    auto it = q.begin();
    tie(d, cx, cy) = (*it);
    q.erase(it);
    for (int i = 0; i < 4; i++) {
      int x = cx + dx[i], y = cy + dy[i];
      if (x <= 0 || x > h || y <= 0 || y > w || v[x][y] == '#')
        continue;
      if (dist[x][y] > d) { // Relax edge
        dist[x][y] = d;
        q.insert({dist[x][y], x, y});
      }
    }

    for (int p = cx - 2; p <= cx + 2; p++) {
      for (int l = cy - 2; l <= cy + 2; l++) {
        if (p <= 0 || p > h || l <= 0 || l > w || v[p][l] == '#')
          continue;
        if (dist[p][l] > d + 1) {
          dist[p][l] = d + 1;
          q.insert({dist[p][l], p, l});
        }
      }
    }
  }
  if (dist[dtx][dty] == inf)
    printf("-1\\n");
  else
    printf("%d\\n", dist[dtx][dty]);
}


Error: TLE

Fixed submission:

/*
 * “The difference between a master and a novice - is that the master has failed
 * more times, than the novice has tried.”
 */
#include <bits/stdc++.h>
#define pvar(x) clog << "[" << #x << ":" << x << "]\\n"
using namespace std;
using ll = long long int;
const int inf = 1e6 + 1;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int main() {
  int h, w, ix, iy, dtx, dty;
  cin >> h >> w >> ix >> iy >> dtx >> dty;
  char v[h + 1][w + 1];
  int dist[h + 1][w + 1];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf(" %c", &v[i][j]);
      dist[i][j] = inf;
    }
  }
  queue<pair<int, int>> q; // d,x,y
  dist[ix][iy] = 0;
  q.push({ix, iy});
  while (!q.empty()) {
    int d, cx, cy;
    cx = q.front().first;
    cy = q.front().second;
    d = dist[cx][cy];
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = cx + dx[i], y = cy + dy[i];
      if (x <= 0 || x > h || y <= 0 || y > w || v[x][y] == '#')
        continue;
      if (dist[x][y] > d) { // Relax edge
        dist[x][y] = d;
        q.push({x, y});
      }
    }

    for (int p = cx - 2; p <= cx + 2; p++) {
      for (int l = cy - 2; l <= cy + 2; l++) {
        if (p <= 0 || p > h || l <= 0 || l > w || v[p][l] == '#')
          continue;
        if (dist[p][l] > d + 1) {
          dist[p][l] = d + 1;
          q.push({p, l});
        }
      }
    }
  }
  if (dist[dtx][dty] == inf)
    printf("-1\\n");
  else
    printf("%d\\n", dist[dtx][dty]);
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int INF = 1e9;
int h, w;
int ch, cw;
int th, tw;
char s[N][N];
int dist[N][N];
int dh[] = {-1, 1, 0, 0};
int dw[] = {0, 0, -1, 1};
bool valid(int a, int b) {
  return a >= 0 and a < h and b >= 0 and b < w and s[a][b] != '#';
}
int main() {
  scanf("%d %d", &h, &w);
  scanf("%d %d", &ch, &cw);
  scanf("%d %d", &th, &tw);
  ch--, cw--, th--, tw--;
  for (int i = 0; i < h; ++i) {
    scanf("%s", s[i]);
    for (int j = 0; j < w; ++j)
      dist[i][j] = INF;
  }
  deque<tuple<int, int, int>> dq;
  dq.emplace_back(ch, cw, 0);
  dist[ch][cw] = 0;
  while (!dq.empty()) {
    auto front = dq.front();
    dq.pop_front();
    int ph = get<0>(front);
    int pw = get<1>(front);
    int d = get<2>(front);
    if (d > dist[ph][pw])
      continue;
    for (int i = 0; i < 4; ++i) {
      int nxt_h = ph + dh[i];
      int nxt_w = pw + dw[i];
      if (valid(nxt_h, nxt_w) and dist[ph][pw] < dist[nxt_h][nxt_w]) {
        dist[nxt_h][nxt_w] = dist[ph][pw];
        dq.emplace_front(nxt_h, nxt_w, dist[nxt_h][nxt_w]);
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nxt_h = ph + i;
        int nxt_w = pw + j;
        if (valid(nxt_h, nxt_w) and dist[ph][pw] + 1 < dist[nxt_h][nxt_w]) {
          dist[nxt_h][nxt_w] = dist[ph][pw] + 1;
          dq.emplace_back(nxt_h, nxt_w, dist[nxt_h][nxt_w]);
        }
      }
    }
  }
  if (dist[th][tw] == INF)
    puts("-1");
  else
    printf("%d\\n", dist[th][tw]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int INF = 1e9;
int h, w;
int ch, cw;
int th, tw;
char s[N][N];
int dist[N][N];
int dh[] = {-1, 1, 0, 0};
int dw[] = {0, 0, -1, 1};
bool valid(int a, int b) {
  return a >= 0 and a < h and b >= 0 and b < w and s[a][b] != '#';
}
int main() {
  scanf("%d %d", &h, &w);
  scanf("%d %d", &ch, &cw);
  scanf("%d %d", &th, &tw);
  ch--, cw--, th--, tw--;
  for (int i = 0; i < h; ++i) {
    scanf("%s", s[i]);
    for (int j = 0; j < w; ++j)
      dist[i][j] = INF;
  }
  queue<tuple<int, int, int>> q;
  q.emplace(ch, cw, 0);
  dist[ch][cw] = 0;
  while (!q.empty()) {
    auto front = q.front();
    q.pop();
    int ph = get<0>(front);
    int pw = get<1>(front);
    int d = get<2>(front);
    if (d > dist[ph][pw])
      continue;
    for (int i = 0; i < 4; ++i) {
      int nxt_h = ph + dh[i];
      int nxt_w = pw + dw[i];
      if (valid(nxt_h, nxt_w) and dist[ph][pw] < dist[nxt_h][nxt_w]) {
        dist[nxt_h][nxt_w] = dist[ph][pw];
        q.emplace(nxt_h, nxt_w, dist[nxt_h][nxt_w]);
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nxt_h = ph + i;
        int nxt_w = pw + j;
        if (valid(nxt_h, nxt_w) and dist[ph][pw] + 1 < dist[nxt_h][nxt_w]) {
          dist[nxt_h][nxt_w] = dist[ph][pw] + 1;
          q.emplace(nxt_h, nxt_w, dist[nxt_h][nxt_w]);
        }
      }
    }
  }
  if (dist[th][tw] == INF)
    puts("-1");
  else
    printf("%d\\n", dist[th][tw]);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  for (string &i : s)
    cin >> i;
  vvi l(h, vi(w, INF));
  priority_queue<vi, vvi, greater<vi>> q;
  q.push({0, ch, cw});
  while (!q.empty()) {
    int gl = q.top().at(0);
    int gh = q.top().at(1);
    int gw = q.top().at(2);
    q.pop();

    if (gl >= l[gh][gw])
      continue;

    l[gh][gw] = gl;
    FOR(mh, -2, 3)
    FOR(mw, -2,
        3) if ((mh != 0 or mw != 0) and (0 <= gh + mh and gh + mh < h) and
               (0 <= gw + mw and gw + mw < w) and
               (s[gh + mh][gw + mw] == '.') and
               (gl + (abs(mh) + abs(mw) == 1 ? 0 : 1) < l[gh + mh][gw + mw]))
        q.push({gl + (abs(mh) + abs(mw) == 1 ? 0 : 1), gh + mh, gw + mw});
  }
  if (l[dh][dw] != INF)
    cout << l[dh][dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
using P = pair<int, int>;
using P2 = pair<P, int>;

int g[1005][1005];
int m[1005][1005];

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#')
        g[i][j] = 0;
      else
        g[i][j] = 1;
    }
  }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      m[i][j] = INF;
  priority_queue<P2, vector<P2>, greater<P2>> q;
  q.push({{ch, cw}, 0});
  while (!q.empty()) {
    P2 p2 = q.top();
    q.pop();
    int x = p2.first.first;
    int y = p2.first.second;
    int mov = p2.second;
    if (x < 0 or x >= h or y < 0 or y >= w)
      continue;

    if (g[x][y] and m[x][y] > mov)
      m[x][y] = mov;
    else
      continue;

    q.push({{x - 2, y - 2}, mov + 1});
    q.push({{x - 2, y - 1}, mov + 1});
    q.push({{x - 2, y}, mov + 1});
    q.push({{x - 2, y + 1}, mov + 1});
    q.push({{x - 2, y + 2}, mov + 1});

    q.push({{x - 1, y - 2}, mov + 1});
    q.push({{x - 1, y - 1}, mov + 1});
    q.push({{x - 1, y}, mov});
    q.push({{x - 1, y + 1}, mov + 1});
    q.push({{x - 1, y + 2}, mov + 1});

    q.push({{x, y - 2}, mov + 1});
    q.push({{x, y - 1}, mov});
    q.push({{x, y + 1}, mov});
    q.push({{x, y + 2}, mov + 1});

    q.push({{x + 1, y - 2}, mov + 1});
    q.push({{x + 1, y - 1}, mov + 1});
    q.push({{x + 1, y}, mov});
    q.push({{x + 1, y + 1}, mov + 1});
    q.push({{x + 1, y + 2}, mov + 1});

    q.push({{x + 2, y - 2}, mov + 1});
    q.push({{x + 2, y - 1}, mov + 1});
    q.push({{x + 2, y}, mov + 1});
    q.push({{x + 2, y + 1}, mov + 1});
    q.push({{x + 2, y + 2}, mov + 1});
  }
  if (m[dh][dw] != INF)
    cout << m[dh][dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}
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));
  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: TLE

Fixed submission:

#include <bits/stdc++.h>
#define repl(i, l, r) for (int i = l; i < r; ++i)
#define rep(i, n) repl(i, 0, n)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using G = vector<vector<int>>;
const int MOD = 1000000007;
const int INF = 1001001001;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  G dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[ch][cw] = 0;
  q.emplace_back(ch, cw);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    rep(v, 4) {
      int ni = i + di[v];
      int 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_back(ni, nj);
    }
    for (int ei = -2; ei <= 2; ++ei) {
      for (int ej = -2; ej <= 2; ++ej) {
        int ni = i + ei;
        int 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[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using P = pair<int, int>;

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

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw;
  --ch;
  --cw;
  cin >> dh >> dw;
  --dh;
  --dw;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<P> q;
  dist[ch][cw] = 0;
  q.emplace_back(ch, cw);
  while (!q.empty()) {
    int i = q.front().first;  // pairの1個目
    int j = q.front().second; // pairの2個目
    q.pop_front();
    int d = dist[i][j];
    for (int v = 0; v < 4; v++) {
      int ni = i + di[v];
      int 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;
        int 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[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
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 ch, cw;
  cin >> ch >> cw;
  --ch;
  --cw;
  int dh, dw;
  cin >> dh >> dw;
  --dh;
  --dw;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }
  deque<P> q;
  vector<vector<int>> dist(h, vector<int>(w, 10000));
  q.emplace_back(ch, cw);
  dist[ch][cw] = 0;
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];
    for (int v = 0; v < 4; v++) {
      int ni = i + di[v];
      int 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;
        int 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[dh][dw];
  if (ans == 10000) {
    ans = -1;
  }
  cout << ans;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

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

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

ll h, w;
ll sx, sy, gx, gy;
vector<string> maze;

int main() {
  cin >> h >> w >> sy >> sx >> gy >> gx;
  --sx, --sy, --gx, --gy;
  maze.resize(h);
  rep(i, h) cin >> maze[i];

  deque<pair<ll, ll>> q;
  q.push_back({sy, sx});
  vector<ll> d(h * w, -1);
  d[sy * w + sx] = 0;

  while (q.size()) {
    ll y = q.front().first;
    ll x = q.front().second;
    q.pop_front();

    rep(i, 4) {
      ll ny = y + dy[i], nx = x + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && maze[ny][nx] == '.')
        if (d[ny * w + nx] == -1 || d[ny * w + nx] > d[y * w + x])
          d[ny * w + nx] = d[y * w + x], q.push_front({ny, nx});
    }

    for (ll i = y - 2; i <= y + 2; ++i)
      for (ll j = x - 2; j <= x + 2; ++j)
        if (i >= 0 && i < h && j >= 0 && j < w && maze[i][j] == '.')
          if (d[i * w + j] == -1 || d[i * w + j] > d[y * w + x] + 1)
            d[i * w + j] = d[y * w + x] + 1, q.push_back({i, j});
  }

  cout << d[gy * w + gx] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

ll h, w;
ll sx, sy, gx, gy;
vector<string> maze;

int main() {
  cin >> h >> w >> sy >> sx >> gy >> gx;
  --sx, --sy, --gx, --gy;
  maze.resize(h);
  rep(i, h) cin >> maze[i];

  deque<pair<ll, ll>> q;
  q.push_back({sy, sx});
  vector<ll> d(h * w, -1);
  d[sy * h + sx] = 0;

  while (q.size()) {
    ll y = q.front().first;
    ll x = q.front().second;
    q.pop_front();

    rep(i, 4) {
      ll ny = y + dy[i], nx = x + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && maze[ny][nx] == '.')
        if (d[ny * h + nx] == -1 || d[ny * h + nx] > d[y * h + x])
          d[ny * h + nx] = d[y * h + x], q.push_front({ny, nx});
    }

    for (ll i = y - 2; i <= y + 2; ++i)
      for (ll j = x - 2; j <= x + 2; ++j)
        if (i >= 0 && i < h && j >= 0 && j < w && maze[i][j] == '.')
          if (d[i * h + j] == -1 || d[i * h + j] > d[y * h + x] + 1)
            d[i * h + j] = d[y * h + x] + 1, q.push_back({i, j});
  }

  cout << d[gy * h + gx] << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

i64 dh[4] = {-1, 1, 0, 0}, dw[4] = {0, 0, -1, 1};

int main() {
  i64 H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  for (i64 i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<i64>> dis(H, vector<i64>(W, 1e9));
  deque<pair<i64, i64>> que;
  dis[Ch][Cw] = 0;
  que.push_back({Ch, Cw});
  while (que.size()) {
    pair<i64, i64> p = que.front();
    que.pop_front();
    for (i64 i = 0; i < 4; i++) {
      i64 ddh = p.first + dh[i], ddw = p.second + dw[i];
      if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
          dis[ddh][ddw] <= dis[p.first][p.second])
        continue;
      dis[ddh][ddw] = dis[p.first][p.second];
      que.push_front({ddh, ddw});
    }
    for (i64 i = -2; i < 3; i++)
      for (i64 j = -2; j < 3; j++) {
        i64 ddh = p.first + i, ddw = p.second + j;
        if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
            dis[ddh][ddw] <= dis[p.first][p.second] + 1)
          continue;
        dis[ddh][ddw] = dis[p.first][p.second] + 1;
        que.push_back({ddh, ddw});
      }
  }
  cout << (dis[Dh][Dw] == 1e9 ? -1 : dis[Dh][Dw]) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\\n"

i64 dh[4] = {-1, 1, 0, 0}, dw[4] = {0, 0, -1, 1};

int main() {
  i64 H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  vector<string> S(H);
  for (i64 i = 0; i < H; i++)
    cin >> S[i];
  vector<vector<i64>> dis(H, vector<i64>(W, 1e9));
  deque<pair<i64, i64>> que;
  dis[Ch][Cw] = 0;
  que.push_back({Ch, Cw});
  while (que.size()) {
    pair<i64, i64> p = que.front();
    que.pop_front();
    for (i64 i = 0; i < 4; i++) {
      i64 ddh = p.first + dh[i], ddw = p.second + dw[i];
      if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
          dis[ddh][ddw] <= dis[p.first][p.second])
        continue;
      dis[ddh][ddh] = dis[p.first][p.second];
      que.push_front({ddh, ddw});
    }
    for (i64 i = -2; i < 3; i++)
      for (i64 j = -2; j < 3; j++) {
        i64 ddh = p.first + i, ddw = p.second + j;
        if (ddh < 0 || H <= ddh || ddw < 0 || W <= ddw || S[ddh][ddw] == '#' ||
            dis[ddh][ddw] <= dis[p.first][p.second] + 1)
          continue;
        dis[ddh][ddw] = dis[p.first][p.second] + 1;
        que.push_back({ddh, ddw});
      }
  }
  cout << (dis[Dh][Dw] == 1e9 ? -1 : dis[Dh][Dw]) << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;
using P = pair<int, int>;

vector<int> x{1, 0, -1, 0};
vector<int> y{0, 1, 0, -1};
int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> s(h);
  vector<vector<int>> warp(h, vector<int>(w, -1));
  rep(i, h) cin >> s[i];
  warp[ch][cw] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, ch + cw * h));
  int ans = -1;
  while (!que.empty()) {
    P now = que.top();
    int num = now.first;
    int nh = now.second % h;
    int nw = now.second / h;
    que.pop();
    if (nh == dh && nw == dw) {
      ans = num;
      break;
    }
    rep(i, 4) {
      int xh = nh + x[i];
      int xw = nw + y[i];
      if (xh < 0 || xh >= h || xw < 0 || xw >= w)
        continue;
      if (s[xh][xw] == '#')
        continue;
      if (warp[xh][xw] != -1 && warp[xh][xw] <= warp[nh][nw])
        continue;
      warp[xh][xw] = warp[nh][nw];
      que.push(P(num, xh + xw * h));
    }
    rep2(i, -2, 3) rep2(j, -2, 3) {
      int xh = nh + i;
      int xw = nw + j;
      if (xh < 0 || xh >= h || xw < 0 || xw >= w)
        continue;
      if (s[xh][xw] == '#')
        continue;
      if (warp[xh][xw] != -1 && warp[xh][xw] <= warp[nh][nw] + 1)
        continue;
      warp[xh][xw] = warp[nh][nw] + 1;
      que.push(P(num + 1, xh + xw * h));
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;
using P = pair<int, int>;

vector<int> x{1, 0, -1, 0};
vector<int> y{0, 1, 0, -1};
int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> s(h);
  vector<vector<int>> warp(h, vector<int>(w, -1));
  rep(i, h) cin >> s[i];
  warp[ch][cw] = 0;
  queue<P> que;
  que.push(P(ch, cw));
  while (!que.empty()) {
    P now = que.front();
    int nh = now.first;
    int nw = now.second;
    que.pop();
    rep(i, 4) {
      int xh = nh + x[i];
      int xw = nw + y[i];
      if (xh < 0 || xh >= h || xw < 0 || xw >= w)
        continue;
      if (s[xh][xw] == '#')
        continue;
      if (warp[xh][xw] != -1 && warp[xh][xw] <= warp[nh][nw])
        continue;
      warp[xh][xw] = warp[nh][nw];
      que.push(P(xh, xw));
    }
    rep2(i, -2, 3) rep2(j, -2, 3) {
      int xh = nh + i;
      int xw = nw + j;
      if (xh < 0 || xh >= h || xw < 0 || xw >= w)
        continue;
      if (s[xh][xw] == '#')
        continue;
      if (warp[xh][xw] != -1 && warp[xh][xw] <= warp[nh][nw] + 1)
        continue;
      warp[xh][xw] = warp[nh][nw] + 1;
      que.push(P(xh, xw));
    }
  }
  cout << warp[dh][dw] << endl;

  return 0;
}