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

#include <bits/stdc++.h>
using namespace std;
int m[1001][1001];
int h, w;
bool checkValid(int x, int y) { return (x >= 0 && y >= 0 && x < h && y < w); }
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '#')
        m[i][j] = 1;
    }
  }
  ch -= 1;
  cw -= 1;
  dh -= 1;
  dw -= 1;

  set<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  vector<int> warp = {-2, -1, 0, 1, 2};
  deque<pair<int, int>> q;
  m[ch][cw] = 2;
  q.push_front(make_pair(ch, cw));
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int cost = m[i][j];
    q.pop_front();
    for (auto p : dir) {
      int x = p.first;
      int y = p.second;
      if (checkValid(i + x, j + y)) {
        if (m[i + x][j + y] == 0 || m[i + x][j + y] > cost) {
          q.push_front({i + x, j + y});
          m[i + x][j + y] = cost;
        }
      }
    }
    for (auto x : warp) {
      for (auto y : warp) {
        if ((x != 0 || y != 0) && checkValid(i + x, j + y) &&
            dir.count(make_pair(x, y)) == 0) {
          if (m[i + x][j + y] == 0 || m[i + x][j + y] > cost + 1) {
            q.push_back(make_pair(i + x, j + y));
            m[i + x][j + y] = cost + 1;
          }
        }
      }
    }
  }
  //    for(int i=0;i<h;i++){
  //        for(int j=0;j<w;j++){
  //            cout <<  m[i][j] << " ";
  //        }
  //        cout << endl;
  //    }
  if (m[dh][dw] == 0)
    cout << -1;
  else
    cout << m[dh][dw] - 2;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dh, dw, h, w;
int m[1001][1001];
bool checkValid(int x, int y) { return (x >= 0 && y >= 0 && x < h && y < w); }
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char tmp;
      cin >> tmp;
      if (tmp == '#')
        m[i][j] = 1;
    }
  }
  ch -= 1;
  cw -= 1;
  dh -= 1;
  dw -= 1;

  set<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  vector<int> warp = {-2, -1, 0, 1, 2};
  deque<pair<int, int>> q;
  m[ch][cw] = 2;
  q.push_front(make_pair(ch, cw));
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int cost = m[i][j];
    q.pop_front();
    for (auto p : dir) {
      int x = p.first;
      int y = p.second;
      if (checkValid(i + x, j + y)) {
        if (m[i + x][j + y] == 0 || m[i + x][j + y] > cost) {
          q.push_front({i + x, j + y});
          m[i + x][j + y] = cost;
        }
      }
    }
    for (auto x : warp) {
      for (auto y : warp) {
        if ((x != 0 || y != 0) && checkValid(i + x, j + y) &&
            dir.count(make_pair(x, y)) == 0) {
          if (m[i + x][j + y] == 0 || m[i + x][j + y] > cost + 1) {
            q.push_front(make_pair(i + x, j + y));
            m[i + x][j + y] = cost + 1;
          }
        }
      }
    }
  }
  //    for(int i=0;i<h;i++){
  //        for(int j=0;j<w;j++){
  //            cout <<  m[i][j] << " ";
  //        }
  //        cout << endl;
  //    }
  if (m[dh][dw] == 0)
    cout << -1;
  else
    cout << m[dh][dw] - 2;
  return 0;
}