alexjercan/bugnet huggingface dataset:
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using P = pair<int, int>;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -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.push_back(make_pair(si, sj));
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];
    rep(v, 4) {
      int ni = i + dy[v];
      int nj = j + dx[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.push_front(make_pair(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.push_back(make_pair(ni, nj));
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int h, w;
int ch, cw;
int dh, dw;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

// スタートから見て何番目の領域かを記録
void dfs(vector<string> G, vector<vector<int>> &visited, int nowx, int nowy,
         int loop) {
  visited[nowy][nowx] = loop;
  // cout<<nowx<<" "<<nowy<<" "<<loop<<endl;
  // ループなしで行けるところ
  for (int i = 0; i < 4; i++) {
    int nextx = nowx + dx[i];
    int nexty = nowy + dy[i];
    if (nextx >= 0 && nextx < w && nexty >= 0 && nexty < h &&
        G[nexty][nextx] != '#' && visited[nexty][nextx] == INT_MAX) {
      dfs(G, visited, nextx, nexty, visited[nowy][nowx]);
    }
  }
  // ループで行けるところ
  for (int y = -2; y <= 2; y++) {
    for (int x = -2; x <= 2; x++) {
      if ((x == 0 && y == 0) || (x == 0 && y == 1) || (x == 0 && y == -1) ||
          (x == 1 && y == 0) || (x == -1 && y == 0))
        continue;
      int nextx = nowx + x;
      int nexty = nowy + y;
      if (nextx >= 0 && nextx < w && nexty >= 0 && nexty < h &&
          G[nexty][nextx] != '#' && visited[nexty][nextx] > loop + 1) {
        dfs(G, visited, nextx, nexty, loop + 1);
      }
    }
  }
  return;
}

int main() {
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> G;
  // 最小で何回のループでこれるか、来れないならINF
  vector<vector<int>> visited(h, vector<int>(w, INT_MAX));
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    G.push_back(s);
  }
  dfs(G, visited, cw, ch, 0);
  if (visited[dh][dw] == INT_MAX)
    cout << -1 << endl;
  else
    cout << visited[dh][dw] << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
vector<int> dy({0, -1, 0, 1}), dx({1, 0, -1, 0});

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  --ch, --cw, --dh, --dw;
  vector<string> s(h);
  for (auto &&i : s)
    cin >> i;
  vector<vector<int>> mn(h, vector<int>(w, 2e9));
  mn.at(ch).at(cw) = 0;
  deque<pair<int, int>> q;
  q.push_front({ch, cw});
  while (q.size()) {
    pair<int, int> t = q.front();
    q.pop_front();
    for (int i = 0; i < 4; ++i) {
      int ny = t.first + dy.at(i), nx = t.second + dx.at(i);
      if (ny < 0 || ny >= h || nx < 0 || nx >= w || s.at(ny).at(nx) != '.' ||
          mn.at(ny).at(nx) <= mn.at(t.first).at(t.second))
        continue;
      mn.at(ny).at(nx) = mn.at(t.first).at(t.second);
      q.push_front({ny, nx});
    }
    for (int y = -2; y <= 2; ++y) {
      for (int x = -2; x <= 2; ++x) {
        int ny = t.first + y, nx = t.second + x;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w || s.at(ny).at(nx) != '.' ||
            mn.at(ny).at(nx) <= mn.at(t.first).at(t.second) + 1)
          continue;
        mn.at(ny).at(nx) = mn.at(t.first).at(t.second) + 1;
        q.push_back({ny, nx});
      }
    }
  }
  cout << (mn.at(dh).at(dw) != 2e9 ? mn.at(dh).at(dw) : -1) << "\\n"s;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> dy({0, -1, 0, 1}), dx({1, 0, -1, 0});

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  --ch, --cw, --dh, --dw;
  vector<string> s(h);
  for (auto &&i : s)
    cin >> i;
  vector<vector<int>> mn(h, vector<int>(w, 2e9));
  mn.at(ch).at(cw) = 0;
  queue<pair<int, int>> q;
  q.push({ch, cw});
  while (q.size()) {
    pair<int, int> t = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int ny = t.first + dy.at(i), nx = t.second + dx.at(i);
      if (ny < 0 || ny >= h || nx < 0 || nx >= w || s.at(ny).at(nx) != '.' ||
          mn.at(ny).at(nx) <= mn.at(t.first).at(t.second))
        continue;
      mn.at(ny).at(nx) = mn.at(t.first).at(t.second);
      q.push({ny, nx});
    }
    for (int y = -2; y <= 2; ++y) {
      for (int x = -2; x <= 2; ++x) {
        int ny = t.first + y, nx = t.second + x;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w || s.at(ny).at(nx) != '.' ||
            mn.at(ny).at(nx) <= mn.at(t.first).at(t.second) + 1)
          continue;
        mn.at(ny).at(nx) = mn.at(t.first).at(t.second) + 1;
        q.push({ny, nx});
      }
    }
  }
  cout << (mn.at(dh).at(dw) != 2e9 ? mn.at(dh).at(dw) : -1) << "\\n"s;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> S(H + 4, vector<int>(W + 4));
  int sr, sc;
  cin >> sr >> sc;
  sr++, sc++;
  int gr, gc;
  cin >> gr >> gc;
  gr++, gc++;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      S[i + 2][j + 2] = c == '.';
    }
  }
  vector<vector<int>> dist(H + 4, vector<int>(W + 4, 1e9));
  dist[sr][sc] = 0;
  deque<tuple<int, int, int>> d;
  d.push_back({sr, sc, 0});
  while (d.size()) {
    auto [r, c, x] = d.front();
    d.pop_front();
    if (x != dist[r][c])
      continue;
    int dr[] = {-1, 0, 1, 0};
    int dc[] = {0, -1, 0, 1};
    for (int i = 0; i < 4; i++) {
      int nr = dr[i] + r;
      int nc = dc[i] + c;
      if (!S[nr][nc])
        continue;
      if (dist[nr][nc] > x) {
        dist[nr][nc] = x;
        d.push_front({nr, nc, x});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nr = i + r;
        int nc = j + c;
        if (!S[nr][nc])
          continue;
        if (dist[nr][nc] > x + 1) {
          dist[nr][nc] = x + 1;
          d.push_back({nr, nc, x + 1});
        }
      }
    }
  }
  int res = dist[gr][gc];
  if (res == 1e9)
    res = -1;
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> S(H + 4, vector<int>(W + 4));
  int sr, sc;
  cin >> sr >> sc;
  sr++, sc++;
  int gr, gc;
  cin >> gr >> gc;
  gr++, gc++;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      S[i + 2][j + 2] = c == '.';
    }
  }
  vector<vector<int>> dist(H + 4, vector<int>(W + 4, 1e9));
  dist[sr][sc] = 0;
  deque<tuple<int, int, int>> d;
  d.push_back({sr, sc, 0});
  while (d.size()) {
    auto [r, c, x] = d.front();
    d.pop_front();
    if (x != dist[r][c])
      continue;
    int dr[] = {-1, 0, 1, 0};
    int dc[] = {0, -1, 0, 1};
    for (int i = 0; i < 4; i++) {
      int nr = dr[i] + r;
      int nc = dc[i] + c;
      if (!S[nr][nc])
        continue;
      if (dist[nr][nc] < x) {
        dist[nr][nc] = x;
        d.push_front({nr, nc, x});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nr = i + r;
        int nc = j + c;
        if (!S[nr][nc])
          continue;
        if (dist[nr][nc] < x + 1) {
          dist[nr][nc] = x + 1;
          d.push_back({nr, nc, x + 1});
        }
      }
    }
  }
  cout << dist[gr][gc] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int h, w;
int ch, cw;
int dh, dw;
string s[1005];
int dp[1005][1005];

void init_dp() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = -1;
    }
  }
}

bool pos_ok(int cur_i, int cur_j) {
  if (cur_i < 0 || h <= cur_i) {
    return false;
  }
  if (cur_j < 0 || w <= cur_j) {
    return false;
  }
  if (s[cur_i][cur_j] == '#') {
    return false;
  }
  if (dp[cur_i][cur_j] != -1) {
    return false;
  }
  return true;
}

int main() {
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  cin >> dh >> dw;
  dh--;
  dw--;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  init_dp();

  deque<pair<int, int>> dq;
  deque<int> dist;
  dq.push_back(make_pair(ch, cw));
  dist.push_back(0);
  while (dp[dh][dw] == -1 && !dq.empty()) {
    int cur_i = dq.front().first;
    int cur_j = dq.front().second;
    dq.pop_front();
    int wp = dist.front();
    dist.pop_front();
    dp[cur_i][cur_j] = wp;
    // walk
    int dir_x[4] = {1, 0, -1, 0};
    int dir_y[4] = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int now_i = cur_i + dir_x[i];
      int now_j = cur_j + dir_y[i];
      if (pos_ok(now_i, now_j)) {
        dq.push_front(make_pair(now_i, now_j));
        dist.push_front(wp);
      }
    }
    // warp
    for (int i = cur_i - 2; i <= cur_i + 2; i++) {
      for (int j = cur_j - 2; j <= cur_j + 2; j++) {
        if (pos_ok(i, j)) {
          dq.push_back(make_pair(i, j));
          dist.push_back(wp + 1);
        }
      }
    }
  }

  cout << dp[dh][dw] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int h, w;
int ch, cw;
int dh, dw;
string s[1005];
int dp[1005][1005];

void init_dp() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = -1;
    }
  }
}

void dfs(int cur_i, int cur_j, int wp) {
  if (cur_i < 0 || h <= cur_i) {
    return;
  }
  if (cur_j < 0 || w <= cur_j) {
    return;
  }
  if (s[cur_i][cur_j] == '#') {
    return;
  }
  if (dp[cur_i][cur_j] != -1 && dp[cur_i][cur_j] <= wp) {
    return;
  }
  dp[cur_i][cur_j] = wp;

  // walk
  dfs(cur_i + 1, cur_j, wp);
  dfs(cur_i - 1, cur_j, wp);
  dfs(cur_i, cur_j + 1, wp);
  dfs(cur_i, cur_j - 1, wp);

  // warp
  for (int i = cur_i - 2; i <= cur_i + 2; i++) {
    for (int j = cur_j - 2; j <= cur_j + 2; j++) {
      dfs(i, j, wp + 1);
    }
  }
  return;
}

int main() {
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  cin >> dh >> dw;
  dh--;
  dw--;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  init_dp();
  dfs(ch, cw, 0);

  cout << dp[dh][dw] << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

/*input
4 4
1 1
4 4
..#.
..#.
.#..
.#..
*/

#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();
    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:

/*input
4 4
1 1
4 4
..#.
..#.
.#..
.#..
*/

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

struct pos {
  int x, y, c;
};

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int INF = 1001001001;
const int H = 1005;
const int W = 1005;

int h, w, ch, cw, dh, dw;
vector<string> s(H);
int d[H][W];
queue<pos> que;

bool paint(int x, int y, int c) {
  // printf("%d %d %d\\n", x, y, c);
  if (x < 0 || h <= x || y < 0 || w <= y || s[x][y] == '#' || c >= d[x][y]) {
    return false;
  }
  // puts("ok");
  d[x][y] = c;
  que.push(pos{x, y, c});
  return true;
}

int main() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  rep(i, h) cin >> s[i];
  // rep(i, h) cout << s[i] << endl;

  rep(i, h) rep(j, w) d[i][j] = INF;
  que.push(pos{ch, cw, 0});
  d[ch][cw] = 0;
  while (!que.empty()) {
    pos p = que.front();
    que.pop();
    rep(i, 4) { paint(p.x + dx[i], p.y + dy[i], p.c); }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        paint(p.x + i, p.y + j, p.c + 1);
      }
    }
  }
  // rep(i, h) {
  //     rep(j, w) cout << d[i][j] << " ";
  //     puts("");
  // }
  if (d[dh][dw] == INF)
    puts("-1");
  else
    cout << d[dh][dw] << endl;
}
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 all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
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];
  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();
    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:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define len(x) (ll)(x).length()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
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];
  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();
    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_front(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int h, w;
int m[1000][1000];

struct Item {
  Item(int x_, int y_, int c_) : x(x_), y(y_), c(c_) {}
  int x;
  int y;
  int c;
};

bool is_not_road(int x, int y) {
  return x < 0 || x >= h || y < 0 || y >= w || m[x][y] < 0;
}

int main() {
  int cx, cy, dx, dy;
  cin >> h >> w;
  cin >> cx >> cy;
  cin >> dx >> dy;
  cx--;
  cy--;
  dx--;
  dy--;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      m[i][j] = (c == '.') ? 0 : -1;
    }
  }

  deque<Item> q;
  q.emplace_front(cx, cy, 1);

  while (!q.empty()) {
    Item item = q.front();
    q.pop_front();
    int x = item.x;
    int y = item.y;
    int c = item.c;

    if (is_not_road(x, y) || (m[x][y] > 0 && m[x][y] <= c))
      continue;
    m[x][y] = c;
    q.emplace_front(x - 1, y, c);
    q.emplace_front(x + 1, y, c);
    q.emplace_front(x, y - 1, c);
    q.emplace_front(x, y + 1, c);

    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (abs(i) + abs(j) <= 1)
          continue;
        q.emplace_back(x + i, y + j, c + 1);
      }
    }
  }

  if (m[dx][dy] > 0)
    cout << m[dx][dy] - 1 << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int h, w;
int m[1000][1000];

struct Item {
  Item(int x_, int y_, int c_) : x(x_), y(y_), c(c_) {}
  int x;
  int y;
  int c;
};

bool is_not_road(int x, int y) {
  return x < 0 || x >= h || y < 0 || y >= w || m[x][y] < 0;
}

int main() {
  int cx, cy, dx, dy;
  cin >> h >> w;
  cin >> cx >> cy;
  cin >> dx >> dy;
  cx--;
  cy--;
  dx--;
  dy--;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      m[i][j] = (c == '.') ? 0 : -1;
    }
  }

  queue<Item> q;
  q.emplace(cx, cy, 1);

  while (!q.empty()) {
    Item item = q.front();
    q.pop();
    int x = item.x;
    int y = item.y;
    int c = item.c;

    if (is_not_road(x, y) || (m[x][y] > 0 && m[x][y] <= c))
      continue;
    m[x][y] = c;
    q.emplace(x - 1, y, c);
    q.emplace(x + 1, y, c);
    q.emplace(x, y - 1, c);
    q.emplace(x, y + 1, c);

    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (abs(i) + abs(j) <= 1)
          continue;
        q.emplace(x + i, y + j, c + 1);
      }
    }
  }

  if (m[dx][dy] > 0)
    cout << m[dx][dy] - 1 << 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++)
typedef long long ll;
using namespace std;
using T = tuple<int, int, int>;
const int INF = 1e9;

struct Loc {
  int dyx;
  int y;
  int x;

  Loc(T t) {
    dyx = get<0>(t);
    y = get<1>(t);
    x = get<2>(t);
  }
};

int main() {
  int h, w, cy, cx, dy, dx;
  cin >> h >> w >> cy >> cx >> dy >> dx;
  char s[h][w];
  rep(i, h) rep(j, w) cin >> s[i][j];
  cy--, cx--, dy--, dx--;

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

  priority_queue<T> q;
  q.push({0, cy, cx});

  while (!q.empty()) {
    Loc loc = Loc(q.top());
    q.pop();
    if (loc.dyx > d[loc.y][loc.x])
      continue;

    for (int i = -2; i <= 2; i++) {
      int y = loc.y + i;
      if (y < 0 || y >= h)
        continue;

      for (int j = -2; j <= 2; j++) {
        int x = loc.x + j;
        if (x < 0 || x >= w)
          continue;
        if (s[y][x] == '#')
          continue;

        int dt = d[loc.y][loc.x] + (abs(i) + abs(j) > 1 ? 1 : 0);
        if (dt < d[y][x]) {
          d[y][x] = dt;
          q.push({-dt, y, x});
        }
      }
    }
  }

  cout << (d[dy][dx] != INF ? d[dy][dx] : -1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
using T = tuple<int, int, int>;
const int INF = 1e9;

struct Loc {
  int dyx;
  int y;
  int x;

  Loc(T t) {
    dyx = get<0>(t);
    y = get<1>(t);
    x = get<2>(t);
  }
};

int main() {
  int h, w, cy, cx, dy, dx;
  cin >> h >> w >> cy >> cx >> dy >> dx;
  char s[h][w];
  rep(i, h) rep(j, w) cin >> s[i][j];
  cy--, cx--, dy--, dx--;

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

  priority_queue<T> q;
  q.push({0, cy, cx});

  while (!q.empty()) {
    Loc loc = Loc(q.top());
    q.pop();
    if (loc.dyx < d[loc.y][loc.x])
      continue;

    for (int i = -2; i <= 2; i++) {
      int y = loc.y + i;
      if (y < 0 || y >= h)
        continue;

      for (int j = -2; j <= 2; j++) {
        int x = loc.x + j;
        if (x < 0 || x >= w)
          continue;
        if (s[y][x] == '#')
          continue;

        int dt = d[loc.y][loc.x] + (abs(i) + abs(j) > 1 ? 1 : 0);
        if (dt < d[y][x]) {
          d[y][x] = dt;
          q.push({dt, y, x});
        }
      }
    }
  }

  cout << (d[dy][dx] != INF ? d[dy][dx] : -1) << endl;
  return 0;
}