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

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
using ll = long long;
using ld = long double;

const int tinf = 1e9;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  int c = (ch - 1) * w + (cw - 1);
  int d = (dh - 1) * w + (dw - 1);

  vector<bool> s(h * w);
  for (int i = 0; i < h; i++) {
    string tmp;
    cin >> tmp;
    for (int j = 0; j < w; j++) {
      if (tmp[j] == '#')
        s[i * w + j] = false;
      else
        s[i * w + j] = true;
    }
  }

  vector<int> warp(h * w, tinf);
  deque<int> q;
  q.push_front(c);
  warp[c] = 0;

  int wr[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  int wc[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  int dr[4] = {1, -1, 0, 0};
  int dc[4] = {0, 0, 1, -1};

  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    int r = v / w;
    int c = v % w;

    for (int i = 0; i < 4; i++) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      int nv = nr * w + nc;
      if (nr < 0 || nr >= h || nc < 0 || nc >= w || s[nv] == false)
        continue;
      if (warp[nv] > warp[v]) {
        warp[nv] = warp[v];
        q.push_front(nv);
      }
    }

    for (int i = 0; i < 20; i++) {
      int nr = r + wr[i];
      int nc = c + wc[i];
      int nv = nr * w + nc;
      if (nr < 0 || nr >= h || nc < 0 || nc >= w || s[nv] == false)
        continue;
      if (warp[nv] > warp[v] + 1) {
        warp[nv] = warp[v] + 1;
        q.push_back(nv);
      }
    }
    if (warp[d] == 0)
      break;
  }

  if (warp[d] == tinf)
    cout << "-1"
         << "\\n";
  else
    cout << warp[d] << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const int tinf = 1e9;

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  int c = (ch - 1) * w + (cw - 1);
  int d = (dh - 1) * w + (dw - 1);

  vector<bool> s(h * w);
  for (int i = 0; i < h; i++) {
    string tmp;
    cin >> tmp;
    for (int j = 0; j < w; j++) {
      if (tmp[j] == '#')
        s[i * w + j] = false;
      else
        s[i * w + j] = true;
    }
  }

  vector<int> warp(h * w, tinf);
  queue<int> q;
  q.push(c);
  warp[c] = 0;

  int wr[20] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, 0,
                0,  1,  1,  1,  1,  2,  2,  2,  2,  2};
  int wc[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};

  int dr[4] = {1, -1, 0, 0};
  int dc[4] = {0, 0, 1, -1};

  int cnt = 0;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    int r = v / w;
    int c = v % w;

    for (int i = 0; i < 4; i++) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      int nv = nr * w + nc;
      if (nr < 0 || nr >= h || nc < 0 || nc >= w || s[nv] == false)
        continue;
      if (warp[nv] > warp[v]) {
        warp[nv] = warp[v];
        q.push(nv);
      }
    }

    for (int i = 0; i < 20; i++) {
      int nr = r + wr[i];
      int nc = c + wc[i];
      int nv = nr * w + nc;
      if (nr < 0 || nr >= h || nc < 0 || nc >= w || s[nv] == false)
        continue;
      if (warp[nv] > warp[v] + 1) {
        warp[nv] = warp[v] + 1;
        q.push(nv);
      }
    }
    if (warp[d] == 0)
      break;
  }

  if (warp[d] == tinf)
    cout << "-1"
         << "\\n";
  else
    cout << warp[d] << "\\n";

  return 0;
}