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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
typedef long long ll;

const int INF = 10010010;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> s[i][j]; }
  }

  vector<vector<int>> d(h, vector<int>(w, INF));
  int wh[4] = {1, 0, -1, 0}, ww[4] = {0, 1, 0, -1};
  int lh[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  int lw[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  deque<P> que;
  que.push_front(P(ch, cw));
  // que.push_back(P(ch,cw));
  d[ch][cw] = 0;

  while (que.size()) {
    P p = que.front();
    que.pop_front();
    if (p.first == dh && p.second == dw)
      break;

    for (int i = 0; i < 4; i++) {
      int nh = p.first + wh[i], nw = p.second + ww[i];

      if (0 <= nh && nh < h && 0 <= nw && nw < w && s[nh][nw] != '#' &&
          d[nh][nw] > d[p.first][p.second]) {
        que.push_front(P(nh, nw));
        d[nh][nw] = d[p.first][p.second];
      }
    }

    for (int i = 0; i < 20; i++) {
      int nh = p.first + lh[i], nw = p.second + lw[i];

      if (0 <= nh && nh < h && 0 <= nw && nw < w && s[nh][nw] != '#' &&
          d[nh][nw] > d[p.first][p.second] + 1) {
        que.push_back(P(nh, nw));
        d[nh][nw] = d[p.first][p.second] + 1;
      }
    }
  }

  if (d[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << d[dh][dw] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> P;
typedef long long ll;

const int INF = 100100100;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) { cin >> s[i][j]; }
  }

  vector<vector<int>> d(h, vector<int>(w, INF));
  int wh[4] = {1, 0, -1, 0}, ww[4] = {0, 1, 0, -1};
  int lh[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  int lw[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  queue<P> que;
  que.push(P(ch, cw));
  d[ch][cw] = 0;

  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == dh && p.second == dw)
      continue;

    for (int i = 0; i < 4; i++) {
      int nh = p.first + wh[i], nw = p.second + ww[i];

      if (0 <= nh && nh < h && 0 <= nw && nw < w && s[nh][nw] != '#' &&
          d[nh][nw] > d[p.first][p.second]) {
        que.push(P(nh, nw));
        d[nh][nw] = d[p.first][p.second];
      }
    }

    for (int i = 0; i < 20; i++) {
      int nh = p.first + lh[i], nw = p.second + lw[i];

      if (0 <= nh && nh < h && 0 <= nw && nw < w && s[nh][nw] != '#' &&
          d[nh][nw] > d[p.first][p.second] + 1) {
        que.push(P(nh, nw));
        d[nh][nw] = d[p.first][p.second] + 1;
      }
    }
  }

  if (d[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << d[dh][dw] << endl;

  return 0;
}