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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define INF 9223372036854775807ll
#define endl '\\n'
#define pii pair<int, int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define lower(u)                                                               \\
  transform(u.begin(), u.end(), u.begin(),                                     \\
            ::tolower); // convert string u to lowercase;
#define upper(u) transform(u.begin(), u.end(), u.begin(), ::toupper);
using namespace std;

using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define N 1000005

int n, m, x, y, st, en;
int fun(int x, int y) { return x * n + y; }
int col[1000][1000];
vector<int> adj[N];
bool vis[1000][1000];

char arr[1000][1000];
bool check(int x, int y) {
  return (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '.');
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int color = 1;
void dfs(int x, int y) {
  vis[x][y] = 1;
  col[x][y] = color;
  for (int i = 0; i < 4; i++) {
    if (check(x + dx[i], y + dy[i]) && !vis[x + dx[i]][y + dy[i]]) {
      dfs(x + dx[i], y + dy[i]);
    }
  }
}
void solve() {
  cin >> n >> m;
  int c1, d1, c2, d2;
  cin >> c1 >> d1;
  cin >> c2 >> d2;
  rep(i, 0, n) {
    rep(j, 0, m) { cin >> arr[i][j]; }
  }
  rep(i, 0, n) {
    rep(j, 0, m) {

      if (arr[i][j] == '.' && !vis[i][j]) {
        dfs(i, j);
        color++;
      }
    }
  }
  st = col[c1 - 1][d1 - 1];
  en = col[c2 - 1][d2 - 1];
  // cout<<st<<" "<<en<<endl;
  if (st == en) {
    cout << 0 << endl;
    return;
  }
  color--;
  vector<pii> vec;
  // rep(i,0,n)
  // {
  //     rep(j,0,m)
  //     {
  //         cout<<col[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (abs(i) + abs(j) > 1) {
        vec.pb({i, j});
      }
    }
  }
  map<pii, ll> lol;
  rep(i, 0, n) {
    rep(j, 0, m) {
      if (arr[i][j] == '#')
        continue;
      int cur = col[i][j];
      for (auto u : vec) {
        if (check(i + u.F, j + u.S) && cur != col[i + u.F][j + u.S]) {
          int to = col[i + u.F][j + u.S];
          if (lol.find({cur, to}) == lol.end()) {
            adj[cur].pb(to);
            adj[to].pb(cur);
            lol[{cur, to}] = 1;
            lol[{to, cur}] = 1;
          }
        }
      }
    }
  }
  // rep(i,1,color+1)
  // {
  //     cout<<i<<endl;
  //     for(auto u:adj[i])
  //     {
  //         cout<<u<<" ";
  //     }
  //     cout<<endl;
  // }
  vector<int> dist(color + 1, hell);

  dist[st] = 0;
  bool visi[color + 1];
  memset(visi, 0, sizeof(visi));
  queue<ll> pq;
  pq.push(st);
  visi[st] = 1;
  while (!pq.empty()) {
    ll t = pq.front();
    pq.pop();

    for (auto u : adj[t]) {
      if (!visi[u]) {
        visi[u] = 1;
        dist[u] = dist[t] + 1;
        pq.push(u);
      }
    }
  }
  if (dist[en] != hell) {
    cout << dist[en] << endl;
  } else {
    cout << -1 << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int TESTS = 1;
  //    cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define INF 9223372036854775807ll
#define endl '\\n'
#define pii pair<int, int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define lower(u)                                                               \\
  transform(u.begin(), u.end(), u.begin(),                                     \\
            ::tolower); // convert string u to lowercase;
#define upper(u) transform(u.begin(), u.end(), u.begin(), ::toupper);
using namespace std;

using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define N 1000005

int n, m, x, y, st, en;
int fun(int x, int y) { return x * n + y; }
int col[1000][1000];
vector<int> adj[N];
bool vis[1000][1000];

char arr[1000][1000];
bool check(int x, int y) {
  return (x >= 0 && x < n && y >= 0 && y < m && arr[x][y] == '.');
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int color = 1;
void dfs(int x, int y) {
  vis[x][y] = 1;
  col[x][y] = color;
  for (int i = 0; i < 4; i++) {
    if (check(x + dx[i], y + dy[i]) && !vis[x + dx[i]][y + dy[i]]) {
      dfs(x + dx[i], y + dy[i]);
    }
  }
}
void solve() {
  cin >> n >> m;
  int c1, d1, c2, d2;
  cin >> c1 >> d1;
  cin >> c2 >> d2;
  rep(i, 0, n) {
    rep(j, 0, m) { cin >> arr[i][j]; }
  }
  rep(i, 0, n) {
    rep(j, 0, m) {

      if (arr[i][j] == '.' && !vis[i][j]) {
        dfs(i, j);
        color++;
      }
    }
  }
  st = col[c1 - 1][d1 - 1];
  en = col[c2 - 1][d2 - 1];
  // cout<<st<<" "<<en<<endl;
  if (st == en) {
    cout << 0 << endl;
    return;
  }
  color--;
  vector<pii> vec;
  // rep(i,0,n)
  // {
  //     rep(j,0,m)
  //     {
  //         cout<<col[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (abs(i) + abs(j) > 1) {
        vec.pb({i, j});
      }
    }
  }
  map<pii, ll> lol;
  rep(i, 0, n) {
    rep(j, 0, m) {
      if (arr[i][j] == '#')
        continue;
      int cur = col[i][j];
      for (auto u : vec) {
        if (check(i + u.F, j + u.S) && cur != col[i + u.F][j + u.S]) {
          int to = col[i + u.F][j + u.S];
          if (lol.find({cur, to}) == lol.end()) {
            adj[cur].pb(to);
            adj[to].pb(cur);
            lol[{cur, to}] = 1;
            lol[{to, cur}] = 1;
          }
        }
      }
    }
  }
  // rep(i,1,color+1)
  // {
  //     cout<<i<<endl;
  //     for(auto u:adj[i])
  //     {
  //         cout<<u<<" ";
  //     }
  //     cout<<endl;
  // }
  vector<int> dist(color + 1, hell);

  dist[st] = 0;
  priority_queue<pii, std::vector<pii>, std::less<pii>> pq;
  pq.push({0, st});
  while (!pq.empty()) {
    pii t = pq.top();
    pq.pop();
    // cout<<t.F<<" "<<t.S<<endl;
    for (auto u : adj[t.S]) {
      if (dist[u] > dist[t.S] + 1) {
        dist[u] = dist[t.S] + 1;
        pq.push({dist[u], u});
      }
    }
  }
  if (dist[en] != hell) {
    cout << dist[en] << endl;
  } else {
    cout << -1 << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int TESTS = 1;
  //    cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  return 0;
}