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

/* author   : mpily :-()
title : Competing.cpp
time :Sun Jun 14 13:11:23 2020

*/
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// 4611686018427387903
using namespace std;
// magic lines for ordered_set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,           \\
       tree_order_statistics_node_update>
/*
        fuctions :

    order_of_key (k) : Number of items strictly smaller than k .
    find_by_order(k) : K-th element in a set (counting from zero).


*/
const int64_t mod = 998244353.;
bool visitedall(const vector<vector<int>> &visited,
                const vector<vector<int>> &weights) {
  int n = visited.size();
  int m = visited[0].size();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      // cerr << visited[i][j] <<" ";
      if (!visited[i][j] && weights[i][j] != 1000000) {
        // cerr << "haven't visited "<<i << " "<< j <<"\\n";
        return false;
      }
    }
    // cerr << "\\n";
  }
  return true;
}
vector<int> di = {-1, 1, 0, 0};
vector<int> dj = {0, 0, -1, 1};
void solve(int cnum) {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<vector<char>> grid(h, vector<char>(w));
  vector<vector<int>> djikstra(h, vector<int>(w, 1000000)),
      visited(h, vector<int>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> grid[i][j];
    }
  }
  ch--;
  cw--;
  dh--;
  dw--;
  djikstra[ch][cw] = 0;
  set<array<int, 3>> bfs;
  bfs.insert({0, ch, cw});
  while (bfs.size()) {
    auto a = (*bfs.begin());
    while (visited[a[1]][a[2]] && bfs.size()) {
      bfs.erase(bfs.begin());
      if (bfs.size() == 0)
        break;
      a = (*bfs.begin());
    }
    if (bfs.size() == 0) {
      break;
    }
    bfs.erase(bfs.begin());
    int nxtr = a[1], nxtcol = a[2];
    // cerr << nxtr << " "<<nxtcol <<"\\n";
    visited[nxtr][nxtcol] = 1;
    for (int i = nxtr - 2; i < nxtr + 3; i++) {
      for (int j = nxtcol - 2; j < nxtcol + 3; ++j) {
        if (i < 0 || j < 0 || i >= h || j >= w) {
          continue;
        }
        if (visited[i][j] || grid[i][j] == '#') {
          continue;
        }
        djikstra[i][j] = min(djikstra[i][j], djikstra[nxtr][nxtcol] + 1);
        bfs.insert({djikstra[i][j], i, j});
      }
    }
    for (int k = 0; k < 4; ++k) {
      int i = di[k] + nxtr, j = dj[k] + nxtcol;
      // cerr <<"\\t tf "<< i <<" "<< j <<"\\n";
      if (i < 0 || j < 0 || i >= h || j >= w) {
        continue;
      }
      if (visited[i][j] || grid[i][j] == '#') {
        continue;
      }
      // cerr << "\\t changing "<< i << " "<<j <<endl;
      djikstra[i][j] = djikstra[nxtr][nxtcol];
      bfs.insert({djikstra[i][j], i, j});
    }
    // cerr << "here \\n" ;
  }
  //    for(int i = 0; i < h; ++i){
  //        for(int j = 0; j < w; ++j){
  //            cout << djikstra[i][j] <<" ";
  //        }
  //        cout << endl;
  //    }
  if (djikstra[dh][dw] == 1000000)
    cout << -1 << "\\n";
  else
    cout << djikstra[dh][dw] << "\\n";
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout.tie(nullptr);
  // initprime();
  cout << setprecision(12);
  cout << fixed;
  int t;
  int count = 1;
  for (t = 1; t--; solve(count), count++)
    ;
  return 0;
}


Error: TLE

Fixed submission:

/* author   : mpily :-()
title : Competing.cpp
time :Sun Jun 14 13:11:23 2020

*/
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// 4611686018427387903
using namespace std;
// magic lines for ordered_set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,           \\
       tree_order_statistics_node_update>
/*
        fuctions :

    order_of_key (k) : Number of items strictly smaller than k .
    find_by_order(k) : K-th element in a set (counting from zero).


*/
const int64_t mod = 998244353.;
bool visitedall(const vector<vector<int>> &visited,
                const vector<vector<int>> &weights) {
  int n = visited.size();
  int m = visited[0].size();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      // cerr << visited[i][j] <<" ";
      if (!visited[i][j] && weights[i][j] != 1000000) {
        // cerr << "haven't visited "<<i << " "<< j <<"\\n";
        return false;
      }
    }
    // cerr << "\\n";
  }
  return true;
}
vector<int> di = {-1, 1, 0, 0};
vector<int> dj = {0, 0, -1, 1};
void solve(int cnum) {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  vector<vector<char>> grid(h, vector<char>(w));
  vector<vector<int>> djikstra(h, vector<int>(w, 1000000)),
      visited(h, vector<int>(w));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> grid[i][j];
    }
  }
  ch--;
  cw--;
  dh--;
  dw--;
  djikstra[ch][cw] = 0;
  while (!visitedall(visited, djikstra)) {
    int nxtr, nxtcol, mnw = 1000000;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (djikstra[i][j] < mnw && !visited[i][j] && grid[i][j] == '.') {
          nxtr = i;
          nxtcol = j;
          mnw = djikstra[i][j];
        }
      }
    }
    // cerr << nxtr << " "<< nxtcol <<"\\n";
    visited[nxtr][nxtcol] = 1;
    for (int i = nxtr - 2; i < nxtr + 3; i++) {
      for (int j = nxtcol - 2; j < nxtcol + 3; ++j) {
        if (i < 0 || j < 0 || i >= h || j >= w) {
          continue;
        }
        if (visited[i][j] || grid[i][j] == '#') {
          continue;
        }
        djikstra[i][j] = min(djikstra[i][j], djikstra[nxtr][nxtcol] + 1);
      }
    }
    for (int k = 0; k < 4; ++k) {
      int i = di[k] + nxtr, j = dj[k] + nxtcol;
      // cerr <<"\\t tf "<< i <<" "<< j <<"\\n";
      if (i < 0 || j < 0 || i >= h || j >= w) {
        continue;
      }
      if (visited[i][j] || grid[i][j] == '#') {
        continue;
      }
      // cerr << "\\t changing "<< i << " "<<j <<endl;
      djikstra[i][j] = djikstra[nxtr][nxtcol];
    }
  }
  //    for(int i = 0; i < h; ++i){
  //        for(int j = 0; j < w; ++j){
  //            cout << djikstra[i][j] <<" ";
  //        }
  //        cout << endl;
  //    }
  if (djikstra[dh][dw] == 1000000)
    cout << -1 << "\\n";
  else
    cout << djikstra[dh][dw] << "\\n";
}
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout.tie(nullptr);
  // initprime();
  cout << setprecision(12);
  cout << fixed;
  int t;
  int count = 1;
  for (t = 1; t--; solve(count), count++)
    ;
  return 0;
}