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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
char a[1010][1010];
int mn[1010][1010];
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
int n, m;
int check(int x, int y, int k) {
  if (x < 0 || y < 0 || x >= n || y >= m)
    return 0;
  return a[x][y] == '.' && mn[x][y] > k;
}
queue<pii> Q, T;
int main() {
  int i, j, x1, y1, x2, y2;
  memset(mn, 0x3f, sizeof(mn));
  scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2);
  x1--, x2--, y1--, y2--;
  for (i = 0; i < n; i++)
    scanf("%s", a[i]);
  mn[x1][y1] = 0;
  T.push(mp(x1, y1));
  while (!T.empty()) {
    int x = T.front().fi, y = T.front().se;
    T.pop();
    Q.push(mp(x, y));
    while (!Q.empty()) {
      int x = Q.front().fi, y = Q.front().se;
      Q.pop();
      int tx, ty, temp = mn[x][y];
      for (i = 0; i < 4; i++) {
        tx = x + dx[i];
        ty = y + dy[i];
        if (check(tx, ty, temp)) {
          mn[tx][ty] = temp;
          Q.push(mp(tx, ty));
          T.push(mp(tx, ty));
        }
      }
    }
    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        int tx, ty, temp = mn[x][y];
        tx = x + i;
        ty = y + j;
        if (check(tx, ty, temp + 1)) {
          mn[tx][ty] = temp + 1;
          T.push(mp(tx, ty));
        }
      }
    }
  }
  printf("%d", mn[x2][y2] > 1e8 ? -1 : mn[x2][y2]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
char a[1010][1010];
int mn[1010][1010];
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
int n, m;
int check(int x, int y, int k) {
  if (x < 0 || y < 0 || x >= n || y >= m)
    return 0;
  return a[x][y] == '.' && mn[x][y] > k;
}
queue<pair<int, pii>> Q;
int main() {
  int i, j, x1, y1, x2, y2;
  memset(mn, 0x3f, sizeof(mn));
  scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2);
  x1--, x2--, y1--, y2--;
  for (i = 0; i < n; i++)
    scanf("%s", a[i]);
  if (a[x1][x2] == '#')
    return printf("-1"), 0;
  mn[x1][y1] = 0;
  Q.push(mp(0, mp(x1, y1)));
  while (!Q.empty()) {
    int last = Q.front().fi;
    int x = Q.front().se.fi, y = Q.front().se.se;
    Q.pop();
    int temp = mn[x][y];
    if (last != temp)
      continue;
    int tx, ty;
    for (i = 0; i < 4; i++) {
      tx = x + dx[i];
      ty = y + dy[i];
      if (check(tx, ty, temp)) {
        mn[tx][ty] = temp;
        Q.push(mp(temp, mp(tx, ty)));
      }
    }
    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        tx = x + i;
        ty = y + j;
        if (check(tx, ty, temp + 1)) {
          mn[tx][ty] = temp + 1;
          Q.push(mp(temp + 1, mp(tx, ty)));
        }
      }
    }
  }
  printf("%d", mn[x2][y2] > 1e8 ? -1 : mn[x2][y2]);
  return 0;
}