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

#include <bits/stdc++.h>

using namespace std;

int n, m, sx, sy, tx, ty;
int x[1005][1005];
char y[1005][1005];
deque<pair<int, int>> q;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &tx, &ty);
  for (int i = 1; i <= n; i++)
    scanf("%s", y[i] + 1);
  x[sx][sy] = 1;
  q.emplace_back(sx, sy);
  while (!q.empty()) {
    tie(sx, sy) = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int ta = sx + dx[i];
      int tb = sy + dy[i];
      if (y[ta][tb] == '.' && (!x[ta][tb] || x[sx][sy] < x[ta][tb])) {
        x[ta][tb] = x[sx][sy];
        q.emplace_front(ta, tb);
      }
    }
    for (int i = sx - 2; i <= sx + 2; i++)
      for (int j = sy - 2; j <= sy + 2; j++) {
        if (y[i][j] == '.' && (!x[i][j] || x[i][j] > x[sx][sy])) {
          x[i][j] = x[sx][sy] + 1;
          q.emplace_back(i, j);
        }
      }
  }
  // for (int i = 1; i <= n; i++) {for (int j = 1; j <= m; j++) printf("%d",
  // x[i][j]); puts("");}
  printf("%d\\n", x[tx][ty] ? x[tx][ty] - 1 : -1);
}

Error: TLE

Fixed submission:

/**
 *    author:  Daredevil666
 *    institution: IIT Patna
 **/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout << "\\n";
#define ll long long int
#define REP(i, a, n) for (i = a; i <= n; i++)
#define F(i, a, b) for (i = a; i < b; i++)
#define RF(i, b, a) for (i = b; i >= a; i--)
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const ll mod = 1e9 + 7;
const ll MAXN = 100200;
ll i, j;
int main() {
  IOS
      /*#ifndef ONLINE_JUDGE
         freopen("input.txt","r",stdin);
         freopen("output.txt","w",stdout);
      #endif // ONLINE_JUDGE*/
      ll n,
      m;
  cin >> n >> m;
  ll x, y;
  cin >> x >> y;
  ll u, v;
  cin >> u >> v;
  char a[n + 2][m + 2];
  ll dp[n + 2][m + 2], vis[n + 2][m + 2];
  memset(vis, 0, sizeof(vis));
  F(i, 0, n) {
    F(j, 0, m) {
      cin >> a[i][j];
      dp[i][j] = mod;
    }
  }
  dp[x - 1][y - 1] = 0;
  ll xc[] = {1, -1, 0, 0};
  ll yc[] = {0, 0, 1, -1};
  queue<pair<ll, ll>> t, q;
  // t.push(make_pair(x-1,y-1));
  q.push(make_pair(x - 1, y - 1));
  vis[x - 1][y - 1] = 1;
  /*while(!t.empty())
  {
      pair<ll,ll> p=t.front();
      t.pop();
      F(i,0,4)
      {
          ll r=p.first+xc[i],s=p.second+yc[i];
          if(r<0 || r>=n || s<0 || s>=m)continue;
          if(a[r][s]=='.' && vis[r][s]==0)
          {
              vis[r][s]=1;
              dp[r][s]=0;
              q.push(make_pair(r,s));
             // cout<<r<<" "<<s;nl
               t.push(make_pair(r,s));
          }
      }
  }*/
  ll k, g;
  while (!q.empty()) {
    pair<ll, ll> p = q.front();
    q.pop();
    F(k, p.first - 2, p.first + 3) {
      F(g, p.second - 2, p.second + 3) {
        if (k < 0 || k >= n || g < 0 || g >= m ||
            (k == p.first && g == p.second))
          continue;
        if (a[k][g] == '.') {
          /*if(vis[k][g]==0)
          {
              vis[k][g]=1;*/
          // cout<<k<<" "<<g;nl
          if (abs(p.first - k) + abs(p.second - g) == 1) {
            if (dp[k][g] > dp[p.first][p.second]) {
              dp[k][g] = min(dp[k][g], dp[p.first][p.second]);
              q.push(make_pair(k, g));
            }
          } else {
            if (dp[k][g] > dp[p.first][p.second] + 1) {
              dp[k][g] = min(dp[k][g], dp[p.first][p.second] + 1);
              q.push(make_pair(k, g));
            }
          }
          /*}
          else
          {
             if(abs(p.first-k)+abs(p.second-g)==1)
              {
                  dp[k][g]=min(dp[k][g],dp[p.first][p.second]);
              }
              else
              dp[k][g]=min(dp[k][g],dp[p.first][p.second]+1);
          }*/
        }
      }
    }
  }
  if (dp[u - 1][v - 1] == mod)
    cout << -1;
  else
    cout << dp[u - 1][v - 1];
  return 0;
}