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

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
#define inf 1e6 + 18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define mod 1000000007
#define f1(i, n) for (i = 1; i <= n; i++)
#define f0(i, n) for (i = 0; i < n; i++)
#define w(x) while (x--)
const int maxn = 1004;
#define pq priority_queue
#define ff first
#define ss second
#define pb push_back
#define lb lower_bound
// Lordvishnu@almighty
char ch[maxn][maxn];
int vis[maxn][maxn];
int dp[maxn][maxn], n, m, sx, sy, ex, ey;
int dx1[] = {-1, 1, 0, 0};
int dy1[] = {0, 0, -1, 1};
int dx[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
            0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
int dy[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
            2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
bool is_valid(int x, int y) {
  if (x <= 0 || y <= 0 || y > m || x > n || ch[x][y] == '#')
    return 0;
  return 1;
}
void solve() {
  int i, j, k;
  deque<pair<int, int>> q;
  q.push_front({sx, sy});
  dp[sx][sy] = 0;
  // vis[sx][sy]=1;
  while (!q.empty()) {
    int curx = q.front().ff;
    int cury = q.front().ss;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int next_x = curx + dx1[i];
      int next_y = cury + dy1[i];
      if (is_valid(next_x, next_y)) {
        // vis[x][y]++;
        if (dp[next_x][next_y] > dp[curx][cury]) {
          vis[next_x][next_y]++;
          dp[next_x][next_y] = dp[curx][cury];
          q.push_front({next_x, next_y});
        }
      }
    }
    for (i = curx - 2; i <= curx + 2; i++) {
      for (j = cury - 2; j <= cury + 2; j++) {
        int next_x = i;
        int next_y = j;
        if (is_valid(next_x, next_y)) {

          if (dp[next_x][next_y] > 1 + dp[curx][cury]) {
            vis[next_x][next_y]++;
            dp[next_x][next_y] = 1 + dp[curx][cury];
            q.push_back({next_x, next_y});
          }
        }
      }
    }
  }
}
int main() {

  fastio;
  ll int i, j, k;
  cin >> n >> m;
  cin >> sx >> sy >> ex >> ey;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> ch[i][j];
      // dp[i][j]=inf;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      dp[i][j] = inf;
  }

  solve();
  if (dp[ex][ey] == inf) {
    cout << -1;
  } else
    cout << dp[ex][ey];
}
// Lordvishnu@almighty

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
#define inf 1e6 + 18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define mod 1000000007
#define f1(i, n) for (i = 1; i <= n; i++)
#define f0(i, n) for (i = 0; i < n; i++)
#define w(x) while (x--)
const int maxn = 1004;
#define pq priority_queue
#define ff first
#define ss second
#define pb push_back
#define lb lower_bound
// Lordvishnu@almighty
char ch[maxn][maxn];

int dp[maxn][maxn], n, m, sx, sy, ex, ey;
int dx1[] = {-1, 1, 0, 0};
int dy1[] = {0, 0, -1, 1};
int dx[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
            0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
int dy[] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
            2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
bool is_valid(int x, int y) {
  if (x <= 0 || y <= 0 || y > m || x > n || ch[x][y] == '#')
    return 0;
  return 1;
}
void solve() {
  int i, j, k;
  queue<pair<int, int>> q;
  q.push({sx, sy});
  dp[sx][sy] = 0;
  // vis[sx][sy]=1;
  while (!q.empty()) {
    int curx = q.front().ff;
    int cury = q.front().ss;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int next_x = curx + dx1[i];
      int next_y = cury + dy1[i];
      if (is_valid(next_x, next_y)) {
        // vis[next_x][next_y]=1;
        if (dp[next_x][next_y] > dp[curx][cury]) {
          dp[next_x][next_y] = dp[curx][cury];
          q.push({next_x, next_y});
        }
      }
    }
    for (i = curx - 2; i <= curx + 2; i++) {
      for (j = cury - 2; j <= cury + 2; j++) {
        int next_x = i;
        int next_y = j;
        if (is_valid(next_x, next_y)) {
          // vis[next_x][next_y]=1;
          if (dp[next_x][next_y] > 1 + dp[curx][cury]) {
            dp[next_x][next_y] = 1 + dp[curx][cury];
            q.push({next_x, next_y});
          }
        }
      }
    }
  }
}
int main() {

  fastio;
  ll int i, j, k;
  cin >> n >> m;
  cin >> sx >> sy >> ex >> ey;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> ch[i][j];
      // dp[i][j]=inf;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      dp[i][j] = inf;
  }

  solve();
  if (dp[ex][ey] == inf) {
    cout << -1;
  } else
    cout << dp[ex][ey];
}
// Lordvishnu@almighty