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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int, int>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
vector<vector<int>> seen(1005, vector<int>(1005, 100000000));
vector<string> v(1005);
int now = 0;
int h, w;
queue<tuple<int, int, int>> q;

void dfs(int x, int y, int z) {
  bool b = 1;
  if (seen[x][y] < z)
    b = 0;
  if (b)
    seen[x][y] = z;
  if (b)
    rep(dir, 4) {
      int nh = x + dx[dir];
      int nw = y + dy[dir];
      if (nh < 0 || nh >= h || nw < 0 || nw >= w)
        continue;
      if (v[nh][nw] == '#')
        continue;
      if (seen[nh][nw] == z)
        continue;
      dfs(nh, nw, z);
    }
  if (b)
    rep(i, 5) rep(j, 5) {
      int nh = x + i - 2;
      int nw = y + j - 2;
      if (nh < 0 || nh >= h || nw < 0 || nw >= w)
        continue;
      if (v[nh][nw] == '#')
        continue;
      if (seen[nh][nw] >= z + 1) {
        seen[nh][nw] = min(seen[nh][nw], z + 1);
        q.push(make_tuple(nh, nw, z + 1));
      }
    }
}

int main() {

  cin >> h >> w;
  P s, g;
  cin >> s.first >> s.second >> g.first >> g.second;

  rep(i, h) cin >> v[i];
  s.first--;
  s.second--;
  g.first--;
  g.second--;
  q.push(make_tuple(s.first, s.second, 0));
  while (!q.empty()) {
    tuple<int, int, int> tu = q.front();
    q.pop();
    dfs(get<0>(tu), get<1>(tu), get<2>(tu));
  }
  if (seen[g.first][g.second] != 100000000)
    cout << seen[g.first][g.second] << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int, int>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
vector<vector<int>> seen(1005, vector<int>(1005, 100000000));
vector<string> v(1005);
int now = 0;
int h, w;

void dfs(int x, int y, int z) {
  seen[x][y] = z;
  rep(dir, 4) {
    int nh = x + dx[dir];
    int nw = y + dy[dir];
    if (nh < 0 || nh >= h || nw < 0 || nw >= w)
      continue;
    if (v[nh][nw] == '#')
      continue;
    if (seen[nh][nw] == z)
      continue;
    dfs(nh, nw, z);
  }
  rep(i, 5) rep(j, 5) {
    int nh = x + i - 2;
    int nw = y + j - 2;
    if (nh < 0 || nh >= h || nw < 0 || nw >= w)
      continue;
    if (v[nh][nw] == '#')
      continue;
    seen[nh][nw] = min(seen[nh][nw], z + 1);
  }
}

int main() {

  cin >> h >> w;
  P s, g;
  cin >> s.first >> s.second >> g.first >> g.second;

  rep(i, h) cin >> v[i];
  s.first--;
  s.second--;
  g.first--;
  g.second--;
  bool b = 1;
  while (b) {
    dfs(s.first, s.second, now);
    now++;
    ll bb = 0;
    rep(i, h) rep(j, w) {
      if (seen[i][j] == now) {
        bb++;
        dfs(i, j, now);
      }
    }
    if (bb == 0)
      b = 0;
  }
  if (seen[g.first][g.second] != 100000000)
    cout << seen[g.first][g.second] << endl;
  else
    cout << -1 << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>
#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 ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[ch][cw] = 0;
  deque<P> q;
  q.emplace_back(ch, cw);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();

    rep(v, 4) {
      int ni = i + di[v], 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[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#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 ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> ch >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[ch][cw] = 0;
  deque<P> q;
  q.emplace_back(ch, cw);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();

    rep(v, 4) {
      int ni = i + di[v], 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[dh][dw];
  if (ans == INF)
    ans = -1;
  cout << dist[dh][dw] << endl;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define MP make_pair
#define PB emplace_back
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

using namespace std;

const int N = 1e3 + 100;

int a[N], h[N][N], n, m;

bool viz[N][N], in[N][N];

char c[N][N];

bool fix[N][N];

bool exist(int x, int y) {
  return x >= 0 && y >= 0 && x < n && y < m && c[x][y] == '.';
}

bool tam(int x1, int y1, int x2, int y2) {
  return (abs(x1 - x2) + abs(y1 - y2) != 1);
}

int main() {
  IOS;
  cin >> n >> m;
  long long int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      h[i][j] = 1e7;
    }
  }
  deque<pair<int, int>> q;
  q.push_back(MP(sx, sy));
  h[sx][sy] = 0;
  while (q.size() > 0) {
    pair<int, int> p = q.front();
    q.pop_front();
    if (fix[p.F][p.S])
      continue;
    fix[p.F][p.S] = 1;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (exist(p.F + i, p.S + j)) {
          bool add = tam(p.F + i, p.S + j, p.F, p.S);
          if (h[p.F + i][p.S + j] > h[p.F][p.S] + add) {
            if (add) {
              h[p.F + i][p.S + j] = h[p.F][p.S] + add;
              q.push_back(MP(p.F + i, p.S + j));
            } else {
              h[p.F + i][p.S + j] = h[p.F][p.S] + add;
              q.push_front(MP(p.F + i, p.S + j));
            }
          }
        }
      }
    }
  }
  if (h[ex][ey] > 1e6 + 1000) {
    cout << -1 << '\\n';
  } else
    cout << h[ex][ey] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define PB emplace_back
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

using namespace std;

const int N = 1e3 + 100;

int a[N], h[N][N], n, m;

bool viz[N][N], in[N][N];

char c[N][N];

bool exist(int x, int y) {
  return x >= 0 && y >= 0 && x < n && y < m && c[x][y] == '.';
}

bool tam(int x1, int y1, int x2, int y2) {
  return (abs(x1 - x2) + abs(y1 - y2) != 1);
}

int main() {
  IOS;
  cin >> n >> m;
  long long int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c[i][j];
      h[i][j] = 1e7;
    }
  }
  queue<pair<int, int>> q;
  q.push(MP(sx, sy));
  h[sx][sy] = 0;
  in[sx][sy] = 1;
  while (q.size() > 0) {
    pair<int, int> p = q.front();
    in[p.F][p.S] = 0;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (exist(p.F + i, p.S + j)) {
          bool add = tam(p.F + i, p.S + j, p.F, p.S);
          if (h[p.F + i][p.S + j] > h[p.F][p.S] + add) {
            h[p.F + i][p.S + j] = h[p.F][p.S] + add;
            if (!in[p.F + i][p.S + j]) {
              q.push(MP(p.F + i, p.S + j));
              in[p.F + i][p.S + j] = 1;
            }
          }
        }
      }
    }
    q.pop();
  }
  if (h[ex][ey] > 1e6 + 1000) {
    cout << -1 << '\\n';
  } else
    cout << h[ex][ey] << '\\n';
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
int h, w;
int arr[1005][1005];
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
bool visited[1005][1005];
int main() {
  int sx, sy;
  int des_x, des_y;
  cin >> h >> w >> sy >> sx >> des_y >> des_x;
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 0; s[j]; j++) {
      if (s[j] == '#')
        arr[i][j + 1] = 1;
    }
  }
  deque<pair<pi, int>> deq;
  deq.push_back(make_pair(pi(sy, sx), 0));
  while (!deq.empty()) {

    int y = deq.front().first.first;
    int x = deq.front().first.second;
    int d = deq.front().second;
    // cout<<y<<" "<<x<<" "<<d<<'\\n';
    deq.pop_front();
    if (visited[y][x])
      continue;
    visited[y][x] = true;
    if (y == des_y && x == des_x) {
      cout << d;
      return 0;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int yy = y + j;
        int xx = x + i;
        if (yy < 1 || yy > h || xx < 1 || xx > w)
          continue;
        if (visited[yy][xx] || arr[yy][xx])
          continue;
        if (abs(yy - y) + abs(xx - x) <= 1) {
          deq.push_front(make_pair(pi(yy, xx), d));
        } else
          deq.push_back(make_pair(pi(yy, xx), d + 1));
      }
    }
  }
  cout << -1;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
int n, m;
bool maze[1010][1010];
int dp[1010][1010];
int desty, destx;
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
void dfs(int y, int x, int dist) {
  dp[y][x] = dist;
  for (int i = 0; i < 4; i++) {
    int yy = y + dy[i];
    int xx = x + dx[i];
    if (maze[yy][xx] && dp[yy][xx] > dist)
      dfs(yy, xx, dist);
  }
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (abs(i) + abs(j) <= 1)
        continue;
      int yy = y + i;
      int xx = x + j;
      if (yy < 0 || xx < 0 || yy > n || xx > m)
        continue;
      if (maze[yy][xx] && dp[yy][xx] > dist + 1)
        dfs(yy, xx, dist + 1);
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      dp[i][j] = INF;
  }
  int y, x;
  cin >> y >> x;
  cin >> desty >> destx;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 0; s[j]; j++) {
      if (s[j] == '.')
        maze[i][j + 1] = true;
    }
  }
  dfs(y, x, 0);
  cout << (dp[desty][destx] == INF ? -1 : dp[desty][destx]);
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
char a[1005][1005];
int mem[1005][1005];
bool flag[1005][1005];
int ci, cj, di, dj;
int h, w;
int dx[] = {0, 1, 0, -1, 1, -1, 1,  -1, 0, 2,  0, -2,
            1, 2, 1, -2, 2, -1, -2, -1, 2, -2, 2, -2};
int dy[] = {1, 0, -1, 0, 1,  -1, -1, 1,  2, 0,  -2, 0,
            2, 1, -2, 1, -1, 2,  -1, -2, 2, -2, -2, 2};
int ans = 1e9;
struct node {
  int x, y, val;
  node(int A, int B, int C) {
    x = A;
    y = B;
    val = C;
  }
  friend bool operator<(node s1, node s2) { return s1.val > s2.val; }
  node() {}
};
void bfs() {
  priority_queue<node> q;
  q.push(node(ci, cj, 0));
  while (!q.empty()) {
    node now = q.top();
    q.pop();
    int ni = now.x;
    int nj = now.y;
    int nc = now.val;
    if (mem[ni][nj] != -1)
      continue;
    mem[ni][nj] = nc;
    if (ni == di && nj == dj) {
      ans = nc;
      break;
    }
    for (int i = 0; i < 24; i++) {
      int ti = ni + dx[i];
      int tj = nj + dy[i];
      if (ti < 1 || tj < 1 || ti > h || tj > w)
        continue;
      if (a[ti][tj] == '#')
        continue;
      if (i < 4)
        q.push(node(ti, tj, nc));
      if (i >= 4)
        q.push(node(ti, tj, nc + 1));
    }
  }
}
int main() {
  cin >> h >> w;
  cin >> ci >> cj;
  cin >> di >> dj;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      cin >> a[i][j];
      mem[i][j] = -1;
    }
  bfs();
  if (ans == (int)1e9)
    ans = -1;
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
char a[1001][1001];
int mem[1001][1001];
bool flag[1001][1001];
int ans = -1;
int ci, cj, di, dj;
int h, w;
int dx[] = {0, 1, 0, -1, 1, -1, 1,  -1, 0, 2,  0, -2,
            1, 2, 1, -2, 2, -1, -2, -1, 2, -2, 2, -2};
int dy[] = {1, 0, -1, 0, 1,  -1, -1, 1,  2, 0,  -2, 0,
            2, 1, -2, 1, -1, 2,  -1, -2, 2, -2, -2, 2};
void dfs(int i, int j, int cnt) {
  if (mem[i][j] == 0) {
    mem[i][j] = cnt;
  } else if (mem[i][j] <= cnt) {
    return;
  }
  mem[i][j] = cnt;
  if (i == di && j == dj) {
    if (ans == -1)
      ans = cnt;
    else
      ans = min(ans, cnt);
  }
  for (int k = 0; k < 24; k++) {
    int ti = i + dx[k];
    int tj = j + dy[k];
    if (ti > h || tj > w || ti < 1 || tj < 1)
      continue;
    if (a[ti][tj] == '#')
      continue;
    if (flag[ti][tj])
      continue;
    flag[i][j] = true;
    if (k < 4)
      dfs(ti, tj, cnt);
    if (k >= 4)
      dfs(ti, tj, cnt + 1);
    flag[i][j] = false;
  }
}
int main() {
  cin >> h >> w;
  cin >> ci >> cj;
  cin >> di >> dj;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      cin >> a[i][j];
  dfs(ci, cj, 0);
  cout << ans;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

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

static const int MAX = 1e3;
const int INF = 1e9;
bool visited[MAX][MAX];
int d[MAX][MAX];

int main() {
  int h, w;
  int ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      visited[i][j] = (s[j] == '#');
      d[i][j] = INF;
    }
  }
  deque<tuple<int, int, int>> deq;
  deq.emplace_front(ch, cw, 0);
  d[ch][cw] = 0;
  while (!deq.empty()) {
    int uh = get<0>(deq.front());
    int uw = get<1>(deq.front());
    int ucost = get<2>(deq.front());
    deq.pop_front();
    visited[uh][uw] = true;
    if (d[uh][uw] < ucost)
      continue;
    auto check = [&](int i, int j, bool c) {
      if (i < 0 || h <= i)
        return;
      if (j < 0 || w <= j)
        return;
      if (visited[i][j])
        return;
      if (d[i][j] > d[uh][uw] + c) {
        d[i][j] = d[uh][uw] + c;
        if (c) {
          deq.emplace_back(i, j, ucost + 1);
        } else {
          deq.emplace_front(i, j, ucost);
        }
      }
    };
    check(uh + 1, uw, 0);
    check(uh, uw + 1, 0);
    check(uh - 1, uw, 0);
    check(uh, uw - 1, 0);
    for (int i = uh - 2; i <= uh + 2; i++) {
      for (int j = uw - 2; j <= uw + 2; j++) {
        if (i == uh && j == uw)
          continue;
        check(i, j, 1);
      }
    }
  }
  cout << (d[dh][dw] == INF ? -1 : d[dh][dw]) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

static const int MAX = 1e3;
const int INF = 1e9;
int mat[MAX][MAX];
int d[MAX][MAX];

int main() {
  int h, w;
  int ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      mat[i][j] = (s[j] == '#');
      d[i][j] = INF;
    }
  }
  queue<pair<int, int>> q;
  q.emplace(ch, cw);
  d[ch][cw] = 0;
  while (!q.empty()) {
    int uh = q.front().first;
    int uw = q.front().second;
    q.pop();
    auto check = [&](int i, int j, int cost) {
      if (i < 0 || h <= i)
        return;
      if (j < 0 || w <= j)
        return;
      if (mat[i][j])
        return;
      if (d[i][j] > d[uh][uw] + cost) {
        d[i][j] = d[uh][uw] + cost;
        q.emplace(i, j);
      }
    };
    check(uh + 1, uw, 0);
    check(uh, uw + 1, 0);
    check(uh - 1, uw, 0);
    check(uh, uw - 1, 0);
    for (int i = uh - 2; i <= uh + 2; i++) {
      for (int j = uw - 2; j <= uw + 2; j++) {
        if (i == uh && j == uw)
          continue;
        check(i, j, 1);
      }
    }
  }
  cout << (d[dh][dw] == INF ? -1 : d[dh][dw]) << endl;
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  --ch;
  --cw;
  int dh, dw;
  cin >> dh >> dw;
  --dh;
  --dw;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  vector<vector<bool>> p(h, vector<bool>(w, false)),
      r(h, vector<bool>(w, false));
  deque<long long> q;
  long long res = 0;
  while (1) {
    if ((0 <= ch) && (ch < h) && (0 <= cw) && (cw < w)) {
      for (int a = max(0, ch - 2); a <= min(h - 1, ch + 2); ++a)
        for (int b = max(0, cw - 2); b <= min(w - 1, cw + 2); ++b) {
          if ((0 <= a) && (a < h) && (0 <= b) && (b < w))
            if (s[a][b] == '.' && p[a][b] == false) {
              if (abs(a - ch) + abs(b - cw) == 1)
                q.push_front((res << 20) + (a << 10) + b);
              else if (r[a][b] == false) {
                q.push_back(((res + 1) << 20) + (a << 10) + b);
                r[a][b] = true;
              }
            }
        }
      p[ch][cw] = true;
    }
    if (p[dh][dw] == true)
      break;
    if (q.empty()) {
      res = -1;
      break;
    } else {
      res = q.front() >> 20;
      ch = (q.front() & 1047552) >> 10;
      cw = (q.front() & 1023);
      q.pop_front();
    }
  }
  cout << res;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  --ch;
  --cw;
  int dh, dw;
  cin >> dh >> dw;
  --dh;
  --dw;
  vector<string> s(h);
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  vector<vector<bool>> p(h, vector<bool>(w, false)),
      r(h, vector<bool>(w, false));
  deque<int> q;
  int res = 0;
  while (1) {
    if ((0 <= ch) && (ch < h) && (0 <= cw) && (cw < w)) {
      for (int a = max(0, ch - 2); a <= min(h - 1, ch + 2); ++a)
        for (int b = max(0, cw - 2); b <= min(w - 1, cw + 2); ++b) {
          if ((0 <= a) && (a < h) && (0 <= b) && (b < w))
            if (s[a][b] == '.' && p[a][b] == false) {
              if (abs(a - ch) + abs(b - cw) == 1) {
                q.push_front((res << 20) + (a << 10) + b);
                p[a][b] = true;
              } else if (r[a][b] == false) {
                q.push_back(((res + 1) << 20) + (a << 10) + b);
                r[a][b] == true;
              }
            }
        }
      p[ch][cw] = true;
    }
    if (p[dh][dw] == true)
      break;
    if (q.empty()) {
      res = -1;
      break;
    } else {
      res = q.front() >> 20;
      ch = (q.front() & 1047552) >> 10;
      cw = (q.front() & 1023);
      q.pop_front();
    }
  }
  cout << res;
  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>;
using Pi = pair<P, int>;
#define FF first.first
#define FS first.second

int main(void) {
  int h, w;
  cin >> h >> w;

  bool map[1001][1001];
  fill(map[0], map[1001], true);

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;

  string s[h];
  rep(i, h) cin >> s[i];

  deque<Pi> dfs;
  dfs.push_front(Pi(P(ch, cw), 0));

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

  while (!dfs.empty()) {
    auto v = dfs.front();
    dfs.pop_front();
    int y = v.FF, x = v.FS, cost = v.second;

    if (!map[y][x])
      continue;
    map[y][x] = false;

    if (y == dh && x == dw) {
      cout << cost << endl;
      return 0;
    }

    for (int i = 0; i < 24; ++i) {
      int to_x = x + dx[i], to_y = y + dy[i];

      if (to_x < 0 || to_x >= w || to_y < 0 || to_y >= h)
        continue;

      if (map[to_y][to_x] && s[to_y][to_x] == '.') {
        if (i < 4)
          dfs.push_front(Pi(P(to_y, to_x), cost));
        else
          dfs.push_back(Pi(P(to_y, to_x), cost + 1));
      }
    }
  }

  cout << -1 << 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>;
using Pi = pair<P, int>;
#define FF first.first
#define FS first.second

int main(void) {
  int h, w;
  cin >> h >> w;
  int map[1001][1001];
  fill(map[0], map[1001], 1e6 + 1);
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  string s[h];
  rep(i, h) cin >> s[i];
  queue<Pi> dfs;
  dfs.push(Pi(P(ch, cw), 0));
  map[ch][cw] = 0;
  int ans = 1e6 + 1;
  while (!dfs.empty()) {
    auto v = dfs.front();
    dfs.pop();
    int x = v.FF, y = v.FS, c = v.second;
    if (x == dh && y == dw)
      ans = min(ans, c);
    for (int i = x - 2; i <= x + 2; ++i) {
      if (i < 0)
        continue;
      if (i > h - 1)
        break;
      for (int j = y - 2; j <= y + 2; ++j) {
        if (j < 0)
          continue;
        if (j > w - 1)
          break;
        if (s[i][j] == '#')
          continue;
        if (abs(i - x) + abs(j - y) >= 2) {
          if (map[i][j] > c + 1) {
            map[i][j] = c + 1;
            dfs.push(Pi(P(i, j), c + 1));
          }
        } else {
          if (map[i][j] > c) {
            map[i][j] = c;
            dfs.push(Pi(P(i, j), c));
          }
        }
      }
    }
  }
  if (map[dh][dw] == 1e6 + 1)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}