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

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

typedef long long ll;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define int ll

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define D1(x)                                                                  \\
  { cerr << " [" << #x << ": " << x << "]\\n"; }
#define D2(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": ";                                                \\
    for (auto it : x)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << "]\\n";                                                             \\
  }

const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = LONG_LONG_MAX;

int n, m, a, b, c, d;
char g[1007][1007];
int vis[1007][1007];

int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
int dm[5] = {-2, -1, 0, 1, 2};
bool check(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.';
}

void solve() {
  cin >> n >> m >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  F0R(i, n) F0R(j, m) cin >> g[i][j], vis[i][j] = 0;

  deque<pair<pii, int>> q;

  q.push_front({{a, b}, 0});

  while (!q.empty()) {
    pair<pii, int> p = q.front();
    q.pop_front();
    int row = p.F.F;
    int col = p.F.S;
    int dist = p.S;

    if (vis[row][col])
      continue;
    vis[row][col] = 1;

    if (row == c && col == d) {
      cout << dist;
      return;
    }

    F0R(i, 4) {

      int curr = row + dr[i];
      int curc = col + dc[i];
      if (check(curr, curc) && !vis[curr][curc]) {
        q.push_front({{curr, curc}, dist});
        // vis[curr][curc] = 1;
      }
    }

    FOR(i, -2, 3) {
      FOR(j, -2, 3) {
        int curr = row + i;
        int curc = col + j;

        if (check(curr, curc) && !vis[curr][curc]) {
          q.push_back({{curr, curc}, dist + 1});
          // vis[curr][curc] = 1;
        }
      }
    }
  }
  // F0R(i, n) {F0R(j, m) cerr << vis[i][j] << ' '; cerr << '\\n';}
  cout << -1;
}

int32_t main() {
  _

#ifndef ONLINE_JUDGE
      freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  // tc(t)
  solve();
  cerr << "[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define int ll

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define D1(x)                                                                  \\
  { cerr << " [" << #x << ": " << x << "]\\n"; }
#define D2(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": ";                                                \\
    for (auto it : x)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << "]\\n";                                                             \\
  }

const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = LONG_LONG_MAX;

int n, m, a, b, c, d;
char g[1007][1007];
int vis[1007][1007];

int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
int dm[5] = {-2, -1, 0, 1, 2};
bool check(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.';
}

void solve() {
  cin >> n >> m >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  F0R(i, n) F0R(j, m) cin >> g[i][j], vis[i][j] = 0;

  deque<pair<pii, int>> q;

  q.push_front({{a, b}, 0});

  while (!q.empty()) {
    pair<pii, int> p = q.front();
    q.pop_front();
    int row = p.F.F;
    int col = p.F.S;
    int dist = p.S;

    if (vis[row][col])
      continue;
    vis[row][col] = 1;

    if (row == c && col == d) {
      cout << dist;
      return;
    }

    F0R(i, 4) {

      int curr = row + dr[i];
      int curc = col + dc[i];
      if (check(curr, curc) && !vis[curr][curc]) {
        q.push_front({{curr, curc}, dist});
        // vis[curr][curc] = 1;
      }
    }

    FOR(i, -2, 3) {
      FOR(j, -2, 3) {
        int curr = row + i;
        int curc = col + j;

        if (check(curr, curc) && !vis[curr][curc]) {
          q.push_back({{curr, curc}, dist + 1});
          // vis[curr][curc] = 1;
        }
      }
    }
  }
  F0R(i, n) {
    F0R(j, m) cerr << vis[i][j] << ' ';
    cerr << '\\n';
  }
  cout << -1;
}

int32_t main() {
  _

#ifndef ONLINE_JUDGE
      freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif

  // tc(t)
  solve();
  cerr << "[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
}