Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 1e3 + 10;
const ll MOD = 1007;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
char grid[MAXN][MAXN];
int xdir[] = {0, 0, -1, 1};
int ydir[] = {-1, 1, 0, 0};
int vis[MAXN][MAXN];
int H, W;
int ch, cw, dh, dw;
bool legal(int x, int y) {
  return x >= 1 && x <= H && y >= 1 && y <= W && grid[x][y] == '.';
}
void bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (vis[x][y])
      continue;
    vis[x][y] = 1;
    for (int k = 0; k < 4; k++) {
      int nx = x + xdir[k], ny = y + ydir[k];
      if (legal(nx, ny) && vis[nx][ny] == 0) {
        q.push({nx, ny});
      }
    }
  }
}
int rxdir[] = {-2, -1, 0, 1, 2};
int rydir[] = {-2, -1, 0, 1, 2};
int mgc[MAXN][MAXN];
int rvis[MAXN][MAXN];
void rbfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  int ans = -1;
  if (vis[sx][sy]) {
    cout << 0;
    return;
  }
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (rvis[x][y])
      continue;
    rvis[x][y] = 1;
    for (int u = 0; u < 5; u++) {
      int nx = x + rxdir[u];
      for (int v = 0; v < 5; v++) {
        int ny = y + rydir[v];
        if (x == nx && y == ny)
          continue;
        if (legal(nx, ny) && rvis[nx][ny] == 0) {
          if (vis[nx][ny]) {
            ans = mgc[x][y] + 1;
            cout << ans << "\\n";
            return;
          } else {
            mgc[nx][ny] = mgc[x][y] + 1;
          }
          q.push({nx, ny});
        }
      }
    }
  }
  cout << -1 << "\\n";
}
void sbfs() {
  deque<pair<int, pair<int, int>>> q;
  memset(mgc, 0x3f, sizeof(mgc));
  q.push_back({0, {ch, cw}});
  mgc[ch][cw] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    int v = p.first;
    int x = p.second.first, y = p.second.second;
    int mgct = mgc[x][y];
    // 用值来判断是否有效
    if (mgct != v)
      continue;
    for (int k = 0; k < 4; k++) {
      int nx = x + xdir[k], ny = y + ydir[k];
      if (legal(nx, ny)) {
        if (mgct < mgc[nx][ny]) {
          mgc[nx][ny] = mgct;
          // 优先遍历四连通
          q.push_front({mgct, {nx, ny}});
        }
      }
    }
    for (int u = -2; u <= 2; u++) {
      for (int v = -2; v <= 2; v++) {
        int nx = x + u, ny = y + v;
        if (legal(nx, ny)) {
          if (mgct + 1 < mgc[nx][ny]) {
            mgc[nx][ny] = mgct + 1;
            q.push_back({mgc[nx][ny], {nx, ny}});
          }
        }
      }
    }
  }
  if (mgc[dh][dw] == 0x3f3f3f3f) {
    cout << -1 << "\\n";
  } else {
    cout << mgc[dh][dw] << "\\n";
  }
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W >> ch >> cw >> dh >> dw;
  for (int h = 1; h <= H; h++) {
    cin >> grid[h] + 1;
  }
  sbfs();
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define DEBUG(x) cout << #x << "=" << x << endl
#define DEBUG2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
typedef long long ll;
// #define LOCAL
const int MAXN = 1e3 + 10;
const ll MOD = 1007;
ll _gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return _gcd(b, a % b);
}
ll gcd(ll a, ll b) {
  a = abs(a), b = abs(b);
  if (a < b)
    swap(a, b);
  return _gcd(a, b);
}
ll qpow(ll a, ll n) {
  ll rt = 1;
  while (n) {
    if (n & 1)
      rt = (rt * a) % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return rt;
}
ll factor[MAXN];
void cal_factor() {
  factor[0] = 1;
  for (int u = 1; u < MAXN; u++) {
    factor[u] = (factor[u - 1] * u) % MOD;
  }
}
ll C(ll n, ll k) {
  return factor[n] * qpow(factor[n - k], MOD - 2) % MOD *
         qpow(factor[k], MOD - 2) % MOD;
}
char grid[MAXN][MAXN];
int xdir[] = {0, 0, -1, 1};
int ydir[] = {-1, 1, 0, 0};
int vis[MAXN][MAXN];
int H, W;
int ch, cw, dh, dw;
bool legal(int x, int y) {
  return x >= 1 && x <= H && y >= 1 && y <= W && grid[x][y] == '.';
}
void bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (vis[x][y])
      continue;
    vis[x][y] = 1;
    for (int k = 0; k < 4; k++) {
      int nx = x + xdir[k], ny = y + ydir[k];
      if (legal(nx, ny) && vis[nx][ny] == 0) {
        q.push({nx, ny});
      }
    }
  }
}
int rxdir[] = {-2, -1, 0, 1, 2};
int rydir[] = {-2, -1, 0, 1, 2};
int mgc[MAXN][MAXN];
int rvis[MAXN][MAXN];
void rbfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  int ans = -1;
  if (vis[sx][sy]) {
    cout << 0;
    return;
  }
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    if (rvis[x][y])
      continue;
    rvis[x][y] = 1;
    for (int u = 0; u < 5; u++) {
      int nx = x + rxdir[u];
      for (int v = 0; v < 5; v++) {
        int ny = y + rydir[v];
        if (x == nx && y == ny)
          continue;
        if (legal(nx, ny) && rvis[nx][ny] == 0) {
          if (vis[nx][ny]) {
            ans = mgc[x][y] + 1;
            cout << ans << "\\n";
            return;
          } else {
            mgc[nx][ny] = mgc[x][y] + 1;
          }
          q.push({nx, ny});
        }
      }
    }
  }
  cout << -1 << "\\n";
}
void sbfs() {
  queue<pair<int, pair<int, int>>> q;
  memset(mgc, 0x3f, sizeof(mgc));
  q.push({0, {ch, cw}});
  mgc[ch][cw] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int v = p.first;
    int x = p.second.first, y = p.second.second;
    int mgct = mgc[x][y];
    // 用值来判断是否有效
    if (mgct != v)
      continue;
    for (int k = 0; k < 4; k++) {
      int nx = x + xdir[k], ny = y + ydir[k];
      if (legal(nx, ny)) {
        if (mgct < mgc[nx][ny]) {
          mgc[nx][ny] = mgct;
          q.push({mgct, {nx, ny}});
        }
      }
    }
    for (int u = -2; u <= 2; u++) {
      for (int v = -2; v <= 2; v++) {
        int nx = x + u, ny = y + v;
        if (legal(nx, ny)) {
          if (mgct + 1 < mgc[nx][ny]) {
            mgc[nx][ny] = mgct + 1;
            q.push({mgc[nx][ny], {nx, ny}});
          }
        }
      }
    }
  }
  if (mgc[dh][dw] == 0x3f3f3f3f) {
    cout << -1 << "\\n";
  } else {
    cout << mgc[dh][dw] << "\\n";
  }
}
void solve() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W >> ch >> cw >> dh >> dw;
  for (int h = 1; h <= H; h++) {
    cin >> grid[h] + 1;
  }
  sbfs();
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
#endif
  solve();
}