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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1ll, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}

int main(void) {
  int h, w, sx, sy, ex, ey;
  cin >> h >> w >> sy >> sx >> ey >> ex;
  sx--, sy--, ex--, ey--;
  vector<string> s(h);
  vector<vector<int>> c(h, vector<int>(w, -1));
  rep(i, 0, h) cin >> s[i];
  deque<pair<int, int>> path, path2;
  path.push_back(make_pair(sx, sy));
  c[sy][sx] = 0;
  const int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
  int C = 0;
  while (1) {
    while (path.size()) {
      auto it = path.front();
      if (it.first == ex && it.second == ey) {
        printf("%d\\n", c[ey][ex]);
        return 0;
      }
      path.pop_front();
      rep(i, 0, 4) {
        int tx = it.first + dx[i], ty = it.second + dy[i];
        if (tx < 0 || tx >= w || ty < 0 || ty >= h ||
            c[ty][tx] != -1 && c[ty][tx] <= c[it.second][it.first])
          continue;
        if (s[ty][tx] == '.') {
          c[ty][tx] = c[it.second][it.first];
          path.push_back(make_pair(tx, ty));
        }
      }
      rep(i, -2, 3) {
        rep(i2, -2, 3) {
          int tx = it.first + i, ty = it.second + i2;
          if (tx < 0 || tx >= w || ty < 0 || ty >= h ||
              c[ty][tx] != -1 && c[ty][tx] <= c[it.second][it.first] + 1)
            continue;
          if (s[ty][tx] == '.') {
            c[ty][tx] = c[it.second][it.first] + 1;
            path2.push_back(make_pair(tx, ty));
          }
        }
      }
    }
    if (path2.empty())
      break;
    path = path2;
    path2.clear();
  }
  puts("-1");
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (auto i = a; i < b; i++)
#define rep2(i, a) for (auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x *= x;
      p /= 2;
    } else {
      a *= x;
      p--;
    }
  }
  return a;
}
ll mpower(ll x, ll p) {
  ll a = 1;
  while (p > 0) {
    if (p % 2 == 0) {
      x = x * x % mod;
      p /= 2;
    } else {
      a = a * x % mod;
      p--;
    }
  }
  return a;
}
ll co(ll n, ll k) {
  ll a = 1;
  rep(i, 1ll, k) {
    a *= n - i + 1;
    a /= i;
  }
  return a;
}
ll mc(ll n, ll m) {
  ll k = 1, l = 1;
  rep(i, n - m + 1, n + 1) k = k * i % mod;
  rep(i, 1, m + 1) l = l * i % mod;
  l = mpower(l, mod - 2);
  return k * l % mod;
}

int main(void) {
  int h, w, sx, sy, ex, ey;
  cin >> h >> w >> sy >> sx >> ey >> ex;
  sx--, sy--, ex--, ey--;
  vector<string> s(h);
  vector<vector<int>> c(h, vector<int>(w, -1));
  rep(i, 0, h) cin >> s[i];
  deque<pair<int, int>> path;
  path.push_back(make_pair(sx, sy));
  c[sy][sx] = 0;
  const int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
  int C = 0;
  while (path.size()) {
    while (path.size()) {
      auto it = path.front();
      /*if (it.first == ex && it.second == ey) {
              C = 1;
      }
      if (C)C++;
      if (C == 900000)break;*/
      path.pop_front();
      rep(i, 0, 4) {
        int tx = it.first + dx[i], ty = it.second + dy[i];
        if (tx < 0 || tx >= w || ty < 0 || ty >= h ||
            c[ty][tx] != -1 && c[ty][tx] <= c[it.second][it.first])
          continue;
        if (s[ty][tx] == '.') {
          c[ty][tx] = c[it.second][it.first];
          path.push_back(make_pair(tx, ty));
        }
      }
    }
    rep(x, 0, w) {
      rep(y, 0, h) {
        if (s[y][x] == '.' && c[y][x] != -1) {
          rep(i, -2, 3) {
            rep(i2, -2, 3) {
              int tx = x + i, ty = y + i2;
              if (tx < 0 || tx >= w || ty < 0 || ty >= h ||
                  c[ty][tx] != -1 && c[ty][tx] <= c[y][x] + 1)
                continue;
              if (s[ty][tx] == '.') {
                c[ty][tx] = c[y][x] + 1;
                path.push_back(make_pair(tx, ty));
              }
            }
          }
        }
      }
    }
  }
  printf("%d\\n", c[ey][ex]);
  return 0;

  return 0;
}