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

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 1005;
char s[N][N];
int n, m, sx, sy, ex, ey;
bool vis[N][N];
int dis[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct node {
  int x, y, v;
  node(int x = 0, int y = 0, int v = 0) : x(x), y(y), v(v) {}
  bool operator<(const node &o) const { return v > o.v; }
};
priority_queue<node> q;
int main() {
  scanf("%d%d%d%d%d%d", &n, &m, &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  memset(dis, inf, sizeof(dis));
  q.push(node(sx, sy, dis[sx][sy]));
  dis[sx][sy] = 0;
  while (!q.empty()) {
    int x = q.top().x, y = q.top().y;
    q.pop();
    if (vis[x][y])
      continue;
    vis[x][y] = true;
    for (int i = 0; i < 4; i++) {
      int xx = dx[i] + x;
      int yy = dy[i] + y;
      if (xx < 1 || xx > n || yy < 1 || yy > m || s[xx][yy] == '#')
        continue;
      if (dis[xx][yy] > dis[x][y]) {
        dis[xx][yy] = dis[x][y];
        q.push(node(xx, yy, dis[xx][yy]));
      }
    }
    for (int dx = -2; dx <= 2; dx++)
      for (int dy = -2; dy <= 2; dy++) {
        int xx = dx + x;
        int yy = dy + y;
        if (xx < 1 || xx > n || yy < 1 || yy > m || s[xx][yy] == '#')
          continue;
        if (dis[xx][yy] > dis[x][y] + 1) {
          dis[xx][yy] = dis[x][y] + 1;
          q.push(node(xx, yy, dis[xx][yy]));
        }
      }
  }
  if (dis[ex][ey] == inf)
    dis[ex][ey] = -1;
  printf("%d\\n", dis[ex][ey]);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f
#define pi 3.1415926535898
using namespace std;
const int N = 3e5 + 10;
const int mod = 1e9 + 7;
char mp[2000][2000];
int dis[2020][2020];
int h, w, x11, y11, x2, y2;
int sum = inf;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void bfs(int x, int y, int muj) {
  if (x == x2 && y == y2) {
    sum = min(sum, muj);
    return;
  }
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && mp[xx][yy] != '#' &&
        dis[xx][yy] == 0) {
      dis[xx][yy] = 1;
      bfs(xx, yy, muj);
      dis[xx][yy] = 0;
    }
  }
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int xx = x + i;
      int yy = y + j;
      if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && mp[xx][yy] != '#' &&
          dis[xx][yy] == 0) {
        dis[xx][yy] = 1;
        bfs(xx, yy, muj + 1);
        dis[xx][yy] = 0;
      }
    }
  }
}
int main() {
  cin >> h >> w;
  cin >> x11 >> y11;
  cin >> x2 >> y2;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> mp[i][j];
    }
  }
  dis[x11][y11] = 1;
  bfs(x11, y11, 0);
  if (sum == inf) {
    cout << -1;
  } else {
    cout << sum << endl;
  }
  return 0;
}