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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  int h, w;

  cin >> h >> w;

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<char>> maze(h, vector<char>(w));
  vector<vector<ll>> warp(h, vector<ll>(w, INF));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> maze[i][j];
    }
  }

  queue<P> q, visited;

  q.push({ch, cw});
  warp[ch][cw] = 0;

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

  while (q.size() || visited.size()) {
    if (q.size()) {
      P p = q.front();
      q.pop();

      int x = p.first, y = p.second;
      visited.push({x, y});

      for (int i = 0; i < 4; i++) {
        int next_x = x + dx[i], next_y = y + dy[i];

        if (next_x < 0 || next_y < 0 || next_x >= h || next_y >= w) {
          continue;
        }

        if (maze[next_x][next_y] == '#') {
          continue;
        }

        if (warp[x][y] < warp[next_x][next_y]) {
          warp[next_x][next_y] = warp[x][y];

          q.push({next_x, next_y});
        }
      }
    } else {
      P p = visited.front();
      visited.pop();

      int x = p.first, y = p.second;

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          int next_x = x + i, next_y = y + j;

          if (next_x < 0 || next_y < 0 || next_x >= h || next_y >= w) {
            continue;
          }

          if (maze[next_x][next_y] == '#') {
            continue;
          }

          if (warp[x][y] + 1 < warp[next_x][next_y]) {
            warp[next_x][next_y] = warp[x][y] + 1;

            q.push({next_x, next_y});
          }
        }
      }
    }
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void) {
  int h, w;

  cin >> h >> w;

  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  vector<vector<char>> maze(h, vector<char>(w));
  vector<vector<bool>> searched(h, vector<bool>(w, false));
  vector<vector<ll>> warp(h, vector<ll>(w, INF));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> maze[i][j];
    }
  }

  queue<P> q;

  q.push({ch, cw});
  searched[ch][cw] = true;
  warp[ch][cw] = 0;

  while (q.size()) {
    P p = q.front();
    q.pop();

    int x = p.first, y = p.second;
    searched[x][y] = true;

    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (x + i < 0 || y + j < 0 || x + i >= h || y + j >= w) {
          continue;
        }

        if (maze[x + i][y + j] == '#' || searched[x + i][y + j]) {
          continue;
        }

        if (abs(i) + abs(j) >= 2) {
          warp[x + i][y + j] = min(warp[x][y] + 1, warp[x + i][y + j]);
        } else {
          warp[x + i][y + j] = min(warp[x][y], warp[x + i][y + j]);
        }

        q.push({x + i, y + j});
      }
    }
  }

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