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

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

#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\\n"
#define mp(x, y) make_pair(x, y)

const int maxn = 5e5 + 10;
const int mod = 998244353;
const double pi = acos(-1);

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int lcm(int a, int b) { return a * b / gcd(a, b); }

int qpow(int a, int n) {
  int b = 1;
  while (n) {
    if (n & 1)
      b = a * b % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}

class Prime_Number {
public:
  int prime[maxn], s, v[maxn];
  void prime_init(int n) {
    for (int i = 2; i <= n; i++) {
      if (!v[i]) {
        prime[s++] = i;
        for (int j = 1; j * i <= n; j++)
          v[i * j] = 1;
      }
    }
  }
};

//**************************
int nex1[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}},
    nex2[20][2] = {{1, 1},   {1, -1}, {-1, 1}, {-1, -1}, {-2, -2},
                   {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},  {-1, 2},
                   {-1, -2}, {0, 2},  {0, -2}, {1, 2},   {1, -2},
                   {2, 2},   {2, 1},  {2, 0},  {2, -1},  {2, -2}};
int d[2005][2005];
char Map[20005][2005];
int n, m;
inline int check(int x, int y) {
  if (x <= 0 || x > n || y <= 0 || y > m || Map[x][y] == '#')
    return 0;
  return 1;
}
void bfs(int a, int b) {
  d[a][b] = 0;
  deque<pair<int, int>> que;
  que.push_back(mp(a, b));
  while (!que.empty()) {
    int x, y;
    x = que.front().first, y = que.front().second;
    que.pop_front();
    for (int i = 0; i < 4; i++) {
      int _x = x + nex1[i][0], _y = y + nex1[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y]) {
        que.push_front(mp(_x, _y));
        d[_x][_y] = d[x][y];
      }
    }
    for (int i = 0; i < 20; i++) {
      int _x = x + nex2[i][0], _y = y + nex2[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y] + 1) {
        d[_x][_y] = d[x][y] + 1;
        que.push_back(mp(_x, _y));
      }
    }
  }
}
void init() {
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      d[i][j] = 1e9;
}
void solve() {
  sll2(n, m);
  int x, y, tox, toy;
  sll2(x, y), sll2(tox, toy);
  init();
  getchar();
  for (int i = 1; i <= n; i++)
    scanf("%s", Map[i] + 1);
  bfs(x, y);
  if (d[tox][toy] == 1e9)
    cout << -1 << endl;
  else
    cout << d[tox][toy] << endl;
}
signed main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

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

#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
#define mem(a, i) memset(a, i, sizeof(a))
#define pb push_back
#define int long long
#define ll long long
#define lson node << 1
#define rson (node << 1) + 1
#define endl "\\n"
#define mp(x, y) make_pair(x, y)

const int maxn = 5e5 + 10;
const int mod = 998244353;
const double pi = acos(-1);

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int lcm(int a, int b) { return a * b / gcd(a, b); }

int qpow(int a, int n) {
  int b = 1;
  while (n) {
    if (n & 1)
      b = a * b % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return b;
}

class Prime_Number {
public:
  int prime[maxn], s, v[maxn];
  void prime_init(int n) {
    for (int i = 2; i <= n; i++) {
      if (!v[i]) {
        prime[s++] = i;
        for (int j = 1; j * i <= n; j++)
          v[i * j] = 1;
      }
    }
  }
};

//**************************
int nex1[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}},
    nex2[20][2] = {{1, 1},   {1, -1}, {-1, 1}, {-1, -1}, {-2, -2},
                   {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2},  {-1, 2},
                   {-1, -2}, {0, 2},  {0, -2}, {1, 2},   {1, -2},
                   {2, 2},   {2, 1},  {2, 0},  {2, -1},  {2, -2}};
int v[2005][2005];
int d[2005][2005];
char Map[20005][2005];
int n, m;
int check(int x, int y) {
  if (x <= 0 || x > n || y <= 0 || y > m || Map[x][y] == '#')
    return 0;
  return 1;
}
void bfs(int a, int b) {
  d[a][b] = 0;
  queue<pair<int, int>> que;
  que.push(mp(a, b));
  v[a][b] = 1;
  while (!que.empty()) {
    int x, y;
    x = que.front().first, y = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int _x = x + nex1[i][0], _y = y + nex1[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y]) {
        v[_x][_y] = 1;
        que.push(mp(_x, _y));
        d[_x][_y] = d[x][y];
      }
    }
    for (int i = 0; i < 20; i++) {
      int _x = x + nex2[i][0], _y = y + nex2[i][1];
      if (check(_x, _y) && d[_x][_y] > d[x][y] + 1) {
        d[_x][_y] = d[x][y] + 1;
        que.push(mp(_x, _y));
      }
    }
  }
}
void init() {
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++)
      d[i][j] = 1e9;
}
void solve() {
  cin >> n >> m;
  int x, y, tox, toy;
  cin >> x >> y >> tox >> toy;
  init();
  getchar();
  for (int i = 1; i <= n; i++)
    scanf("%s", Map[i] + 1);
  bfs(x, y);
  if (d[tox][toy] == 1e9)
    cout << -1 << endl;
  else
    cout << d[tox][toy] << endl;
}
signed main() {
  solve();
  return 0;
}