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

#include <bits/stdc++.h>

using namespace std;

struct P {
  int x;
  int y;
};

const int dx[4] = {-1, 0, +1, 0};
const int dy[4] = {0, -1, 0, +1};
int H, W, sy, sx, gy, gx;
vector<string> S;
vector<vector<int>> V;

int main() {
  cin >> H >> W >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  S.resize(H);
  for (int y = 0; y < H; y++)
    cin >> S[y];
  V = vector<vector<int>>(H, vector<int>(W, -1));

  int level = 0;
  queue<P> same;
  same.push({sx, sy});
  while (!same.empty()) {
    queue<P> jumps;
    while (!same.empty()) {
      P p = same.front();
      same.pop();

      if (V[p.y][p.x] != -1)
        continue;

      V[p.y][p.x] = level;
      for (int d = 0; d < 4; d++) {
        int x2 = p.x + dx[d];
        int y2 = p.y + dy[d];
        if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2)
          continue;
        if (S[y2][x2] == '#')
          continue;
        same.push({x2, y2});
      }

      for (int x2 = p.x - 2; x2 <= p.x + 2; x2++) {
        for (int y2 = p.y - 2; y2 <= p.y + 2; y2++) {
          if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2)
            continue;
          if (S[y2][x2] == '#')
            continue;
          jumps.push({x2, y2});
        }
      }
    }

    while (!jumps.empty()) {
      P p = jumps.front();
      jumps.pop();
      if (V[p.y][p.x] != -1)
        continue;
      same.push(p);
    }
    level++;
  }

  cout << V[gy][gx] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct P {
  int x;
  int y;
};

const int dx[4] = {-1, 0, +1, 0};
const int dy[4] = {0, -1, 0, +1};
int H, W, sy, sx, gy, gx;
vector<string> S;
vector<vector<int>> V;

void dfs(int x, int y, int level) {
  if (V[y][x] != -1 && V[y][x] <= level)
    return;

  queue<P> same, jumps;
  same.push({x, y});

  while (!same.empty()) {
    P p = same.front();
    same.pop();

    if (V[p.y][p.x] != -1 && V[p.y][p.x] <= level)
      continue;

    V[p.y][p.x] = level;
    for (int d = 0; d < 4; d++) {
      int x2 = p.x + dx[d];
      int y2 = p.y + dy[d];
      if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2)
        continue;
      if (S[y2][x2] == '#')
        continue;
      same.push({x2, y2});
    }

    for (int x2 = p.x - 2; x2 <= p.x + 2; x2++) {
      for (int y2 = p.y - 2; y2 <= p.y + 2; y2++) {
        if (x2 < 0 || W <= x2 || y2 < 0 || H <= y2)
          continue;
        if (S[y2][x2] == '#')
          continue;
        jumps.push({x2, y2});
      }
    }
  }

  while (!jumps.empty()) {
    P p = jumps.front();
    jumps.pop();

    if (V[p.y][p.x] != -1 && V[p.y][p.x] <= level)
      continue;
    dfs(p.x, p.y, level + 1);
  }
}

int main() {
  cin >> H >> W >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  S.resize(H);
  for (int y = 0; y < H; y++)
    cin >> S[y];
  V = vector<vector<int>>(H, vector<int>(W, -1));
  dfs(sx, sy, 0);

  cout << V[gy][gx] << endl;

  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <deque>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using P = pair<int, int>;

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

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);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  const int INF = 1e18;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[ch][cw] = 0;
  deque<P> q;
  q.emplace_front(ch, cw);
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];
    for (int k = 0; k < 4; k++) {
      int ni = i + di[k];
      int nj = j + dj[k];
      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 a = -2; a <= 2; a++) {
      for (int b = -2; b <= 2; b++) {
        int ni = i + a;
        int nj = j + b;
        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;
  if (dist[dh][dw] == INF) {
    ans = -1;
  } else {
    ans = dist[dh][dw];
  }
  cout << ans << endl;
  return 0;
}

Error: -11

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[] = {0, -1, 0, 1};
const int dj[] = {-1, 0, 1, 0};

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct node {
  int x, y;
} t;
char a[1005][1005];
int inq[1005][1005];
int n, m;
deque<node> q;
bool check(int x, int y, int w) {
  return !(x < 1 || x > n || y < 1 || y > m || inq[x][y] <= w);
}
signed main() {
  int i, j, sx, sy, ex, ey;
  string s;
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (i = 1; i <= n; i++) {
    cin >> s;
    for (j = 1; j <= m; j++) {
      a[i][j] = s[j - 1];
      if (a[i][j] == '#')
        inq[i][j] = -1;
      else
        inq[i][j] = 2147483647;
    }
  }
  q.push_front((node){sx, sy});
  inq[sx][sy] = 0;
  while (q.size()) {
    t = q.front();
    q.pop_front();
    if (t.x == ex && t.y == ey) {
      cout << inq[ex][ey];
      while (q.size())
        q.pop_front();
      return 0;
    }
    for (i = 0; i < 4; i++)
      if (check(t.x + dx[i], t.y + dy[i], inq[t.x][t.y])) {
        inq[t.x + dx[i]][t.y + dy[i]] = inq[t.x][t.y];
        q.push_front((node){t.x + dx[i], t.y + dy[i]});
      }
    for (i = -2; i < 3; i++)
      for (j = -2; j < 3; j++)
        if (check(t.x + i, t.y + j, inq[t.x][t.y] + 1)) {
          inq[t.x + i][t.y + j] = inq[t.x][t.y] + 1;
          q.push_back((node){t.x + i, t.y + j});
        }
  }
  cout << -1;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
struct node {
  int x, y;
} t;
char a[1005][1005];
int inq[1005][1005];
int n, m;
deque<node> q;
bool check(int x, int y, int w) {
  return !(inq[x][y] <= w || x < 1 || x > n || y < 1 || y > m);
}
signed main() {
  int i, j, sx, sy, ex, ey;
  string s;
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (i = 1; i <= n; i++) {
    cin >> s;
    for (j = 1; j <= m; j++) {
      a[i][j] = s[j - 1];
      if (a[i][j] == '#')
        inq[i][j] = -1;
      else
        inq[i][j] = 2147483647;
    }
  }
  q.push_front((node){sx, sy});
  inq[sx][sy] = 0;
  while (q.size()) {
    t = q.front();
    q.pop_front();
    if (t.x == ex && t.y == ey) {
      cout << inq[ex][ey];
      while (q.size())
        q.pop_front();
      return 0;
    }
    for (i = 0; i < 4; i++)
      if (check(t.x + dx[i], t.y + dy[i], inq[t.x][t.y])) {
        inq[t.x + dx[i]][t.y + dy[i]] = inq[t.x][t.y];
        q.push_front((node){t.x + dx[i], t.y + dy[i]});
      }
    for (i = -2; i < 3; i++)
      for (j = -2; j < 3; j++)
        if (check(t.x + i, t.y + j, inq[t.x][t.y] + 1)) {
          inq[t.x + i][t.y + j] = inq[t.x][t.y] + 1;
          q.push_back((node){t.x + i, t.y + j});
        }
  }
  cout << -1;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll int
#define pb push_back
#define endl '\\n'
#define pii pair<ll ll, ll ll>
#define all(v) (v).begin(), (v).end()
using namespace std;
const int N = 1e3 + 5;
ll vis[N][N], a[N][N];
const std::vector<ll> v = {-2, -1, 0, 1, 2};
ll n, m;
ll tx, ty, nx, ny, type;
deque<pair<ll, ll>> q;
inline bool check(ll x, ll y) { return x > 0 and y > 0 and x <= n and y <= m; }

void solve() {
  char c;
  cin >> n >> m;
  ll x, y;
  cin >> x >> y >> tx >> ty;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> c;
      a[i + 1][j + 1] = (c == '.');
      vis[i + 1][j + 1] = INT_MAX;
    }

  q.push_back({x, y});
  vis[x][y] = 0;
  while (!q.empty()) {

    tie(x, y) = q.front();
    q.pop_front();
    if (x == tx and y == ty)
      break;

    for (ll dx : v)
      for (ll dy : v) {
        nx = dx + x, ny = dy + y;
        type = (abs(dx) + abs(dy) != 1);
        if (check(nx, ny) and a[nx][ny] and vis[nx][ny] > vis[x][y] + type) {
          vis[nx][ny] = vis[x][y] + type;
          if (type)
            q.push_back({nx, ny});
          else
            q.push_front({nx, ny});
        }
      }
  }

  cout << (vis[tx][ty] == INT_MAX ? -1 : vis[tx][ty]);
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll TESTS = 1;
  // cin >> TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll int
#define pb push_back
#define endl '\\n'
#define pii pair<ll ll, ll ll>
#define all(v) (v).begin(), (v).end()
using namespace std;
const int N = 1e3 + 5;
ll vis[N][N], a[N][N];
const std::vector<ll> v = {-2, -1, 0, 1, 2};
ll n, m;
ll tx, ty, nx, ny, type;
queue<pair<ll, ll>> q;
inline bool check(ll x, ll y) { return x > 0 and y > 0 and x <= n and y <= m; }

void solve() {
  char c;
  cin >> n >> m;
  ll x, y;
  cin >> x >> y >> tx >> ty;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> c;
      a[i + 1][j + 1] = (c == '.');
      vis[i + 1][j + 1] = INT_MAX;
    }

  q.push({x, y});
  vis[x][y] = 0;
  while (!q.empty()) {

    tie(x, y) = q.front();
    q.pop();
    for (ll dx : v)
      for (ll dy : v) {
        nx = dx + x, ny = dy + y;
        type = (abs(dx) + abs(dy) != 1);
        if (check(nx, ny) and a[nx][ny] and vis[nx][ny] > vis[x][y] + type)
          vis[nx][ny] = vis[x][y] + type, q.push({nx, ny});
      }
  }

  cout << (vis[tx][ty] == INT_MAX ? -1 : vis[tx][ty]);
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll TESTS = 1;
  // cin >> TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define maxn 1086

using namespace std;

int n, m;
int sx, sy, tx, ty;
char s[maxn][maxn];

int f[maxn * maxn];
queue<int> q;

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

int col[maxn][maxn], cnt;

void dfs(int i, int j) {
  for (int k = 0; k < 4; k++) {
    int ii = i + dx[k], jj = j + dy[k];
    if (s[ii][jj] == '.' && !col[ii][jj]) {
      col[ii][jj] = col[i][j], dfs(ii, jj);
    }
  }
}

vector<int> v[maxn * maxn];

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &tx, &ty);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '.' && !col[i][j]) {
        col[i][j] = ++cnt;
        dfs(i, j);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] != '.')
        continue;
      for (int x = -2; x <= 2; x++) {
        for (int y = -2; y <= 2; y++) {
          if (!x && !y)
            continue;
          int ii = x + i, jj = y + j;
          if (ii <= 0 || ii > n || jj <= 0 || jj > m || s[ii][jj] != '.')
            continue;
          if (col[ii][jj] == col[i][j])
            continue;
          v[col[i][j]].push_back(col[ii][jj]);
        }
      }
    }
  }
  memset(f, 0x3f, sizeof(f));
  f[col[sx][sy]] = 0, q.push(col[sx][sy]);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int to = v[x][i];
      if (f[to] > f[x] + 1) {
        f[to] = f[x] + 1;
        q.push(to);
      }
    }
  }
  printf("%d", f[col[tx][ty]] == 0x3f3f3f3f ? -1 : f[col[tx][ty]]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define maxn 1086

using namespace std;

int n, m;
int sx, sy, tx, ty;
char s[maxn][maxn];

int f[maxn][maxn];
queue<pair<int, int>> q;
bool inq[maxn][maxn];

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

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &tx, &ty);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  memset(f, 0x3f, sizeof(f));
  f[sx][sy] = 0, q.push({sx, sy}), inq[sx][sy] = true;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    inq[x][y] = false;
    for (int k = 0; k < 4; k++) {
      int i = p.first + dx[k], j = p.second + dy[k];
      if (s[i][j] == '.') {
        if (f[i][j] > f[x][y]) {
          f[i][j] = f[x][y];
          if (!inq[i][j]) {
            q.push({i, j}), inq[i][j] = false;
          }
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (!i && !j)
          continue;
        int ii = x + i, jj = y + j;
        if (ii <= 0 || ii > n || jj <= 0 || jj > m)
          continue;
        if (s[ii][jj] != '.')
          continue;
        if (f[ii][jj] > f[x][y] + 1) {
          f[ii][jj] = f[x][y] + 1;
          if (!inq[ii][jj]) {
            q.push({ii, jj}), inq[ii][jj] = false;
          }
        }
      }
    }
  }
  printf("%d", f[tx][ty] == 0x3f3f3f3f ? -1 : f[tx][ty]);
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
signed main() {
  ll H, W, i, j, c = 0;
  P S, G;
  cin >> H >> W >> S.F >> S.S >> G.F >> G.S;
  bool X[H][W];
  ll Y[H][W];
  deque<P> B, C;
  REP(i, H) REP(j, W) {
    char s;
    cin >> s;
    X[i][j] = (s == '.');
    Y[i][j] = H * W;
  }
  S.F--;
  S.S--;
  G.F--;
  G.S--;
  B.push_back(S);
  Y[S.F][S.S] = 0;
  vector<P> O(4);
  O.at(0) = P(-1, 0);
  O.at(1) = P(0, -1);
  O.at(2) = P(1, 0);
  O.at(3) = P(0, 1);
  while (B.size() + C.size() && c < 7000000) {
    P E;
    if (B.size()) {
      E = B.front();
      B.pop_front();
    } else {
      E = C.front();
      C.pop_front();
    }
    REP(i, 4) {
      ll x = E.F + O.at(i).F, y = E.S + O.at(i).S;
      if (0 <= x && x < H && 0 <= y && y < W && X[x][y] &&
          Y[x][y] > Y[E.F][E.S]) {
        Y[x][y] = Y[E.F][E.S];
        B.push_back(P(x, y));
      }
    }
    FOR(i, -2, 3) FOR(j, -2, 3) if (abs(i) + abs(j) > 1) {
      ll x = E.F + i, y = E.S + j;
      if (0 <= x && x < H && 0 <= y && y < W && X[x][y] &&
          Y[x][y] > Y[E.F][E.S] + 1) {
        Y[x][y] = Y[E.F][E.S] + 1;
        C.push_back(P(x, y));
      }
    }
  }
  if (Y[G.F][G.S] == H * W)
    Y[G.F][G.S] = -1;
  cout << Y[G.F][G.S] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i, l, r) for (i = l; i < r; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define P pair<ll, ll>
#define F first
#define S second
signed main() {
  ll H, W, i, j, c = 0;
  P S, G;
  cin >> H >> W >> S.F >> S.S >> G.F >> G.S;
  bool X[H][W];
  ll Y[H][W];
  deque<P> B;
  REP(i, H) REP(j, W) {
    char s;
    cin >> s;
    X[i][j] = (s == '.');
    Y[i][j] = H * W;
  }
  S.F--;
  S.S--;
  G.F--;
  G.S--;
  B.push_back(S);
  Y[S.F][S.S] = 0;
  vector<P> O(4);
  O.at(0) = P(-1, 0);
  O.at(1) = P(0, -1);
  O.at(2) = P(1, 0);
  O.at(3) = P(0, 1);
  while (B.size() && c < 8000000) {
    P E = B.front();
    B.pop_front();
    c++;
    REP(i, 4) {
      ll x = E.F + O.at(i).F, y = E.S + O.at(i).S;
      if (0 <= x && x < H && 0 <= y && y < W && X[x][y] &&
          Y[x][y] > Y[E.F][E.S]) {
        Y[x][y] = Y[E.F][E.S];
        B.push_back(P(x, y));
      }
    }
    FOR(i, -2, 3) FOR(j, -2, 3) if (abs(i) + abs(j) > 1) {
      ll x = E.F + i, y = E.S + j;
      if (0 <= x && x < H && 0 <= y && y < W && X[x][y] &&
          Y[x][y] > Y[E.F][E.S] + 1) {
        Y[x][y] = Y[E.F][E.S] + 1;
        B.push_back(P(x, y));
      }
    }
  }
  if (Y[G.F][G.S] == H * W || c == 8000000)
    Y[G.F][G.S] = -1;
  cout << Y[G.F][G.S] << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAXN 1005
#define INF (1LL << 30)

char m[MAXN][MAXN];
long long dist[MAXN][MAXN];

int r, c;
int sx, sy;
int dx, dy;

int dirx[] = {0, 0, -1, 1};
int diry[] = {-1, 1, 0, 0};

bool check(int x, int y) {
  return (x >= 1 and x <= r and y >= 1 and y <= c and m[x][y] == '.');
}

void bfs(int x, int y) {
  deque<pair<int, int>> q;
  q.push_front({x, y});
  dist[x][y] = 0;

  while (not q.empty()) {
    pair<int, int> cur = q.front();
    q.pop_front();

    x = cur.first;
    y = cur.second;

    for (int i = 0; i < 4; i++) {
      if (check(x + dirx[i], y + diry[i])) {
        if (dist[x + dirx[i]][y + diry[i]] > dist[x][y]) {
          dist[x + dirx[i]][y + diry[i]] = dist[x][y];
          q.push_front({x + dirx[i], y + diry[i]});
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (check(x + i, y + j)) {
          if (dist[x + i][y + j] > dist[x][y] + 1) {
            dist[x + i][y + j] = dist[x][y] + 1;
            q.push_back({x + i, y + j});
          }
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> r >> c;
  cin >> sx >> sy;
  cin >> dx >> dy;

  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> m[i][j];
      dist[i][j] = INF;
    }
  }

  bfs(sx, sy);

  cout << (dist[dx][dy] == INF ? -1 : dist[dx][dy]) << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAXN 1005
#define INF (1LL << 30)

vector<vector<char>> m;
vector<vector<long long>> dist;

int r, c;
int sx, sy;
int dx, dy;

int dirx[] = {0, 0, -1, 1};
int diry[] = {-1, 1, 0, 0};

bool check(int x, int y) {
  return (x >= 1 and x <= r and y >= 1 and y <= c and m[x][y] == '.');
}

void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  dist[x][y] = 0;

  while (not q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();

    x = cur.first;
    y = cur.second;

    for (int i = 0; i < 4; i++) {
      if (check(x + dirx[i], y + diry[i])) {
        if (dist[x + dirx[i]][y + diry[i]] > dist[x][y]) {
          dist[x + dirx[i]][y + diry[i]] = dist[x][y];
          q.push({x + dirx[i], y + diry[i]});
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (check(x + i, y + j)) {
          if (dist[x + i][y + j] > dist[x][y] + 1) {
            dist[x + i][y + j] = dist[x][y] + 1;
            q.push({x + i, y + j});
          }
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> r >> c;
  cin >> sx >> sy;
  cin >> dx >> dy;

  m.assign(r + 1, vector<char>(c + 1));
  dist.assign(r + 1, vector<long long>(c + 1));

  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      cin >> m[i][j];
      dist[i][j] = INF;
    }
  }

  bfs(sx, sy);

  cout << (dist[dx][dy] == INF ? -1 : dist[dx][dy]) << '\\n';

  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int inf = 0x3f3f3f3f;
char Map[1005][1005];
int dist[1005][1005];
bool vis[1005][1005];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, m;
int sx, sy, ex, ey;
void bfsDeque() {
  memset(dist, 0x3f, sizeof(dist));
  memset(vis, 0, sizeof(vis));
  dist[sx][sy] = 0;
  deque<pair<int, int>> q;
  q.push_back(make_pair(sx, sy));
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop_front();
    if (vis[u.x][u.y]) {
      continue;
    }
    vis[u.x][u.y] = 1;
    for (int i = 0; i < 4; i++) {
      int tx = u.x + dx[i];
      int ty = u.y + dy[i];
      if (!vis[tx][ty] && tx >= 1 && tx <= n && ty >= 1 && ty <= m &&
          Map[tx][ty] != '#') {
        if (dist[tx][ty] > dist[u.x][u.y]) {
          dist[tx][ty] = dist[u.x][u.y];
          q.push_front(make_pair(tx, ty));
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int tx = u.x + i;
        int ty = u.y + j;
        if (!vis[tx][ty] && tx >= 1 && tx <= n && ty >= 1 && ty <= m &&
            Map[tx][ty] != '#') {
          if (dist[tx][ty] > dist[u.x][u.y] + 1) {
            dist[tx][ty] = dist[u.x][u.y] + 1;
            q.push_back(make_pair(tx, ty));
          }
        }
      }
    }
  }
}

int main() {
  scanf("%d %d %d %d %d %d", &n, &m, &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; i++) {
    scanf("%s", Map[i] + 1);
  }
  bfsDeque();
  if (dist[ex][ey] == inf) {
    printf("-1");
    return 0;
  }
  printf("%d", dist[ex][ey]);
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
char Map[1005][1005];
int step[1005][1005];
bool vis[1005][1005];
int n, m;
int sx, sy, ex, ey;

int dfs(int x, int y) {
  if (step[x][y] != -1) {
    return step[x][y];
  }
  int ans = 0x3f3f3f3f;
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int tx = x + i;
      int ty = y + j;
      if (tx == i && ty == j) {
        continue;
      }
      if (Map[tx][ty] == '#') {
        continue;
      }
      if (vis[tx][ty] == 1) {
        continue;
      }
      vis[tx][ty] = 1;
      ans = min(ans, dfs(tx, ty) + 1);
      vis[tx][ty] = 0;
    }
  }
  return step[x][y] = ans;
}

int main() {
  scanf("%d %d %d %d %d %d", &n, &m, &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; i++) {
    scanf("%s", Map[i] + 1);
  }
  memset(step, -1, sizeof(step));
  step[ex][ey] = 0;
  printf("%d", dfs(sx, sy));
  return 0;
}