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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1e3 + 5;
int n, m, x_1, y_1, x_2, y_2;
char s[MAXN][MAXN];
int vis[MAXN][MAXN];
int move_1_x[10] = {0, 1, 0, -1, 0};
int move_1_y[10] = {0, 0, 1, 0, -1};
struct node {
  int x, y, time;
  node() {}
  node(int X, int Y, int TIME) { x = X, y = Y, time = TIME; }
  friend bool operator<(node X, node Y) { return X.time > Y.time; }
};
priority_queue<node> Q;
bool check(int x, int y, int time) {
  if (x < 1 || y < 1 || x > n || y > m || vis[x][y] <= time || s[x][y] == '#')
    return false;
  return true;
}
void bfs() {
  int Time = 0x3f3f3f3f;
  memset(vis, 0x3f, sizeof(vis));
  Q.push(node(x_1, y_1, 0));
  vis[x_1][y_1] = 0;
  while (!Q.empty()) {
    node f = Q.top();
    Q.pop();
    for (int i = 1; i <= 4; i++) {
      int nx = f.x + move_1_x[i];
      int ny = f.y + move_1_y[i];
      int time = f.time;
      if (check(nx, ny, time)) {
        vis[nx][ny] = time;
        // printf("%d %d\\n",nx,ny);
        if (nx == x_2 && ny == y_2) {
          Time = min(Time, time);
        }
        Q.push(node(nx, ny, time));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int nx = f.x + i;
        int ny = f.y + j;
        int time = f.time + 1;
        if (check(nx, ny, time)) {
          vis[nx][ny] = time;
          if (nx == x_2 && ny == y_2) {
            Time = min(Time, time);
          }
          Q.push(node(nx, ny, time));
        }
      }
    }
  }
  if (Time == 0x3f3f3f3f)
    printf("-1");
  else
    printf("%d", Time);
}
int main() {
  scanf("%d %d %d %d %d %d", &n, &m, &x_1, &y_1, &x_2, &y_2);
  for (int i = 1; i <= n; i++) {
    scanf("\\n");
    for (int j = 1; j <= m; j++) {
      scanf("%c", &s[i][j]);
    }
  }
  bfs();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1e3 + 5;
int n, m, x_1, y_1, x_2, y_2;
char s[MAXN][MAXN];
int vis[MAXN][MAXN];
int move_1_x[10] = {0, 1, 0, -1, 0};
int move_1_y[10] = {0, 0, 1, 0, -1};
struct node {
  int x, y, time;
  node() {}
  node(int X, int Y, int TIME) { x = X, y = Y, time = TIME; }
};
queue<node> Q;
bool check(int x, int y, int time) {
  if (x < 1 || y < 1 || x > n || y > m || vis[x][y] <= time || s[x][y] == '#')
    return false;
  return true;
}
void bfs() {
  int Time = 0x3f3f3f3f;
  memset(vis, 0x3f, sizeof(vis));
  Q.push(node(x_1, y_1, 0));
  vis[x_1][y_1] = 0;
  while (!Q.empty()) {
    node f = Q.front();
    Q.pop();
    for (int i = 1; i <= 4; i++) {
      int nx = f.x + move_1_x[i];
      int ny = f.y + move_1_y[i];
      int time = f.time;
      if (check(nx, ny, time)) {
        vis[nx][ny] = time;
        // printf("%d %d\\n",nx,ny);
        if (nx == x_2 && ny == y_2) {
          Time = min(Time, time);
        }
        Q.push(node(nx, ny, time));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int nx = f.x + i;
        int ny = f.y + j;
        int time = f.time + 1;
        if (check(nx, ny, time)) {
          vis[nx][ny] = time;
          if (nx == x_2 && ny == y_2) {
            Time = min(Time, time);
          }
          Q.push(node(nx, ny, time));
        }
      }
    }
  }
  if (Time == 0x3f3f3f3f)
    printf("-1");
  else
    printf("%d", Time);
}
int main() {
  scanf("%d %d %d %d %d %d", &n, &m, &x_1, &y_1, &x_2, &y_2);
  for (int i = 1; i <= n; i++) {
    scanf("\\n");
    for (int j = 1; j <= m; j++) {
      scanf("%c", &s[i][j]);
    }
  }
  bfs();
  return 0;
}