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

/*
    Author : Nishant Gupta 2.0
*/
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;

#define LL long long int
#define getcx getchar_unlocked
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define MAX 1000005
#define LOG_MAX 20
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL (LL(1e18))
#define chk(a) cerr << endl << #a << " : " << a << endl
#define chk2(a, b)                                                             \\
  cerr << endl << #a << " : " << a << "\\t" << #b << " : " << b << endl
#define chk3(a, b, c)                                                          \\
  cerr << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << endl
#define chk4(a, b, c, d)                                                       \\
  cerr << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << "\\t" << #d << " : " << d << endl
#define rep(i, a, n) for (i = a; i < n; i++)
#define rev(i, a, n) for (i = a; i >= n; i--)
#define in(x) scanf("%d", &x)
#define inl(x) scanf("%lld", &x)
#define in2(x, y) scanf("%d %d", &x, &y)
#define inl2(x, y) scanf("%lld %lld", &x, &y)
#define MSV(A, a) memset(A, a, sizeof(A))
#define rep_itr(itr, c) for (itr = (c).begin(); itr != (c).end(); itr++)
#define finish(x)                                                              \\
  {                                                                            \\
    cout << x << '\\n';                                                         \\
    return;                                                                    \\
  }
typedef pair<int, int> pi;
typedef pair<LL, LL> pl;
const char en = '\\n';

int n, m;
vector<pi> adj[MAX];
bool valid(int i, int j) { return i >= 0 && j >= 0 && i < n && j < m; }

/* Dijkstra Algo */
int dijkstra(int s, int d) {
  int mincost[MAX], i;
  rep(i, 0, MAX) mincost[i] = INT_MAX;

  priority_queue<pi, vector<pi>, greater<pi>> pq;
  pq.push(MP(0, s));
  mincost[s] = 0;

  while (!pq.empty()) {
    pi _top = pq.top();
    pq.pop();
    int node = _top.Y;

    for (i = 0; i < adj[node].size(); i++) {
      int ch = adj[node][i].X;
      int wt = adj[node][i].Y;
      if (mincost[node] + wt < mincost[ch]) {
        mincost[ch] = mincost[node] + wt;
        pq.push(MP(mincost[ch], ch));
      }
    }
  }

  return mincost[d];
}

int get_node(int i, int j) { return i * m + j; }

void solve() {
  in2(n, m);
  int si, sj, di, dj;
  in2(si, sj);
  in2(di, dj);
  si--;
  sj--;
  di--;
  dj--;

  string s[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '#')
        continue;
      int u = get_node(i, j);

      for (int xi = i - 2; xi <= i + 2; xi++) {
        for (int xj = j - 2; xj <= j + 2; xj++) {
          if (valid(xi, xj) && s[xi][xj] == '.') {
            int wt = (abs(xi - i) + abs(xj - j)) == 1 ? 0 : 1;
            int v = get_node(xi, xj);
            adj[u].PB(MP(v, wt));
          }
        }
      }
    }
  }

  int st = get_node(si, sj);
  int dest = get_node(di, dj);
  int ans = dijkstra(st, dest);
  if (ans == INT_MAX)
    ans = -1;
  cout << ans << en;
}

int main() {
  int t = 1;

  //~cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
    Author : Nishant Gupta 2.0
*/
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;

#define LL long long int
#define getcx getchar_unlocked
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define MAX 1005
#define LOG_MAX 20
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL (LL(1e18))
#define chk(a) cerr << endl << #a << " : " << a << endl
#define chk2(a, b)                                                             \\
  cerr << endl << #a << " : " << a << "\\t" << #b << " : " << b << endl
#define chk3(a, b, c)                                                          \\
  cerr << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << endl
#define chk4(a, b, c, d)                                                       \\
  cerr << endl                                                                 \\
       << #a << " : " << a << "\\t" << #b << " : " << b << "\\t" << #c << " : "  \\
       << c << "\\t" << #d << " : " << d << endl
#define rep(i, a, n) for (i = a; i < n; i++)
#define rev(i, a, n) for (i = a; i >= n; i--)
#define in(x) scanf("%d", &x)
#define inl(x) scanf("%lld", &x)
#define in2(x, y) scanf("%d %d", &x, &y)
#define inl2(x, y) scanf("%lld %lld", &x, &y)
#define MSV(A, a) memset(A, a, sizeof(A))
#define rep_itr(itr, c) for (itr = (c).begin(); itr != (c).end(); itr++)
#define finish(x)                                                              \\
  {                                                                            \\
    cout << x << '\\n';                                                         \\
    return;                                                                    \\
  }
typedef pair<int, int> pi;
typedef pair<LL, LL> pl;
const char en = '\\n';

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};

bool vis[MAX][MAX];
int di, dj, color = 1;
int col[MAX][MAX];
string s[MAX];
int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};

bool valid(int i, int j, int n, int m) {
  return i >= 0 && j >= 0 && i < n && j < m;
}

void call(int i, int j, int n, int m, int c) {
  vis[i][j] = true;
  col[i][j] = c;

  for (int x = 0; x < 4; x++) {
    int ni = i + dir[x][0];
    int nj = j + dir[x][1];

    if (valid(ni, nj, n, m) && s[ni][nj] == '.' && !vis[ni][nj]) {
      call(ni, nj, n, m, c);
    }
  }

  vis[i][j] = false;
}

void solve() {
  int n, m;
  in2(n, m);
  int si, sj;
  in2(si, sj);
  in2(di, dj);
  si--;
  sj--;
  di--;
  dj--;

  for (int i = 0; i < n; i++)
    cin >> s[i];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!col[i][j] && s[i][j] == '.') {
        call(i, j, n, m, color);
        color++;
      }
    }
  }

  unordered_map<pi, bool, hash_pair> mp;
  vector<int> adj[color + 1];
  MSV(adj, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '#')
        continue;
      for (int xi = i - 2; xi <= i + 2; xi++) {
        for (int xj = j - 2; xj <= j + 2; xj++) {
          if (valid(xi, xj, n, m) && col[i][j] != col[xi][xj] &&
              mp[MP(col[i][j], col[xi][xj])] == false) {
            adj[col[i][j]].push_back(col[xi][xj]);
            mp[MP(col[i][j], col[xi][xj])] = true;
          }
        }
      }
    }
  }

  int st = col[si][sj];
  int dest = col[di][dj];
  bool v[color + 1];
  MSV(v, false);
  queue<pi> q;
  q.push(MP(st, 0));
  v[st] = true;

  int ans = -1;
  while (!q.empty()) {
    int node = q.front().X;
    int step = q.front().Y;
    q.pop();

    if (node == dest) {
      ans = step;
      break;
    }
    for (int i = 1; i < adj[node].size(); i++) {
      if (node != adj[node][i] && !v[adj[node][i]]) {
        q.push(MP(adj[node][i], step + 1));
        v[adj[node][i]] = true;
      }
    }
  }
  cout << ans << en;
}

int main() {
  int t = 1;

  //~cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}