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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define qi queue<int>
#define mii map<int, int>
#define msi map<string, int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep3(i, a, n) for (ll i = (a); i < (n); i++)
#define rep4(i, a, n) for (ll i = (a)-1; i >= n; i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n, 0, sizeof(n));
#define SZ(n) n.size()
#define sn st.nd
ll dx[4] = {0, -1, 0, 1};
ll dy[4] = {1, 0, -1, 0};
/****************************/
char c[1010][1010];
int H, W;
int h1, w1, h2, w2;
bool onfield(int x, int y) { return x >= 1 && x <= H && y >= 1 && y <= W; }
int used[1010][1010];
int main() {
  memset(used, -1, sizeof(used));
  cin >> H >> W;
  cin >> h1 >> w1;
  cin >> h2 >> w2;
  rep3(i, 1, H + 1) {
    rep3(j, 1, W + 1) { cin >> c[i][j]; }
  }
  deque<pii> q;
  q.push_back(mp(h1, w1));
  used[h1][w1] = 0;
  // bool flag=false;
  while (!q.empty()) {
    pii p = q.front();
    q.pop_front();
    int x = p.st, y = p.nd;
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!onfield(xx, yy) ||
          (used[xx][yy] != -1 && used[xx][yy] <= used[x][y]) ||
          c[xx][yy] == '#')
        continue;
      used[xx][yy] = used[x][y];
      // if(xx==h2&&yy==w2&&used[xx][yy]) flag=true;
      /*if(!flag) */ q.push_front(mp(xx, yy));
    }
    rep3(i, x - 2, x + 3) {
      if (i > H)
        break;
      rep3(j, y - 2, y + 3) {
        if (j > W)
          break;
        if (!onfield(i, j) ||
            (used[i][j] != -1 && used[i][j] <= used[x][y] + 1) ||
            c[i][j] == '#' || (i == x && abs(j - y) <= 1) ||
            (j == y && abs(i - x) <= 1))
          continue;
        used[i][j] = used[x][y] + 1;
        /*if(!flag) */ q.push_back(mp(i, j));
      }
    }
  }
  cout << used[h2][w2];
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define qi queue<int>
#define mii map<int, int>
#define msi map<string, int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep3(i, a, n) for (ll i = (a); i < (n); i++)
#define rep4(i, a, n) for (ll i = (a)-1; i >= n; i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n, 0, sizeof(n));
#define SZ(n) n.size()
#define sn st.nd
ll dx[4] = {0, -1, 0, 1};
ll dy[4] = {1, 0, -1, 0};
/****************************/
char c[1010][1010];
int H, W;
int h1, w1, h2, w2;
bool onfield(int x, int y) { return x >= 1 && x <= H && y >= 1 && y <= W; }
bool vis[1010][1010];
bool dfs(int x, int y, int tar1, int tar2) {
  vis[x][y] = true;
  if (x == tar1 && y == tar2)
    return true;
  rep(i, 4) {
    int xx = x + dx[i], yy = y + dy[i];
    if (!onfield(xx, yy) || c[xx][yy] == '#' || vis[xx][yy])
      continue;
    if (dfs(xx, yy, tar1, tar2))
      return true;
  }
  return false;
}
int used[1010][1010];
int main() {
  memset(used, -1, sizeof(used));
  cin >> H >> W;
  cin >> h1 >> w1;
  cin >> h2 >> w2;
  rep3(i, 1, H + 1) {
    rep3(j, 1, W + 1) { cin >> c[i][j]; }
  }
  queue<pii> q;
  while (!q.empty())
    q.pop();
  q.push(mp(h1, w1));
  used[h1][w1] = 0;
  // bool flag=false;
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    int x = p.st, y = p.nd;
    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!onfield(xx, yy) ||
          (used[xx][yy] != -1 && used[xx][yy] <= used[x][y]) ||
          c[xx][yy] == '#')
        continue;
      used[xx][yy] = used[x][y];
      // if(xx==h2&&yy==w2&&used[xx][yy]) flag=true;
      /*if(!flag) */ q.push(mp(xx, yy));
    }
    rep3(i, x - 2, x + 3) {
      if (i > H)
        break;
      rep3(j, y - 2, y + 3) {
        if (j > W)
          break;
        if (!onfield(i, j) ||
            (used[i][j] != -1 && used[i][j] <= used[x][y] + 1) ||
            c[i][j] == '#' || (i == x && abs(j - y) <= 1) ||
            (j == y && abs(i - x) <= 1))
          continue;
        zero(vis);
        if (dfs(x, y, i, j))
          continue;
        used[i][j] = used[x][y] + 1;
        /*if(!flag) */ q.push(mp(i, j));
      }
    }
  }
  cout << used[h2][w2];
  return 0;
}