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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

vector<vector<ll>> used, color;

vector<vector<char>> s;

const ll INF = 1e18;

ll h, w;

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

ll ddx[5] = {-2, -1, 0, 1, 2};
ll ddy[5] = {-2, -1, 0, 1, 2};

void dfs(ll i, ll j, ll num) {
  used[i][j] = 1;
  color[i][j] = num;
  rep(k, 4) {
    ll nx = i + dx[k];
    ll ny = j + dy[k];
    if (0 <= nx && nx <= h - 1 && 0 <= ny && ny <= w - 1 && s[nx][ny] == '.' &&
        used[nx][ny] == 0) {
      dfs(nx, ny, num);
    }
  }
}

int main() {

  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  used = vector<vector<ll>>(h, vector<ll>(w));
  color = vector<vector<ll>>(h, vector<ll>(w));
  s = vector<vector<char>>(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        color[i][j] = -1;
    }
  }
  ll cnt = 0;
  rep(i, h) {
    rep(j, w) {
      if (used[i][j] == 1)
        continue;
      if (color[i][j] == -1)
        continue;
      dfs(i, j, cnt);
      cnt++;
    }
  }
  /*rep(i,h){
      rep(j,w){
          cout << color[i][j] << " ";
      }
      cout << endl;
  }*/
  map<pll, ll> t;
  // vector<set<ll>> to(cnt);
  vector<vector<ll>> m;
  m.resize(cnt);
  rep(i, h) {
    rep(j, w) {
      rep(a, 5) {
        rep(b, 5) {
          if (ddx[a] == 0 && ddy[b] == 0)
            continue;
          if (color[i][j] == -1)
            continue;
          ll nx = i + ddx[a];
          ll ny = j + ddy[b];
          if (0 <= nx && nx <= h - 1 && 0 <= ny && ny <= w - 1 &&
              color[nx][ny] != -1 && color[nx][ny] != color[i][j] &&
              t.count(make_pair(color[i][j], color[nx][ny])) == false) {
            m[color[i][j]].push_back(color[nx][ny]);
            m[color[nx][ny]].push_back(color[i][j]);
            t[make_pair(color[i][j], color[nx][ny])] = 1;
            t[make_pair(color[nx][ny], color[i][j])] = 1;
          }
        }
      }
    }
  }

  vector<ll> vs(cnt);
  vector<ll> d(cnt, INF);
  queue<ll> que;
  que.push(color[ch][cw]);
  d[color[ch][cw]] = 0;
  while (!que.empty()) {
    ll x = que.front();
    que.pop();
    vs[x] = 1;
    for (auto v : m[x]) {
      if (d[v] != INF)
        continue;
      d[v] = d[x] + 1;
      que.push(v);
    }
  }
  if (d[color[dh][dw]] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << d[color[dh][dw]] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\\n'
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

vector<vector<ll>> used, color;

vector<vector<char>> s;

const ll INF = 1e18;

ll h, w;

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

ll ddx[3] = {0, 1, 2};
ll ddy[3] = {0, 1, 2};

void dfs(ll i, ll j, ll num) {
  used[i][j] = 1;
  color[i][j] = num;
  rep(k, 4) {
    ll nx = i + dx[k];
    ll ny = j + dy[k];
    if (0 <= nx && nx <= h - 1 && 0 <= ny && ny <= w - 1 && s[nx][ny] == '.' &&
        used[nx][ny] == 0) {
      dfs(nx, ny, num);
    }
  }
}

int main() {
  ios;
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  used = vector<vector<ll>>(h, vector<ll>(w));
  color = vector<vector<ll>>(h, vector<ll>(w));
  s = vector<vector<char>>(h, vector<char>(w));
  rep(i, h) {
    rep(j, w) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        color[i][j] = -1;
    }
  }
  ll cnt = 0;
  rep(i, h) {
    rep(j, w) {
      if (used[i][j] == 1)
        continue;
      if (color[i][j] == -1)
        continue;
      dfs(i, j, cnt);
      cnt++;
    }
  }
  /*rep(i,h){
      rep(j,w){
          cout << color[i][j] << " ";
      }
      cout << endl;
  }*/
  map<pll, ll> t;
  vector<set<ll>> to(cnt);
  vector<vector<ll>> m;
  m.resize(cnt);
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '#')
        continue;
      rep(a, 3) {
        rep(b, 3) {
          if (ddx[a] == 0 && ddy[b] == 0)
            continue;
          if (color[i][j] == -1)
            continue;
          ll nx = i + ddx[a];
          ll ny = j + ddy[b];
          if (0 <= nx && nx <= h - 1 && 0 <= ny && ny <= w - 1 &&
              color[nx][ny] != -1 && color[nx][ny] != color[i][j] &&
              to[color[i][j]].find(color[nx][ny]) == to[color[i][j]].end()) {
            // m[color[i][j]].push_back(color[nx][ny]);
            // m[color[nx][ny]].push_back(color[i][j]);
            // t[make_pair(color[i][j],color[nx][ny])]=1;
            // t[make_pair(color[nx][ny],color[i][j])]=1;
            to[color[i][j]].insert(color[nx][ny]);
            to[color[nx][ny]].insert(color[i][j]);
          }
        }
      }
    }
  }

  vector<ll> vs(cnt);
  vector<ll> d(cnt, INF);
  queue<ll> que;
  que.push(color[ch][cw]);
  d[color[ch][cw]] = 0;
  ll times = 0;
  while (!que.empty()) {
    ll x = que.front();
    que.pop();
    vs[x] = 1;
    times++;
    if (times > h * w)
      break;
    for (auto v = to[x].begin(); v != to[x].end(); v++) {
      ll w = *v;
      if (vs[w] == 1)
        continue;
      v = to[x].erase(to[x].find(w));
      to[w].erase(x);
      d[w] = min(d[w], d[x] + 1);
      que.push(w);
    }
  }
  if (d[color[dh][dw]] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << d[color[dh][dw]] << endl;
  return 0;
}