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

#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int h, w, c1, c2, d1, d2;
vector<string> mp;
unordered_map<int, int> color;
unordered_map<int, int> coord;
vector<unordered_set<int>> g(1000020, unordered_set<int>());
unordered_set<int> vis;

int encode(int i, int j) { return i * w + j; }

void decode(int idx, int &i, int j) {
  i = idx / w;
  j = idx % w;
}

void build() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int di = -2; di <= 2; ++di) {
        for (int dj = -2; dj <= 2; ++dj) {
          int ni = i + di, nj = j + dj;
          if (ni < 0 || ni >= h || nj < 0 || nj >= w || mp[ni][nj] == '#')
            continue;
          int c1 = color[encode(i, j)];
          int c2 = color[encode(ni, nj)];
          g[c1].insert(c2);
        }
      }
    }
  }
}

void coloring(int i, int j, int c) {
  if (i < 0 || i >= h || j < 0 || j >= w || mp[i][j] == '#')
    return;
  int idx = encode(i, j);
  if (color.find(idx) == color.end()) {
    color[idx] = c;
    coloring(i + 1, j, c);
    coloring(i - 1, j, c);
    coloring(i, j + 1, c);
    coloring(i, j - 1, c);
  }
}

struct S {
  int idx, step;
};

int bfs(int start, int end) {
  if (start == end)
    return 0;
  queue<S> qu;
  qu.push(S{start, 0});
  vis.insert(start);
  while (!qu.empty()) {
    S cur = qu.front();
    qu.pop();
    for (int nc : g[cur.idx]) {
      if (vis.find(nc) != vis.end()) {
        continue;
      }
      if (nc == end) {
        return cur.step + 1;
      }
      vis.insert(nc);
      qu.push(S{nc, cur.step + 1});
    }
  }
  return -1;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w >> c1 >> c2 >> d1 >> d2;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    mp.push_back(s);
  }
  c1--;
  c2--;
  d1--;
  d2--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      coloring(i, j, encode(i, j) + 1);
    }
  }
  build();
  int ans = bfs(color[encode(c1, c2)], color[encode(d1, d2)]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int h, w, c1, c2, d1, d2;
vector<string> mp;
map<int, int> color;
map<int, int> coord;
vector<set<int>> g(2000000, set<int>());
set<int> vis;

int encode(int i, int j) { return i * w + j; }

void decode(int idx, int &i, int j) {
  i = idx / w;
  j = idx % w;
}

void build() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int di = -2; di <= 2; ++di) {
        for (int dj = -2; dj <= 2; ++dj) {
          int ni = i + di, nj = j + dj;
          if (ni < 0 || ni >= h || nj < 0 || nj >= w || mp[ni][nj] == '#')
            continue;
          int c1 = color[encode(i, j)];
          int c2 = color[encode(ni, nj)];
          g[c1].insert(c2);
        }
      }
    }
  }
}

void coloring(int i, int j, int c) {
  if (i < 0 || i >= h || j < 0 || j >= w || mp[i][j] == '#')
    return;
  int idx = encode(i, j);
  if (color.find(idx) == color.end()) {
    color[idx] = c;
    coloring(i + 1, j, c);
    coloring(i - 1, j, c);
    coloring(i, j + 1, c);
    coloring(i, j - 1, c);
  }
}

struct S {
  int idx, step;
};

int bfs(int start, int end) {
  if (start == end)
    return 0;
  queue<S> qu;
  qu.push(S{start, 0});
  vis.insert(start);
  while (!qu.empty()) {
    S cur = qu.front();
    qu.pop();
    for (int nc : g[cur.idx]) {
      if (vis.find(nc) != vis.end()) {
        continue;
      }
      if (nc == end) {
        return cur.step + 1;
      }
      vis.insert(nc);
      qu.push(S{nc, cur.step + 1});
    }
  }
  return -1;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> h >> w >> c1 >> c2 >> d1 >> d2;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    mp.push_back(s);
  }
  c1--;
  c2--;
  d1--;
  d2--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      coloring(i, j, encode(i, j) + 1);
    }
  }
  build();
  int ans = bfs(color[encode(c1, c2)], color[encode(d1, d2)]);
  cout << ans << endl;
  return 0;
}