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

// D - Wizard in Maze
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  int cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  cx--;
  cy--;
  dx--;
  dy--;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int id = 0;
  vector<vector<int>> vi(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (vi[i][j] || s[i][j] == '#')
        continue;
      queue<pair<int, int>> q;
      q.push({i, j});
      vi[i][j] = ++id;
      for (; !q.empty(); q.pop()) {
        int x = q.front().first, y = q.front().second;
        if (x - 1 >= 0 && s[x - 1][y] == '.' && vi[x - 1][y] == 0)
          vi[x - 1][y] = vi[x][y], q.push({x - 1, y});
        if (x + 1 < h && s[x + 1][y] == '.' && vi[x + 1][y] == 0)
          vi[x + 1][y] = vi[x][y], q.push({x + 1, y});
        if (y - 1 >= 0 && s[x][y - 1] == '.' && vi[x][y - 1] == 0)
          vi[x][y - 1] = vi[x][y], q.push({x, y - 1});
        if (y + 1 < w && s[x][y + 1] == '.' && vi[x][y + 1] == 0)
          vi[x][y + 1] = vi[x][y], q.push({x, y + 1});
      }
    }

  vector<int> dist(h * w, -1);
  auto sp = [&](int src) {
    dist[src] = 0;
    priority_queue<pair<int, int>> q;
    for (q.push(make_pair(0, src)); !q.empty();) {
      int u = q.top().second;
      q.pop();
      int x = u / w, y = u % w;
      for (int xx = x - 2; xx <= x + 2; xx++)
        for (int yy = y - 2; yy <= y + 2; yy++) {
          if (xx < 0 || xx >= h || yy < 0 || yy >= w)
            continue;
          if (s[xx][yy] != '.')
            continue;
          // if (vi[x][y] != vi[xx][yy]) cout << "!!!!!" << endl;
          int v = xx * w + yy, d = dist[u] + (vi[x][y] == vi[xx][yy] ? 0 : 1);
          if (dist[v] < 0 || d < dist[v]) {
            dist[v] = d;
            q.push({-d, v});
          }
        }
    }
  };

  sp(cx * w + cy);
  cout << dist[dx * w + dy] << endl;

  return 0;
}


Error: TLE

Fixed submission:

// D - Wizard in Maze
#include <bits/stdc++.h>

using namespace std;

void dijkstra(const vector<vector<int>> &g, int src, vector<int> &dist) {
  int n = g.size();
  dist.assign(n, -1);
  dist[src] = 0;
  priority_queue<pair<int, int>> q;
  for (q.push(make_pair(0, src)); !q.empty();) {
    int u = q.top().second;
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i], d = dist[u] + 1;
      if (dist[v] < 0 || d < dist[v]) {
        dist[v] = d;
        q.push(make_pair(-d, v));
      }
    }
  }
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  int cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  cx--;
  cy--;
  dx--;
  dy--;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  vector<vector<int>> vi(h, vector<int>(w));
  vector<set<pair<int, int>>> g;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (vi[i][j] || s[i][j] == '#')
        continue;
      g.push_back(set<pair<int, int>>());
      queue<pair<int, int>> q;
      q.push({i, j});
      vi[i][j] = g.size();
      for (; !q.empty(); q.pop()) {
        int x = q.front().first, y = q.front().second;
        g.back().insert({x, y});
        if (x - 1 >= 0 && s[x - 1][y] == '.' && vi[x - 1][y] == 0)
          vi[x - 1][y] = g.size(), q.push({x - 1, y});
        if (x + 1 < h && s[x + 1][y] == '.' && vi[x + 1][y] == 0)
          vi[x + 1][y] = g.size(), q.push({x + 1, y});
        if (y - 1 >= 0 && s[x][y - 1] == '.' && vi[x][y - 1] == 0)
          vi[x][y - 1] = g.size(), q.push({x, y - 1});
        if (y + 1 < w && s[x][y + 1] == '.' && vi[x][y + 1] == 0)
          vi[x][y + 1] = g.size(), q.push({x, y + 1});
      }
    }

  int m = g.size();
  vector<vector<int>> t(m, vector<int>(m));
  for (int i = 0; i < m; i++) {
    for (auto p : g[i]) {
      int x = p.first, y = p.second;
      for (int xx = x - 2; xx <= x + 2; xx++)
        for (int yy = y - 2; yy <= y + 2; yy++)
          if (xx >= 0 && xx < h && yy >= 0 && yy < w && vi[xx][yy]) {
            int j = vi[xx][yy] - 1;
            t[i][j] = t[j][i] = 1;
          }
    }
  }

  vector<vector<int>> e(m);
  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++)
      if (i != j && t[i][j])
        e[i].push_back(j);

  int src = vi[cx][cy] - 1, dst = vi[dx][dy] - 1;
  vector<int> dist;
  dijkstra(e, src, dist);

  cout << dist[dst] << endl;

  return 0;
}