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

#include <iostream>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;

const int INF = 1e9;
int H, W, c_i, c_j, d_i, d_j, cost[1010][1010];
string S[1010];

int main() {

  cin >> H >> W >> c_i >> c_j >> d_i >> d_j;
  for (int i = 0; i < H; ++i) {
    cin >> S[i];
  }

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cost[i][j] = INF;
    }
  }

  deque<pair<int, int>> q;
  cost[c_i - 1][c_j - 1] = 0;
  q.push_front(make_pair(c_i - 1, c_j - 1));

  while (!q.empty()) {
    auto p = q.front();
    q.pop_front();
    int i = p.first;
    int j = p.second;

    if (i >= 1 && S[i - 1][j] == '.' && cost[i - 1][j] > cost[i][j]) {
      cost[i - 1][j] = cost[i][j];
      q.push_front(make_pair(i - 1, j));
    }
    if (i <= H - 2 && S[i + 1][j] == '.' && cost[i + 1][j] > cost[i][j]) {
      cost[i + 1][j] = cost[i][j];
      q.push_front(make_pair(i + 1, j));
    }
    if (j >= 1 && S[i][j - 1] == '.' && cost[i][j - 1] > cost[i][j]) {
      cost[i][j - 1] = cost[i][j];
      q.push_front(make_pair(i, j - 1));
    }
    if (j <= W - 2 && S[i][j + 1] == '.' && cost[i][j + 1] > cost[i][j]) {
      cost[i][j + 1] = cost[i][j];
      q.push_front(make_pair(i, j + 1));
    }

    for (int k = max(0, i - 2); k <= min(i + 2, H - 1); ++k) {
      for (int l = max(0, j - 2); l <= min(j + 2, W - 1); ++l) {
        if (S[k][l] == '.' && cost[k][l] > cost[i][j] + 1) {
          cost[k][l] = cost[i][j] + 1;
          q.push_back(make_pair(k, l));
        }
      }
    }
  }
  if (cost[d_i - 1][d_j - 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[d_i - 1][d_j - 1] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define in1(n) cin >> n;
#define in2(n, k) cin >> n >> k;
#define in3(n, k, s) cin >> n >> k >> s;
#define scn(t) scanf("%d", &t)
#define scnld(t) scanf("%ld", &t)
#define scnlld(t) scanf("%lld", &t)
#define scnc(t) scanf("%c", &t)
#define scns(t) scanf("%s", t)
#define scnf(t) scanf("%f", &t)
#define scnlf(t) scanf("%lf", &t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define nloop(i, j, k, in) for (int i = j; i < k; i += in)
#define rloop(i, j, k, in) for (int i = j; i >= k; i -= in)
#define rep(i, j) nloop(i, 0, j, 1)
#define rrep(i, j) rloop(i, j, 0, 1)
#define inar(n, a) rep(i, n) cin >> a[i];
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define MP make_pair
#define PB push_back
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

int cost[1010][1010];
string s[1010];

int main() {
  fast int h, w, ci, cj, di, dj;
  in3(h, w, ci) in3(cj, di, dj) rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) cost[i][j] = INF;
  deque<PII> q;
  ci--, cj--;
  q.push_front(MP(ci, cj));
  cost[ci][cj] = 0;
  while (!q.empty()) {
    auto a = q.front();
    q.pop_front();
    int i = a.first;
    int j = a.second;
    if (i > 0 && s[i - 1][j] == '.' && cost[i - 1][j] > cost[i][j])
      cost[i - 1][j] = cost[i][j], q.push_front(MP(i - 1, j));
    if (i < h - 1 && s[i + 1][j] == '.' && cost[i + 1][j] > cost[i][j])
      cost[i + 1][j] = cost[i][j], q.push_front(MP(i + 1, j));
    if (j > 0 && s[i][j - 1] == '.' && cost[i][j - 1] > cost[i][j])
      cost[i][j - 1] = cost[i][j], q.push_front(MP(i, j - 1));
    if (j < w - 1 && s[i][j + 1] == '.' && cost[i][j + 1] > cost[i][j])
      cost[i][j + 1] = cost[i][j], q.push_front(MP(i, j + 1));
    for (int k = max(0, i - 2); k <= min(h - 1, i + 2); ++k)
      for (int l = max(0, j - 2); l <= min(w - 1, j + 2); ++l)
        if (s[k][l] == '.' && cost[k][l] > cost[i][j] + 1) {
          cost[k][l] = cost[i][j] + 1;
          q.push_front(MP(k, l));
        }
  }
  di--, dj--;
  if (cost[di][dj] == INF)
    cout << "-1" << endl;
  else
    cout << cost[di][dj] << endl;
  return 0;
}