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

// Hail god Yato

#include <bits/stdc++.h>
using namespace std;

#define hs                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
int xchanges[] = {-1, +1, 0, 0};
int ychanges[] = {0, 0, -1, +1};

void solve() {
  int n, m;
  cin >> n >> m;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  --sx, --ex, --sy, --ey;
  vector<string> vec(n);
  for (int i = 0; i < n; i++)
    cin >> vec[i];

  vector<vector<int>> val(n, vector<int>(m, INT_MAX));
  deque<pair<int, pair<int, int>>> q;
  q.push_front({0, {sx, sy}});
  while (!q.empty()) {
    pair<int, pair<int, int>> temp = q.front();
    q.pop_front();
    int x = temp.second.first, y = temp.second.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + xchanges[i], ny = y + ychanges[i];
      if (nx >= 0 && ny >= 0 && nx < n && ny < m && vec[nx][ny] != '#') {
        if (val[nx][ny] > temp.first) {
          val[nx][ny] = temp.first;
          q.push_front({temp.first, {nx, ny}});
        }
      }
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int nx = i + x, ny = j + y;
        if (nx >= 0 && ny >= 0 && nx < n && ny < m && vec[nx][ny] != '#') {
          if (val[nx][ny] > temp.first + 1) {
            val[nx][ny] = temp.first + 1;
            q.push_back({temp.first + 1, {nx, ny}});
          }
        }
      }
  }
  // for(int i = 0; i < n; i++){
  //     for(int j = 0; j < m; j++)
  //         cout<<val[i][j]<<" ";
  //     cout<<"\\n";
  // }
  if (val[ex][ey] == INT_MAX)
    val[ex][ey] = -1;
  cout << val[ex][ey];
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

// Hail god Yato

#include <bits/stdc++.h>
using namespace std;

#define hs                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
const ll mod = 1000000007;
const ll INF = 1e18;
const ll MAX = 1005;
//
//
int n, m, x1, x2, yy, y2;
char arr[MAX][MAX];
void dfs(int x, int y, vector<vector<ll>> &val, int present) {
  if (x <= 0 || y <= 0 || x > n || y > m || arr[x][y] == '#')
    return;
  if (val[x][y] == -1)
    val[x][y] = present;
  else {
    if (val[x][y] <= present)
      return;
    val[x][y] = present;
  }
  // if(x == x1 && y == yy)
  // return ;
  dfs(x, y + 1, val, present);
  dfs(x, y - 1, val, present);
  dfs(x + 1, y, val, present);
  dfs(x - 1, y, val, present);
  for (int i = -2; i <= 2; i++)
    for (int j = -2; j <= 2; j++) {
      dfs(x + i, y + j, val, present + 1);
    }
}
void solve() {
  cin >> n >> m >> x1 >> yy >> x2 >> y2;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> arr[i][j];
  vector<vector<ll>> val(n + 1, vector<ll>(m + 1, -1));
  dfs(x2, y2, val, 0);
  cout << val[x1][yy];
}
int main() {
  hs;
  ll t;
  t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}