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

#include <bits/stdc++.h>

using namespace std;
int dr[] = {1, 0, -1, 0}; // S,SE,E,NE,N,NW,W,SW
int dc[] = {0, 1, 0, -1}; // neighbors

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  int dist[h][w];
  char s[h][w];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> s[i][j];
      dist[i][j] = 1e9;
    }
  }
  int cost = 0;
  dist[ch][cw] = 0;
  deque<pair<int, int>> q;
  q.push_back(pair<int, int>(ch, cw));
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop_front(); // queue: layer by layer!
    int x = u.first;
    int y = u.second;
    cost = dist[x][y];
    //    	cout<<x<<" "<<y<<" "<<cost<<endl;
    for (int i = 0; i < 4; ++i) {
      int r = x + dr[i];
      int c = y + dc[i];
      if (r < 0 || r >= h || c < 0 || c >= w)
        continue; // outside
      if (s[r][c] == '#')
        continue;
      if (dist[r][c] <= cost)
        continue;
      dist[r][c] = cost;
      //    		cout<<r<<" "<<c<<" "<<cost<<endl;
      q.push_front(pair<int, int>(r, c));
    }
    cost++;
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int r = x + i;
        int c = y + j;
        if (r < 0 || r >= h || c < 0 || c >= w)
          continue; // outside
        if (s[r][c] == '#')
          continue;
        if (dist[r][c] <= cost)
          continue;
        dist[r][c] = cost;
        q.push_back(pair<int, int>(r, c));
      }
    }
  }
  dist[dh][dw] == 1e9 ? cout << "-1" : cout << dist[dh][dw];
}


Error: TLE

Fixed submission:

/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <bits/stdc++.h>
using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

inline void solve() {
  int r, c, ch, cw, dh, dw;
  cin >> r >> c >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  int dis[r][c];
  char s[r][c];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      cin >> s[i][j];
      dis[i][j] = 1e9;
    }
  }
  int cost = 0;
  dis[ch][cw] = 0;
  deque<pair<int, int>> q;
  q.emplace_back(ch, cw);
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop_front();
    cost = dis[x][y];
    // move...
    for (int t = 0; t < 4; t++) {
      int i = x + dx[t];
      int j = y + dy[t];
      if (i >= 0 and i < r and j >= 0 and j < c and s[i][j] == '.' and
          (dis[i][j] > cost)) {
        dis[i][j] = cost;
        q.emplace_back(i, j);
      }
    }
    // jump...
    for (int i = max(0, x - 2); i <= (min(r, x + 2)); i++) {
      for (int j = max(0, y - 2); j <= min(c, y + 2); j++) {
        if (i < r and j < c and s[i][j] == '.' and (dis[i][j] > cost + 1)) {
          dis[i][j] = cost + 1;
          q.emplace_back(i, j);
        }
      }
    }
  }
  if (dis[dh][dw] != 1e9)
    cout << (dis[dh][dw]) << endl;
  else
    cout << (-1) << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}