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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000003

int main() {
  int h, w, sx, sy, ex, ey, i, j, x, y;
  pair<int, int> t;
  cin >> h >> w;
  cin >> sx >> sy;
  cin >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  vector<vector<char>> board(h, vector<char>(w));

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

  vector<vector<int>> dist(h, vector<int>(w));
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      dist[i][j] = 1e7;

  deque<pair<int, int>> q;
  q.push_front({sx, sy});
  dist[sx][sy] = 0;
  int movex[] = {0, -1, 0, 1};
  int movey[] = {-1, 0, 1, 0};
  while (!q.empty()) {
    t = q.front();
    q.pop_front();

    for (i = 0; i < 4; i++) {
      x = t.first + movex[i];
      y = t.second + movey[i];
      if (x < 0 || x >= h || y < 0 || y >= w || board[x][y] == '#')
        continue;
      if (dist[x][y] > dist[t.first][t.second]) {
        dist[x][y] = dist[t.first][t.second];
        q.push_front({x, y});
      }
    }

    for (i = t.first - 2; i <= t.first + 2; i++) {
      for (j = t.second - 2; j <= t.second + 2; j++) {
        if (i < 0 || i >= h || j < 0 || j >= w ||
            (i == t.first && j == t.second) || board[i][j] == '#')
          continue;
        if (dist[i][j] > dist[t.first][t.second] + 1) {
          dist[i][j] = dist[t.first][t.second] + 1;
          q.push_back({i, j});
        }
      }
    }
  }

  int ans = (dist[ex][ey] >= 1e7) ? -1 : dist[ex][ey];
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000003

int main() {
  int h, w, sx, sy, ex, ey, i, j, x, y;
  pair<int, int> t;
  cin >> h >> w;
  cin >> sx >> sy;
  cin >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  vector<vector<char>> board(h, vector<char>(w));

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

  vector<vector<int>> dist(h, vector<int>(w, 1e7));
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dist[sx][sy] = 0;
  int movex[] = {0, -1, 0, 1};
  int movey[] = {-1, 0, 1, 0};
  while (!q.empty()) {
    t = q.front();
    q.pop();

    for (i = 0; i < 4; i++) {
      x = t.first + movex[i];
      y = t.second + movey[i];
      if (x < 0 || x >= h || y < 0 || y >= w || board[x][y] == '#')
        continue;
      if (dist[x][y] > dist[t.first][t.second]) {
        dist[x][y] = dist[t.first][t.second];
        q.push({x, y});
      }
    }

    for (i = t.first - 2; i <= t.first + 2; i++) {
      for (j = t.second - 2; j <= t.second + 2; j++) {
        if (i < 0 || i >= h || j < 0 || j >= w ||
            (i == t.first && j == t.second) || board[i][j] == '#')
          continue;
        if (dist[i][j] > dist[t.first][t.second] + 1) {
          dist[i][j] = dist[t.first][t.second] + 1;
          q.push({i, j});
        }
      }
    }
  }

  int ans = (dist[ex][ey] < 1e7) ? dist[ex][ey] : -1;
  cout << ans << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> P;
const int INF = 10000000;

int mh[4] = {-1, 0, 1, 0};
int mw[4] = {0, -1, 0, 1};

void Main() {
  int H, W;
  cin >> H >> W;

  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  char S[H][W];
  int d[H][W];
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      cin >> S[h][w];
      d[h][w] = INF;
    }
  }

  d[Ch][Cw] = 0;
  deque<P> q;
  q.push_front(P(Ch, Cw));
  while (!q.empty()) {
    P p = q.front();
    q.pop_front();
    int h = p.first, w = p.second;
    for (int i = 0; i < 4; i++) {
      int nh = h + mh[i], nw = w + mw[i];
      if (nh >= 0 && nh < H && nw >= 0 && nw < W && S[nh][nw] == '.') {
        if (d[nh][nw] > d[h][w]) {
          d[nh][nw] = d[h][w];
          q.push_front(P(nh, nw));
        }
      }
    }

    for (int y = -2; y <= 2; y++) {
      for (int x = -2; x <= 2; x++) {
        int nh = h + y, nw = w + x;
        if (nh >= 0 && nh < H && nw >= 0 && nw < W && S[nh][nw] == '.' &&
            d[nh][nw] == INF) {
          if (d[nh][nw] - d[h][w] > 1) {
            d[nh][nw] = d[h][w] + 1;
            q.push_back(P(nh, nw));
          }
        }
      }
    }
  }

  if (d[Dh][Dw] == INF)
    cout << -1 << endl;
  else
    cout << d[Dh][Dw] << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

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

typedef pair<int, int> P;
const int INF = 10000000;

int mh[4] = {-1, 0, 1, 0};
int mw[4] = {0, -1, 0, 1};

void Main() {
  int H, W;
  cin >> H >> W;

  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  char S[H][W];
  int d[H][W];
  for (int h = 0; h < H; h++) {
    for (int w = 0; w < W; w++) {
      cin >> S[h][w];
      d[h][w] = INF;
    }
  }

  d[Ch][Cw] = 0;
  queue<P> q;
  q.push(P(Ch, Cw));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int h = p.first, w = p.second;
    for (int i = 0; i < 4; i++) {
      if (h + mh[i] >= 0 && h + mh[i] < H && w + mw[i] >= 0 && w + mw[i] < W &&
          S[h + mh[i]][w + mw[i]] == '.') {
        if (d[h + mh[i]][w + mw[i]] > d[h][w]) {
          d[h + mh[i]][w + mw[i]] = d[h][w];
          q.push(P(h + mh[i], w + mw[i]));
        }
      }
    }

    map<int, map<int, int>> m;
    for (int y = -2; y <= 2; y++) {
      for (int x = -2; x <= 2; x++) {
        int nh = h + y, nw = w + x;
        if (nh >= 0 && nh < H && nw >= 0 && nw < W && S[nh][nw] == '.') {
          if (d[nh][nw] - d[h][w] > 1) {
            d[nh][nw] = d[h][w] + 1;
            m[nh][nw]++;
          }
        }
      }
    }
    for (auto mm : m) {
      for (auto mmm : mm.second) {
        q.push(P(mm.first, mmm.first));
      }
    }
  }
  if (d[Dh][Dw] == INF)
    cout << -1 << endl;
  else
    cout << d[Dh][Dw] << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e3 + 111;
const int inf = 0x3f3f3f3f;
struct node {
  int x, y;
};
int h, w, times[maxn][maxn];
int Ci, Cj, Di, Dj;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
string mp[maxn];

int bfs() {
  deque<node> q;
  q.push_front({Ci, Cj});
  times[Ci][Cj] = 0;
  while (!q.empty()) {
    node cnt = q.front();
    q.pop_front();
    int x = cnt.x, y = cnt.y;
    if (x == Di && y == Dj)
      return times[x][y];
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (mp[nx][ny] == '.' &&
          (times[nx][ny] == -1 || times[nx][ny] > times[x][y])) {
        times[nx][ny] = times[x][y];
        q.push_front({nx, ny});
      }
    }
    int sx = x - 2, sy = y - 2;
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        int nx = i + sx, ny = j + sy;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (mp[nx][ny] == '.' && times[nx][ny] == -1) {
          times[nx][ny] = times[x][y] + 1;
          q.push_back({nx, ny});
        }
      }
    }
  }
  return -1;
}

int main() {
  memset(times, -1, sizeof(times));
  scanf("%d%d", &h, &w);
  scanf("%d%d", &Ci, &Cj);
  Ci--, Cj--;
  scanf("%d%d", &Di, &Dj);
  Di--, Dj--;
  for (int i = 0; i < h; i++) {
    cin >> mp[i];
  }
  printf("%d", bfs());
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e3 + 111;
const int inf = 0x3f3f3f3f;
struct node {
  int x, y, k;
};
int h, w, times[maxn][maxn];
int Ci, Cj, Di, Dj;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
string mp[maxn];

int main() {
  memset(times, inf, sizeof(times));
  scanf("%d%d", &h, &w);
  scanf("%d%d", &Ci, &Cj);
  Ci--, Cj--;
  scanf("%d%d", &Di, &Dj);
  Di--, Dj--;
  for (int i = 0; i < h; i++) {
    cin >> mp[i];
  }
  queue<node> q;
  q.push({Ci, Cj, 0});
  while (!q.empty()) {
    node cnt = q.front();
    q.pop();
    int x = cnt.x, y = cnt.y, k = cnt.k;
    //        printf("%d %d %d\\n", x, y, k);
    if (k > times[x][y])
      continue;
    times[x][y] = k;
    int fg = 0;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || mp[nx][ny] == '#') {
        fg = 1;
        continue;
      }
      if (times[nx][ny] > k)
        q.push({nx, ny, k}), times[nx][ny] = k + 1;
    }
    if (fg) {
      int sx = x - 2, sy = y - 2;
      for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
          int nx = sx + i, ny = sy + j;
          if (nx < 0 || nx >= h || ny < 0 || ny >= w || mp[nx][ny] == '#')
            continue;
          if (times[nx][ny] > k + 1)
            q.push({nx, ny, k + 1}), times[nx][ny] = k + 1;
        }
      }
    }
  }
  if (times[Di][Dj] == inf)
    printf("-1");
  else
    printf("%d", times[Di][Dj]);
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
int INF = 1e9;

int dp[1005][1005];
char maze[1005][1005];

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  rep(i, 1005) rep(j, 1005) dp[i][j] = INF;
  dp[ch][cw] = 0;
  rep(i, h) rep(j, w) cin >> maze[i][j];
  priority_queue<P, vector<P>, greater<P>> q;
  q.emplace(0, ch * w + cw);
  while (!q.empty()) {
    P p = q.top();
    if (p.first > dp[dh][dw])
      break;
    int y = p.second / w;
    int x = p.second % w;
    // cout << p.first << endl;
    q.pop();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int ny = y + i;
        int nx = x + j;
        if (ny < 0 || nx < 0 || h <= ny || w <= nx)
          continue;
        if (maze[ny][nx] == '#')
          continue;
        if ((abs(i) == 1 && j == 0) || (abs(j) == 1 && i == 0)) {
          if (dp[ny][nx] > dp[y][x]) {
            dp[ny][nx] = dp[y][x];
            q.emplace(p.first, w * ny + nx);
          }
        } else {
          if (dp[ny][nx] > dp[y][x] + 1) {
            dp[ny][nx] = dp[y][x] + 1;
            q.emplace(p.first + 1, w * ny + nx);
          }
        }
      }
  }

  if (dp[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
int INF = 1e9;

int dp[1005][1005];
char maze[1005][1005];

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  rep(i, 1005) rep(j, 1005) dp[i][j] = INF;
  dp[ch][cw] = 0;
  rep(i, h) rep(j, w) cin >> maze[i][j];
  queue<int> q;
  q.push(ch * w + cw);
  while (!q.empty()) {
    int y = q.front() / w;
    int x = q.front() % w;
    q.pop();
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int ny = y + i;
        int nx = x + j;
        if (ny < 0 || nx < 0 || h <= ny || w <= nx)
          continue;
        if (maze[ny][nx] == '#')
          continue;
        if ((abs(i) == 1 && j == 0) || (abs(j) == 1 && i == 0)) {
          if (dp[ny][nx] > dp[y][x]) {
            dp[ny][nx] = dp[y][x];
            q.push(w * ny + nx);
          }
        } else {
          if (dp[ny][nx] > dp[y][x] + 1) {
            dp[ny][nx] = dp[y][x] + 1;
            q.push(w * ny + nx);
          }
        }
      }
  }

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

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

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m;
  cin >> n >> m;

  int x, y;
  cin >> x >> y;

  int t_x, t_y;
  cin >> t_x >> t_y;
  x--, y--;
  t_x--, t_y--;

  vector<vector<char>> grid(n, vector<char>(m));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> grid[i][j];

  vector<vector<long long>> sp(n, vector<long long>(m, -1));
  priority_queue<pair<long long, pair<int, int>>> pq;
  pq.push({0, {x, y}});

  vector<int> X0 = {1, -1, 0, 0};
  vector<int> Y0 = {0, 0, 1, -1};

  vector<int> X1 = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                    0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  vector<int> Y1 = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                    2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  while (pq.empty() ^ 1) {
    int cx = pq.top().second.first;
    int cy = pq.top().second.second;
    int cost = pq.top().first * -1;

    pq.pop();
    if (cx < 0 || cx >= n || cy < 0 || cy >= m || grid[cx][cy] == '#' ||
        sp[cx][cy] != -1)
      continue;
    sp[cx][cy] = cost;
    if (cx == t_x && cy == t_y) {
      cout << cost;
      return 0;
    }
    for (int i = 0; i < 4; ++i)
      pq.push({-cost, {cx + X0[i], cy + Y0[i]}});

    for (int i = 0; i < 20; ++i)
      pq.push({-(cost + 1), {cx + X1[i], cy + Y1[i]}});
  };

  cout << sp[t_x][t_y];
}


Error: TLE

Fixed submission:

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

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, m;
  cin >> n >> m;

  int x, y;
  cin >> x >> y;

  int t_x, t_y;
  cin >> t_x >> t_y;
  x--, y--;
  t_x--, t_y--;

  vector<vector<char>> grid(n, vector<char>(m));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      cin >> grid[i][j];

  vector<vector<long long>> sp(n, vector<long long>(m, -1));
  priority_queue<pair<long long, pair<int, int>>> pq;
  pq.push({0, {x, y}});

  vector<int> X0 = {1, -1, 0, 0};
  vector<int> Y0 = {0, 0, 1, -1};

  vector<int> X1 = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                    0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  vector<int> Y1 = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                    2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  while (pq.empty() ^ 1) {
    int cx = pq.top().second.first;
    int cy = pq.top().second.second;
    int cost = pq.top().first * -1;

    pq.pop();
    if (cx < 0 || cx >= n || cy < 0 || cy >= m || grid[cx][cy] == '#' ||
        sp[cx][cy] != -1)
      continue;
    sp[cx][cy] = cost;

    for (int i = 0; i < 4; ++i)
      pq.push({-cost, {cx + X0[i], cy + Y0[i]}});

    for (int i = 0; i < 20; ++i)
      pq.push({-(cost + 1), {cx + X1[i], cy + Y1[i]}});
  };

  cout << sp[t_x][t_y];
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define INF 1000000000

struct Node {
  int i;
  int j;
  int d;
};
bool operator<(const Node &m, const Node &n) { return m.d > n.d; }

int H, W, sh, sw, gh, gw;
char S[1001][1001];
int D[1001][1001];
int dh[4]{0, 1, 0, -1};
int dw[4]{1, 0, -1, 0};

int cannot_move(Node &n) {
  if (n.i < 0 || H <= n.i)
    return true;
  if (n.j < 0 || W <= n.j)
    return true;
  if (S[n.i][n.j] == '#')
    return true;
  if (D[n.i][n.j] <= n.d)
    return true;
  return false;
}

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

  REP(i, H) REP(j, W) cin >> S[i][j];
  REP(i, H) REP(j, W) D[i][j] = INF;

  priority_queue<Node> Q;
  D[sh][sw] = 0;
  Q.push(Node{sh, sw, 0});

  while (!Q.empty()) {
    Node m = Q.top();
    Q.pop();
    if (D[m.i][m.j] < m.d)
      continue;

    // cout << endl;
    // REP(i, H) { REP(j, W) { printf("%10d ", D[i][j]); } cout << endl; }

    REP(i, 4) {
      Node n{m.i + dh[i], m.j + dw[i], m.d};
      if (cannot_move(n))
        continue;
      D[n.i][n.j] = n.d;
      Q.push(n);
    }
    REP(i, 5) REP(j, 5) {
      if (i == 2 && j == 2)
        continue;
      Node n{m.i + i - 2, m.j + j - 2, m.d + 1};
      if (cannot_move(n))
        continue;
      D[n.i][n.j] = n.d;
      Q.push(n);
    }
  }

  cout << (D[gh][gw] != INF ? D[gh][gw] : -1) << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define INF 1000000000

struct Node {
  int i;
  int j;
  int d;
};
bool operator<(const Node &m, const Node &n) { return m.d >= n.d; }

int H, W, sh, sw, gh, gw;
char S[1001][1001];
int D[1001][1001];
int dh[4]{0, 1, 0, -1};
int dw[4]{1, 0, -1, 0};

int cannot_move(Node &n) {
  bool res = false;
  res |= n.i < 0 || H <= n.i;
  res |= n.j < 0 || W <= n.j;
  res |= S[n.i][n.j] == '#';
  res |= D[n.i][n.j] <= n.d;
  return res;
}

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

  REP(i, H) REP(j, W) cin >> S[i][j];
  REP(i, H) REP(j, W) D[i][j] = INF;

  priority_queue<Node> Q;
  D[sh][sw] = 0;
  Q.push(Node{sh, sw, 0});

  while (!Q.empty()) {
    Node m = Q.top();
    Q.pop();
    if (D[m.i][m.j] < m.d)
      continue;

    // cout << endl;
    // REP(i, H) { REP(j, W) { printf("%10d ", D[i][j]); } cout << endl; }

    REP(i, 4) {
      Node n{m.i + dh[i], m.j + dw[i], m.d};
      if (cannot_move(n))
        continue;
      D[n.i][n.j] = n.d;
      Q.push(n);
    }
    REP(i, 5) REP(j, 5) {
      if (i == 2 && j == 2)
        continue;
      Node n{m.i + i - 2, m.j + j - 2, m.d + 1};
      if (cannot_move(n))
        continue;
      D[n.i][n.j] = n.d;
      Q.push(n);
    }
  }

  cout << (D[gh][gw] != INF ? D[gh][gw] : -1) << endl;

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

#include <deque>
#include <iostream>
using namespace std;
typedef pair<int, int> i_i;
const int dh[4] = {-1, 1, 0, 0};
const int dw[4] = {0, 0, -1, 1};
const int INF = 10000000;
int H, W, sh, sw, th, tw;
char field[1000][1000];
int dp[1000][1000];

void solve() {
  // 幅優先探索
  deque<i_i> deq;
  deq.emplace_back(sh, sw);
  dp[sh][sw] = 0;
  while (!deq.empty()) {
    i_i now = deq.front();
    deq.pop_front();
    int ch = now.first;
    int cw = now.second;
    int cd = dp[ch][cw];
    // 歩いて移動する
    for (int i = 0; i < 4; i++) {
      int nh = ch + dh[i];
      int nw = cw + dw[i];
      if (0 > nh || nh >= H || 0 > nw || nw >= W || field[nh][nw] == '#') {
        continue;
      }
      if (dp[nh][nw] > cd) {
        dp[nh][nw] = cd;
        deq.emplace_front(nh, nw);
      }
    }
    // ワープして移動する
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = ch + i;
        int nw = cw + j;
        if (0 > nh || nh >= H || 0 > nw || nw >= W || field[nh][nw] == '#') {
          continue;
        }
        if (dp[nh][nw] > cd + 1) {
          dp[nh][nw] = cd + 1;
          deq.emplace_back(nh, nw); // 　後方にpushする
        }
      }
    }
  }

  if (dp[th][tw] == INF)
    cout << "-1";
  else
    cout << dp[th][tw];
}

int main() {

  cin >> H >> W >> sh >> sw >> th >> tw;
  sh--;
  sw--;
  th--;
  tw--;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> field[i][j];
      dp[i][j] = INF;
    }
  }

  solve();
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
int H, W, CH, CW, DH, DW;
char S[1001][1001];
int dp[1001][1001]; // i,jへの最小コスト
const int INF = 10000000;
const int dy[4] = {-1, 0, 1, 0};
const int dx[4] = {0, -1, 0, 1};

struct point {
  int h, w;
};

void bfs(int sh, int sw, int sc) {
  // キュー
  queue<point> que;
  dp[sh][sw] = 0;
  que.push((point){sh, sw});
  while (!que.empty()) {
    point p = que.front();
    que.pop();
    int d = dp[p.h][p.w];
    // 四方向に歩く
    for (int i = 0; i < 4; i++) {
      int nh = p.h + dy[i];
      int nw = p.w + dx[i];
      // 画面外か壁なら歩けない
      if (0 <= nh && nh < H && 0 <= nw && nw < W && S[nh][nw] == '.') {
        // コストを更新できるならプッシュ
        if (d < dp[nh][nw]) {
          dp[nh][nw] = d;
          que.push((point){nh, nw});
        }
      }
    }

    // ワープする
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = p.h + i;
        int nw = p.w + j;
        // 画面外か壁なら歩けない
        if (0 <= nh && nh < H && 0 <= nw && nw < W && S[nh][nw] == '.') {
          // コストを更新できるならプッシュ
          if (d + 1 < dp[nh][nw]) {
            dp[nh][nw] = d + 1;
            que.push((point){nh, nw});
          }
        }
      }
    }
  }
}

int main() {
  cin >> H >> W >> CH >> CW >> DH >> DW;
  CH--;
  CW--;
  DH--;
  DW--; // 0オリジンとする
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> S[i][j];
      dp[i][j] = INF;
    }
  }

  bfs(CH, CW, 0);
  if (dp[DH][DW] == INF)
    cout << -1;
  else
    cout << dp[DH][DW];
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <deque>
#include <iostream>
using namespace std;
int H, W, CH, CW, DH, DW;
char S[1001][1001];
int dp[1001][1001]; // i,jへの最小コスト
const int INF = 10000000;
const int dy[4] = {-1, 0, 1, 0};
const int dx[4] = {0, -1, 0, 1};

struct point {
  int h, w;
};

void bfs(int sh, int sw) {
  // キュー
  deque<point> que;
  dp[sh][sw] = 0;
  que.emplace_back((point){sh, sw});
  while (!que.empty()) {
    point p = que.front();
    que.pop_front();
    int d = dp[p.h][p.w];
    // 四方向に歩く
    for (int i = 0; i < 4; i++) {
      int nh = p.h + dy[i];
      int nw = p.w + dx[i];
      // 画面外か壁なら歩けない
      if (0 <= nh && nh < H && 0 <= nw && nw < W && S[nh][nw] == '.') {
        // コストを更新できるならプッシュ
        if (d < dp[nh][nw]) {
          dp[nh][nw] = d;
          que.emplace_front((point){nh, nw});
        }
      }
    }

    // ワープする
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = p.h + i;
        int nw = p.w + j;
        // 画面外か壁なら歩けない
        if (0 <= nh && nh < H && 0 <= nw && nw < W && S[nh][nw] == '.') {
          // コストを更新できるならプッシュ
          if (d + 1 < dp[nh][nw]) {
            dp[nh][nw] = d + 1;
            que.emplace_back((point){nh, nw});
          }
        }
      }
    }
  }
}

int main() {
  cin >> H >> W >> CH >> CW >> DH >> DW;
  CH--;
  CW--;
  DH--;
  DW--; // 0オリジンとする
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> S[i][j];
      dp[i][j] = INF;
    }
  }

  bfs(CH, CW);
  if (dp[DH][DW] == INF)
    cout << -1;
  else
    cout << dp[DH][DW];
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int H, W, Ch, Dh, Cw, Dw;
char S[1001][1001];
int dp[1001][1001]; // i, j に到達するための最小ワープ回数
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};
const int INF = 100000000;

void bfs(int y, int x, int w) {
  dp[y][x] = w;
  // 歩く
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    // 壁でない時
    if (0 < ny && ny <= H && 0 < nx && nx <= W) {
      if (S[ny][nx] == '.' && dp[y][x] < dp[ny][nx]) {
        bfs(ny, nx, w);
      }
    }
  }
  // ワープする
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (i == 0 && j == 0)
        continue;
      int ny = y + i;
      int nx = x + j;
      // 壁でない時
      if (0 < ny && ny <= H && 0 < nx && nx <= W) {
        if (S[ny][nx] == '.' && dp[y][x] + 1 < dp[ny][nx]) {
          bfs(ny, nx, w + 1);
        }
      }
    }
  }
}

int main() {
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> S[i][j];
      dp[i][j] = INF;
    }
  }

  bfs(Ch, Cw, 0);
  if (dp[Dh][Dw] == INF)
    cout << -1;
  else
    cout << dp[Dh][Dw];
}