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

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <memory.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int n, m, sx, sy, ex, ey;
char a[1005][1005];
int d[1005][1005];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

bool check(int x, int y) {
  if (x < 1 || y < 1 || x > n || y > m || a[x][y] == '#')
    return false;
  return true;
}
void bfs() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      d[i][j] = 1e9;
  d[sx][sy] = 0;
  deque<pair<int, int>> q;
  q.push_front(make_pair(sx, sy));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int x1 = x + dir[i][0], y1 = y + dir[i][1];
      if (check(x1, y1) && d[x1][y1] > d[x][y])
        q.push_front(make_pair(x1, y1)), d[x1][y1] = d[x][y];
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x1 = x + i, y1 = y + j;
        if (check(x1, y1) && d[x1][y1] > d[x][y] + 1) {
          q.push_back(make_pair(x1, y1));
          d[x1][y1] = d[x][y] + 1;
        }
      }
    }
  }
}

int main() {
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  bfs();
  if (d[ex][ey] == 1e9)
    cout << -1;
  else
    cout << d[ex][ey];
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
int n, m, sx, sy, ex, ey;
char a[1005][1005];
int vis[1005][1005], flag = 0, vis1[1005][1005], d[maxn];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

bool check(int x, int y) {
  if (x < 1 || y < 1 || x > n || y > m || a[x][y] == '#')
    return false;
  return true;
}
void dfs(int x, int y) {
  vis[x][y] = flag;
  for (int i = 0; i < 4; i++) {
    int x1 = x + dir[i][0], y1 = y + dir[i][1];
    if (!vis[x1][y1] && check(x1, y1))
      dfs(x1, y1);
  }
}
void bfs() {
  for (int i = 0; i < maxn; i++)
    d[i] = 1e9;
  d[vis[sx][sy]] = 0;
  queue<pair<int, int>> q;
  q.push(make_pair(sx, sy));
  vis1[sx][sy] = 1;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x1 = x + dir[i][0], y1 = y + dir[i][1];
      if (check(x1, y1) && !vis1[x1][y1])
        q.push(make_pair(x1, y1)), vis1[x1][y1] = 1;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int x1 = x + i, y1 = y + j;
        if (check(x1, y1) && vis[x1][y1] != vis[x][y])
          d[vis[x1][y1]] = min(d[vis[x1][y1]], d[vis[x][y]] + 1);
        if (check(x1, y1) && !vis1[x1][y1] && vis[x1][y1] != vis[x][y]) {
          q.push(make_pair(x1, y1));
          d[vis[x1][y1]] = min(d[vis[x1][y1]], d[vis[x][y]] + 1);
        }
      }
    }
  }
}

int main() {
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '.' && !vis[i][j]) {
        flag++;
        dfs(i, j);
      }
    }
  }
  bfs();
  if (d[vis[ex][ey]] == 1e9)
    cout << -1;
  else
    cout << d[vis[ex][ey]];
  return 0;
}