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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define ffor(n) for (int i = 0; i < n; i++)
#define fffor(n) for (int j = 0; j < n; j++)
int INF = 1e9 + 7;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;
  vector<vector<int>> visited(n, vector<int>(m, INF));
  vector<string> grid(n);

  int x, y, x2, y2, dist, a, b;
  cin >> x >> y >> x2 >> y2;
  x--;
  y--;
  x2--;
  y2--;
  ffor(n) cin >> grid[i];
  visited[x][y] = 0;
  priority_queue<pair<int, pair<int, int>>> q, q2; // contains {dist, {x, y}}
  queue<pair<int, int>> bfs;
  map<pair<int, int>, bool> pushed;
  q.push({0, {x, y}});
  while (q.size() > 0) {
    while (q.size() > 0) {
      x = q.top().second.first, y = q.top().second.second;
      dist = q.top().first;
      q.pop();
      if (grid[x][y] == 'q')
        continue;
      bfs.push({x, y});
      while (bfs.size() > 0) {
        a = bfs.front().first, b = bfs.front().second;
        bfs.pop();
        if (grid[a][b] == 'q')
          continue;

        visited[a][b] = dist;
        grid[a][b] = 'q';
        q2.push({dist, {a, b}});
        pushed[{a, b}] = true;

        for (int i = a + 1; i < n; i++) {
          if (grid[i][b] != '.')
            break; // not supposed to reach this place
          if (pushed[{i, b}])
            continue;
          bfs.push({i, b});
          pushed[{i, b}] = true;
        }
        for (int i = a - 1; i >= 0; i--) {
          if (grid[i][b] != '.')
            break;
          if (pushed[{i, b}])
            continue;
          bfs.push({i, b});
          pushed[{i, b}] = true;
        }
        for (int i = b + 1; b < m; i++) {
          if (grid[a][i] != '.')
            break;
          if (pushed[{a, i}])
            continue;
          bfs.push({a, i});
          pushed[{a, i}] = true;
        }
        for (int i = b - 1; i >= 0; i--) {
          if (grid[a][i] != '.')
            break;
          if (pushed[{a, i}])
            continue;
          bfs.push({a, i});
          pushed[{a, i}] = true;
        }
      }
    }
    while (q2.size() > 0) {
      x = q2.top().second.first, y = q2.top().second.second,
      dist = q2.top().first;
      q2.pop();
      for (int i = x - 2; i <= x + 2; i++) {
        if (i < 0 || i >= n)
          continue;
        for (int j = y - 2; j <= y + 2; j++) {
          if (j < 0 || j >= m)
            continue;
          if (visited[i][j] > dist + 1 && grid[i][j] == '.') {
            visited[i][j] = dist + 1;
            q.push({dist + 1, {i, j}});
          }
        }
      }
    }
  }
  cout << (visited[x2][y2] == INF ? -1 : visited[x2][y2]);
}

/*
C:\\Users\\User\\Desktop\\comp_programming\\final_straw_after_this_im_switching_to_vs_code\\final_straw_after_this_im_switching_to_vs_code.cpp
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define ffor(n) for (int i = 0; i < n; i++)
#define fffor(n) for (int j = 0; j < n; j++)
int INF = 1e9 + 7;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;
  vector<vector<int>> visited(n, vector<int>(m, INF));
  vector<string> grid(n);

  int x, y, x2, y2, dist;
  cin >> x >> y >> x2 >> y2;
  x--;
  y--;
  x2--;
  y2--;
  ffor(n) cin >> grid[i];
  visited[x][y] = 0;
  priority_queue<pair<int, pair<int, int>>> q; // contains {dist, {x, y}}
  q.push({0, {x, y}});
  while (q.size() > 0) {
    x = q.top().second.first, y = q.top().second.second, dist = q.top().first;
    q.pop();
    if (x - 1 >= 0 && visited[x - 1][y] > dist && grid[x - 1][y] == '.') {
      q.push({dist, {x - 1, y}});
      visited[x - 1][y] = dist;
    }
    if (x + 1 < n && visited[x + 1][y] > dist && grid[x + 1][y] == '.') {
      q.push({dist, {x + 1, y}});
      visited[x + 1][y] = dist;
    }
    if (y - 1 >= 0 && visited[x][y - 1] > dist && grid[x][y - 1] == '.') {
      q.push({dist, {x, y - 1}});
      visited[x][y - 1] = dist;
    }
    if (y + 1 < m && visited[x][y + 1] > dist && grid[x][y + 1] == '.') {
      q.push({dist, {x, y + 1}});
      visited[x][y + 1] = dist;
    }

    for (int i = x - 2; i <= x + 2; i++) {
      if (i < 0 || i >= n)
        continue;
      for (int j = y - 2; j <= y + 2; j++) {
        if (j < 0 || j >= m)
          continue;
        if (visited[i][j] > dist + 1 && grid[i][j] == '.') {
          visited[i][j] = dist + 1;
          q.push({dist + 1, {i, j}});
        }
      }
    }
  }
  cout << (visited[x2][y2] == INF ? -1 : visited[x2][y2]);
}

/*
C:\\Users\\User\\Desktop\\comp_programming\\final_straw_after_this_im_switching_to_vs_code\\final_straw_after_this_im_switching_to_vs_code.cpp
*/