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

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

constexpr int H_MAX = 1001;
constexpr int W_MAX = 1001;
char s[H_MAX][W_MAX] = {};
int dp[H_MAX][W_MAX] = {};
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
constexpr int UNREACHABLE = 1000000000;

struct Point {
  int x;
  int y;
  int n;
};

int main() {
  int h, w, ch, cw, dh, dw;
  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] = UNREACHABLE;
    }
  }
  auto cmp = [](Point &a, Point &b) { return a.n > b.n; };
  priority_queue<Point, vector<Point>, decltype(cmp)> pq(cmp);
  pq.push({ch, cw, 0});
  dp[ch][cw] = 0;
  while (!pq.empty()) {
    auto pos = pq.top();
    pq.pop();
    // 歩いて移動
    for (int i = 0; i < 4; ++i) {
      int x = pos.x + dx[i];
      int y = pos.y + dy[i];
      if (x < 1 || x > h || y < 1 || y > w)
        continue;
      if (s[x][y] == '.' && dp[x][y] > dp[pos.x][pos.y]) {
        dp[x][y] = dp[pos.x][pos.y];
        pq.push({x, y, dp[x][y]});
      }
    }
    // ワープ移動
    for (int i = max(1, pos.x - 2); i <= min(h, pos.x + 2); ++i) {
      for (int j = max(1, pos.y - 2); j <= min(w, pos.y + 2); ++j) {
        if (s[i][j] == '.' && dp[i][j] > dp[pos.x][pos.y] + 1) {
          dp[i][j] = dp[pos.x][pos.y] + 1;
          pq.push({i, j, dp[i][j]});
        }
      }
    }
  }
  if (dp[dh][dw] == UNREACHABLE) {
    cout << -1;
  } else {
    cout << dp[dh][dw];
  }
  return 0;
}


Error: TLE

Fixed submission:

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

constexpr int H_MAX = 1001;
constexpr int W_MAX = 1001;
char s[H_MAX][W_MAX] = {};
int dp[H_MAX][W_MAX] = {};
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
constexpr int UNREACHABLE = 1000000000;

int main() {
  int h, w, ch, cw, dh, dw;
  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] = UNREACHABLE;
    }
  }
  queue<pair<int, int>> st;
  st.push({ch, cw});
  dp[ch][cw] = 0;
  while (!st.empty()) {
    auto pos = st.front();
    st.pop();
    // 歩いて移動
    for (int i = 0; i < 4; ++i) {
      int x = pos.first + dx[i];
      int y = pos.second + dy[i];
      if (x < 1 || x > h || y < 1 || y > w)
        continue;
      if (s[x][y] == '.' && dp[x][y] > dp[pos.first][pos.second]) {
        dp[x][y] = dp[pos.first][pos.second];
        st.push({x, y});
      }
    }
    // ワープ移動
    for (int i = max(1, pos.first - 2); i <= min(h, pos.first + 2); ++i) {
      for (int j = max(1, pos.second - 2); j <= min(w, pos.second + 2); ++j) {
        if (s[i][j] == '.' && dp[i][j] > dp[pos.first][pos.second] + 1) {
          dp[i][j] = dp[pos.first][pos.second] + 1;
          st.push({i, j});
        }
      }
    }
  }
  if (dp[dh][dw] == UNREACHABLE) {
    cout << -1;
  } else {
    cout << dp[dh][dw];
  }
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
char s[1010][1010];
struct node {
  int h, w, v;
};
int dh[4] = {0, 0, 1, -1};
int dw[4] = {1, -1, 0, 0};
int vis[1010][1010];
int main() {
  int h, w, ch, cw, eh, ew;
  cin >> h >> w >> ch >> cw >> eh >> ew;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        vis[i][j] = 1;
    }
  }
  deque<node> dq;
  dq.push_front(node{ch, cw, 0});
  vis[ch][cw] = 1;
  int ans = 1e9;
  while (!dq.empty()) {
    node te = dq.front();
    dq.pop_front();
    // cout<<te.h<<" "<<te.w<<" "<<te.v<<endl;
    if (te.h == eh && te.w == ew) {
      ans = te.v;
      break;
    }
    vis[te.h][te.w] = 1;
    for (int i = 0; i < 4; i++) {
      int _h = dh[i] + te.h;
      int _w = dw[i] + te.w;
      if (_h >= 1 && _h <= h && _w >= 1 && _w <= w && !vis[_h][_w]) {
        // vis[_h][_w]=1;
        // cout<<"  "<<_h<<" "<<_w<<endl;
        dq.push_front(node{_h, _w, te.v});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int _h = te.h + i;
        int _w = te.w + j;
        if (i != 0 || j != 0) {
          if (_h >= 1 && _h <= h && _w >= 1 && _w <= w && !vis[_h][_w]) {
            // vis[_h][_w]=1;
            dq.push_back(node{_h, _w, te.v + 1});
          }
        }
      }
    }
  }
  if (ans == 1e9)
    cout << -1;
  else
    cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char s[1010][1010];
struct node {
  int h, w, v;
};
int dh[4] = {0, 0, 1, -1};
int dw[4] = {1, -1, 0, 0};
int vis[1010][1010];
int main() {
  int h, w, ch, cw, eh, ew;
  cin >> h >> w >> ch >> cw >> eh >> ew;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        vis[i][j] = 1;
    }
  }
  deque<node> dq;
  dq.push_front(node{ch, cw, 0});
  vis[ch][cw] = 1;
  int ans = 1e9;
  while (!dq.empty()) {
    node te = dq.front();
    dq.pop_front();
    // cout<<te.h<<" "<<te.w<<" "<<te.v<<endl;
    if (te.h == eh && te.w == ew) {
      ans = te.v;
      break;
    }
    // vis[te.h][te.w]=1;
    for (int i = 0; i < 4; i++) {
      int _h = dh[i] + te.h;
      int _w = dw[i] + te.w;
      if (_h >= 1 && _h <= h && _w >= 1 && _w <= w && !vis[_h][_w]) {
        vis[_h][_w] = 1;
        // cout<<"  "<<_h<<" "<<_w<<endl;
        dq.push_front(node{_h, _w, te.v});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int _h = te.h + i;
        int _w = te.w + j;
        if (i != 0 || j != 0) {
          if (_h >= 1 && _h <= h && _w >= 1 && _w <= w && !vis[_h][_w]) {
            // vis[_h][_w]=1;
            dq.push_back(node{_h, _w, te.v + 1});
          }
        }
      }
    }
  }
  if (ans == 1e9)
    cout << -1;
  else
    cout << ans;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

// 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];

  int INF = 1e9;
  vector<vi> dst(H, vi(W, INF));
  dst[sh][sw] = 0;
  deque<pair<int, int>> dq; // <h, w>
  dq.emplace_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.emplace_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.emplace_back(nx_h, nx_w);
    }
  }

  cout << (dst[eh][ew] == INF ? -1 : dst[eh][ew]) << 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];

  int INF = 1e9;
  vector<vi> dst(H, vi(W, INF));
  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] == INF ? -1 : dst[eh][ew]) << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int h, w;
int Sx, Sy, Ex, Ey;
char Map[1005][1005];
int fx[4] = {1, 0, -1, 0};
int fy[4] = {0, 1, 0, -1};
int dis[1005][1005];

struct node {
  int x, y;
};

void BFS01() {
  deque<node> Q;
  memset(dis, 0x3f, sizeof(dis));
  dis[Sx][Sy] = 0;
  node now;
  Q.push_front({Sx, Sy});

  while (!Q.empty()) {
    now = Q.front();
    Q.pop_front();
    for (int i = 0; i < 4; i++) {
      int nx = now.x + fx[i];
      int ny = now.y + fy[i];
      if (nx > h || ny > w || nx < 1 || ny < 1 || Map[nx][ny] == '#')
        continue;
      if (dis[now.x][now.y] < dis[nx][ny]) {
        dis[nx][ny] = dis[now.x][now.y];
        Q.push_front({nx, ny});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = now.x + i;
        int ny = now.y + j;
        if (nx > h || ny > w || nx < 1 || ny < 1 || Map[nx][ny] == '#')
          continue;
        if (dis[now.x][now.y] + 1 < dis[nx][ny]) {
          dis[nx][ny] = dis[now.x][now.y] + 1;
          Q.push_back({nx, ny});
        }
      }
    }
  }
}

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d%d%d", &Sx, &Sy, &Ex, &Ey);
  for (int i = 1; i <= h; i++)
    scanf("%s", Map[i] + 1);

  BFS01();
  if (dis[Ex][Ey] <= h * w)
    printf("%d\\n", dis[Ex][Ey]);
  else
    printf("-1");
  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

int h, w;
int Sx, Sy, Ex, Ey;
char Map[1005][1005];
int fx[4] = {1, 0, -1, 0};
int fy[4] = {0, 1, 0, -1};
int dp[1005][1005], Min = 0x3f3f3f;
const int INF = 0x3f3f3f;

void dfs(int nx, int ny, int cnt) {
  if (dp[nx][ny] <= cnt)
    return;
  else
    dp[nx][ny] = cnt;

  if (nx == Ex && ny == Ey)
    return;

  for (int i = 0; i < 4; i++) {
    int x = nx + fx[i];
    int y = ny + fy[i];
    if (x > h || y > w || x < 1 || y < 1 || Map[x][y] == '#')
      continue;
    dfs(x, y, cnt);
  }

  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int x = nx + i;
      int y = ny + j;
      if (x > h || y > w || x < 1 || y < 1 || Map[x][y] == '#')
        continue;
      dfs(x, y, cnt + 1);
    }
  }
  return;
}

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d%d%d", &Sx, &Sy, &Ex, &Ey);
  for (int i = 1; i <= h; i++)
    scanf("%s", Map[i] + 1);

  memset(dp, INF, sizeof(dp));
  dfs(Sx, Sy, 0);
  if (dp[Ex][Ey] >= h * w)
    puts("-1");
  else
    printf("%d\\n", dp[Ex][Ey]);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int inf = 1000000000;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(h);

  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) { cin >> s[i]; }
  vector<vector<int>> dist(h, vector<int>(w, inf));
  deque<pair<int, int>> q;
  dist[ch][cw] = 0;
  q.emplace_back(ch, cw);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];
    rep(k, 4) {
      int ny = i + dx[k];
      int nx = j + dy[k];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dist[ny][nx] <= d)
        continue;
      dist[ny][nx] = d;
      q.emplace_front(ny, nx);
    }
    for (int ai = -2; ai <= 2; ai++) {
      for (int aj = -2; aj <= 2; aj++) {
        int ny = i + ai;
        int nx = j + aj;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (dist[ny][nx] <= d + 1)
          continue;
        dist[ny][nx] = d + 1;
        q.emplace_back(ny, nx);
      }
    }
  }
  int ans = dist[dh][dw];
  if (ans == inf) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(h);
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) { cin >> s[i]; }
  vector<vector<int>> dist(h, vector<int>(w, inf));
  deque<pair<int, int>> q;
  dist[ch][cw] = 0;
  q.emplace_back(ch, cw);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];
    rep(k, 4) {
      int nx = i + dx[k];
      int ny = j + dy[k];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (s[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= d)
        continue;
      dist[nx][ny] = d;
      q.emplace_front(nx, ny);
    }
    for (int ai = -2; ai <= 2; ai++) {
      for (int aj = -2; aj <= 2; aj++) {
        int nx = i + ai;
        int ny = j + aj;
        if (nx < 0 || nx >= w || ny < 0 || ny >= h)
          continue;
        if (s[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= d + 1)
          continue;
        dist[nx][ny] = d + 1;
        q.emplace_back(nx, ny);
      }
    }
  }
  int ans = dist[dh][dw];
  if (ans == inf) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  rep(i, 1002) rep(j, 1002) {
    if (i % (h + 1) == 0 || j % (w + 1) == 0)
      mp[i][j] = -1;
    else
      mp[i][j] = 1e9;
  }
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] != '.') mp[i + 1][j + 1] = -1;
  }
  deque<pair<int, pair<int, int>>> q;
  mp[sy][sx] = 0;
  q.emplace_back(0, make_pair(sy, sx));
  while (q.size()) {
    int cost = q.front().first;
    int y = q.front().second.first;
    int x = q.front().second.second;
    q.pop_front();
    if (mp[y][x] < cost)
      continue;
    for (int i = y - 2; i <= y + 2; i++) {
      for (int j = x - 2; j <= x + 2; j++) {
        if (i < 1 || h < i || j < 1 || w < j)
          continue;
        if (i == y && j == x)
          continue;
        if (abs(i - y) + abs(j - x) == 1 && mp[i][j] > cost) {
          mp[i][j] = cost;
          q.emplace_front(cost, make_pair(i, j));
        } else if (mp[i][j] > cost + 1) {
          mp[i][j] = cost + 1;
          q.emplace_back(cost + 1, make_pair(i, j));
        }
      }
    }
  }
  if (mp[gy][gx] == 1e9)
    mp[gy][gx] = -1;
  cout << mp[gy][gx] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define MOD 1000000007

using namespace std;
using ll = long long;

int mp[1005][1005] = {};

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  rep(i, 1002) rep(j, 1002) {
    if (i % (h + 1) == 0 || j % (w + 1) == 0)
      mp[i][j] = -1;
    else
      mp[i][j] = 1e9;
  }
  rep(i, h) {
    string s;
    cin >> s;
    rep(j, w) if (s[j] != '.') mp[i + 1][j + 1] = -1;
  }
  deque<pair<int, pair<int, int>>> q;
  mp[sy][sx] = 0;
  q.emplace_back(0, make_pair(sy, sx));
  while (q.size()) {
    deque<pair<int, pair<int, int>>> cp;
    swap(q, cp);
    while (cp.size()) {
      int cost = cp.front().first;
      int y = cp.front().second.first;
      int x = cp.front().second.second;
      cp.pop_front();
      if (mp[y][x] < cost)
        continue;
      for (int i = y - 2; i <= y + 2; i++) {
        for (int j = x - 2; j <= x + 2; j++) {
          if (i < 1 || h < i || j < 1 || w < j)
            continue;
          if (i == y && j == x)
            continue;
          if (abs(i - y) + abs(j - x) == 1 && mp[i][j] > cost) {
            mp[i][j] = cost;
            q.emplace_front(cost, make_pair(i, j));
          } else if (mp[i][j] > cost + 1) {
            mp[i][j] = cost + 1;
            q.emplace_back(cost + 1, make_pair(i, j));
          }
        }
      }
    }
  }
  if (mp[gy][gx] == 1e9)
    mp[gy][gx] = -1;
  cout << mp[gy][gx] << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int H, W;
  cin >> H >> W;
  int sx, sy;
  int gx, gy;
  cin >> sy >> sx;
  cin >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  string S;
  int iii = 10101010;
  vector<vector<int>> c(H, vector<int>(W, iii));
  bool f = false;
  for (int i = 0; i < H; i++) {
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == '#')
        c[i][j] = -1;
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(sy, sx));
  c[sy][sx] = 0;
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();
    int n = c[now.first][now.second];
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int dy = now.first + i;
        int dx = now.second + j;
        int ttt = 0;
        if (dy < 0 || dy >= H)
          continue;
        if (dx < 0 || dx >= W)
          continue;
        int nn = n;
        int dd = c[dy][dx];
        if (abs(i) > 1 || abs(j) > 1) {
          nn++;
        } else if (abs(i) == 1 && abs(j) == 1) {
          nn++;
        }
        if (dd == -1) {
          continue;
        } else if (dd > nn) {
          c[dy][dx] = nn;
          q.push(make_pair(dy, dx));
        } else {
          continue;
        }
      }
    }
  }
  if (c[gy][gx] != iii) {
    cout << c[gy][gx] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W;
  cin >> H >> W;
  int sx, sy;
  int gx, gy;
  cin >> sy >> sx;
  cin >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  string S;
  int iii = 10101010;
  vector<vector<int>> c(H, vector<int>(W, iii));
  bool f = false;
  for (int i = 0; i < H; i++) {
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == '#')
        c[i][j] = -1;
    }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(sy, sx));
  c[sy][sx] = 0;
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();
    int y = now.first, x = now.second;
    int n = c[y][x];
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int dy = y + i;
        int dx = x + j;
        int ttt = 0;
        if (dy < 0 || dx < 0 || dy >= H || dx >= W)
          continue;
        int nn = n;
        int dd = c[dy][dx];
        if (abs(i) > 1 || abs(j) > 1) {
          nn++;
        } else if (abs(i) == 1 && abs(j) == 1) {
          nn++;
        }
        if (dd == -1) {
          continue;
        } else if (dd > nn) {
          c[dy][dx] = nn;
          q.push(make_pair(dy, dx));
        } else {
          continue;
        }
      }
    }
  }
  if (c[gy][gx] != iii) {
    cout << c[gy][gx] << 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();
    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 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_front(ni, nj);
      }
    }
  }
  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}