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:

#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_back(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];
}