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

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

int main() {
  int H, W;
  cin >> H >> W;
  pair<int, int> st, go;
  cin >> st.first >> st.second >> go.first >> go.second;

  st.first--, st.second--, go.first--, go.second--;
  vector<string> grid(H);
  for (int i = 0; i < H; i++) {
    cin >> grid[i];
  }

  vector<vector<pair<int, bool>>> maze(
      H, vector<pair<int, bool>>(W, make_pair(1e7, false)));
  deque<pair<int, pair<int, int>>> deq;
  deq.push_back(make_pair(0, st));
  maze[st.first][st.second] = make_pair(0, true);
  int b = 0;
  while (!deq.empty()) {
    int cst = deq.front().first;
    int x = deq.front().second.first, y = deq.front().second.second;
    maze[x][y].second = true;
    deq.pop_front();
    /*
    cout << "x=" << x << " y=" << y;
    cout << " cst=" << cst << endl;
    */
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int xto = x + i, yto = y + j;
        if (xto < 0 || H <= xto || yto < 0 || W <= yto)
          continue;
        if (grid[xto][yto] == '#')
          continue;
        if (maze[xto][yto].second)
          continue;

        if (abs(xto - x) + abs(yto - y) <= 1) {
          deq.push_front(make_pair(cst, make_pair(xto, yto)));
          maze[xto][yto].first = min(maze[xto][yto].first, cst);
          maze[xto][yto].second = true;
        } else {
          deq.push_back(make_pair(cst + 1, make_pair(xto, yto)));
          maze[xto][yto].first = min(maze[xto][yto].first, cst + 1);
        }
      }
    }

    b++;
    // if (maze[go.first][go.second].second)break;
  }

  if (maze[go.first][go.second].second)
    cout << maze[go.first][go.second].first << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

int main() {
  int H, W;
  cin >> H >> W;
  pair<int, int> st, go;
  cin >> st.first >> st.second >> go.first >> go.second;

  st.first--, st.second--, go.first--, go.second--;
  vector<string> grid(H);
  for (int i = 0; i < H; i++) {
    cin >> grid[i];
  }

  vector<vector<pair<int, bool>>> maze(
      H, vector<pair<int, bool>>(W, make_pair(1e7, false)));
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.emplace(0, st);
  maze[st.first][st.second] = make_pair(0, true);
  int b = 0;
  while (!pq.empty()) {
    int cst = pq.top().first;
    int x = pq.top().second.first, y = pq.top().second.second;
    maze[x][y].second = true;
    pq.pop();
    /*
    cout << "x=" << x << " y=" << y;
    cout << " cst=" << cst << endl;
    */
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int xto = x + i, yto = y + j;
        if (xto < 0 || H <= xto || yto < 0 || W <= yto)
          continue;
        if (grid[xto][yto] == '#')
          continue;
        if (maze[xto][yto].second)
          continue;

        if (abs(xto - x) + abs(yto - y) <= 1) {
          pq.emplace(cst, make_pair(xto, yto));
          maze[xto][yto].first = cst;
          maze[xto][yto].second = true;
        } else {
          pq.emplace(cst + 1, make_pair(xto, yto));
          maze[xto][yto].first = cst + 1;
        }
      }
    }

    b++;
    // if (b >= H * W + 100)break;
  }

  if (maze[go.first][go.second].second)
    cout << maze[go.first][go.second].first << endl;
  else
    cout << -1 << endl;
}