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

#include <bits/stdc++.h>
using namespace std;
bool isvalid(int i, int j, int n, int m) {
  return i >= 0 && j >= 0 && i < n && j < m;
}
int main() {
  int n, m;
  cin >> n >> m;
  int x1, y1;
  cin >> x1 >> y1;
  int x2, y2;
  cin >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<char>> adj(n, vector<char>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      adj[i][j] = c;
    }
  }
  vector<vector<int>> dp(n, vector<int>(m, 1e9));
  deque<pair<int, pair<int, int>>> q;
  q.push_front({0, {x1, y1}});
  dp[x1][y1] = 0;
  while (!q.empty()) {
    int dist = q.front().first;
    int x = q.front().second.first;
    int y = q.front().second.second;
    q.pop_front();
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int newx = x + i;
        int newy = y + j;
        if (isvalid(newx, newy, n, m) && adj[newx][newy] == '.') {
          if (abs(i) + abs(j) == 1) {
            if (dp[newx][newy] > dp[x][y]) {
              dp[newx][newy] = dp[x][y];
              q.push_front({dist, {newx, newy}});
            }

          } else {
            if (dp[newx][newy] > dp[x][y] + 1) {
              dp[newx][newy] = dp[x][y] + 1;
              q.push_back({dist + 1, {newx, newy}});
            }
          }
        }
      }
    }
  }
  if (dp[x2][y2] == 1e9)
    cout << -1 << endl;
  else {
    cout << dp[x2][y2] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll mod = 1e9 + 7;
int xdir[4] = {0, 1, -1, 0};
int ydir[4] = {1, 0, 0, -1};
int xdir1[20] = {2, 2,  2,  2,  2,  1,  1,  1,  1,  0,
                 0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
int ydir1[20] = {-2, -1, 0, 1, 2,  -2, -1, 1, 2,  2,
                 -2, -2, 2, 1, -1, 0,  1,  2, -1, -2};

bool isvalid(int i, int j, int n, int m) {
  return i >= 0 && j >= 0 && i < n && j < m;
}

// void dfs(int x,int y,int n,int m)
// {
//     for(int i=0;i<4;i++)
//     {
//         int x1=x+xdir[i];
//         int y1=y+ydir[i];
//         if(isvalid(x1,y1,n,m))
//         {
//             dp[x1][y1]=0;
//             dfs(x1,y1,n,m);
//         }
//     }
//     for(int i=0;i<20;i++)
//     {
//         int x1=x+xdir1[i];
//         int y1=y+ydir1[i];
//         if(isvalid(x1,y1,n,m)&&dp[x1][y1]>dp[x][y]+1)
//         {
//             dp[x1][y1]=dp[x][y]+1;
//             dfs(x1,y1,n,m);
//         }
//     }
// }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  char adj[n][m];

  // dp.resize(n,vector<int>(m,1e9));
  int dp[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      dp[i][j] = 1e9;
  }
  int x1, y1;
  int x2, y2;
  cin >> x1 >> y1;
  cin >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cin >> adj[i][j];
  }
  dp[x1][y1] = 0;
  queue<pair<int, int>> q;
  q.push({x1, y1});
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    int ct = 0;
    for (int i = 0; i < 4; i++) {
      x1 = x + xdir[i];
      y1 = y + ydir[i];
      if (isvalid(x1, y1, n, m) && adj[x1][y1] == '.') {
        ct++;
        if (dp[x1][y1] > dp[x][y]) {
          dp[x1][y1] = dp[x][y];
          q.push({x1, y1});
        }
      }
    }
    if (ct == 4)
      continue;
    for (int i = 0; i < 20; i++) {
      x1 = x + xdir1[i];
      y1 = y + ydir1[i];
      if (isvalid(x1, y1, n, m) && dp[x1][y1] > dp[x][y] + 1 &&
          adj[x1][y1] == '.') {
        dp[x1][y1] = dp[x][y] + 1;
        q.push({x1, y1});
      }
    }
  }
  if (dp[x2][y2] == 1e9)
    cout << -1 << endl;
  else {
    cout << dp[x2][y2];
  }
}