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

#include <bits/stdc++.h>

using namespace std;

struct Point {
  int cost, x, y;
  bool operator<(const Point &o) const { return cost > o.cost; }
};

int h, w;
priority_queue<Point> q;
int c[1010][1010];
int m[1010][1010];
int vis[1010][1010];
int color[1010][1010];

int insize(int x, int y) {
  return 1 <= x && x <= h && 1 <= y && y <= w && !m[x][y];
}

void dfs(int x, int y, int col) {
  color[x][y] = col;
  if (insize(x + 1, y) && !color[x + 1][y])
    dfs(x + 1, y, col);
  if (insize(x - 1, y) && !color[x - 1][y])
    dfs(x - 1, y, col);
  if (insize(x, y + 1) && !color[x][y + 1])
    dfs(x, y + 1, col);
  if (insize(x, y - 1) && !color[x][y - 1])
    dfs(x, y - 1, col);
}

int main() {
  scanf("%d%d", &h, &w);
  int hc, wc, hd, wd;
  scanf("%d%d%d%d", &hc, &wc, &hd, &wd);
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) {
      char c;
      scanf(" %c", &c);
      m[i][j] = c == '#';
    }

  int cnt = 0;
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      if (!m[i][j] && !color[i][j])
        dfs(i, j, ++cnt);

  q.push({0, hc, wc});
  memset(c, 0x3f, sizeof(c));
  c[hc][wc] = 0;
  while (!q.empty()) {
    int x = q.top().x, y = q.top().y;
    q.pop();
    if (vis[x][y])
      continue;
    vis[x][y] = 1;

    for (int dx = -2; dx <= 2; ++dx)
      for (int dy = -2; dy <= 2; ++dy) {
        int cost = color[x][y] != color[x + dx][y + dy];
        if (insize(x + dx, y + dy) && c[x + dx][y + dy] > c[x][y] + cost) {
          c[x + dx][y + dy] = c[x][y] + cost;
          q.push({c[x + dx][y + dy], x + dx, y + dy});
        }
      }
  }

  if (c[hd][wd] == 0x3f3f3f3f) {
    puts("-1");
    return 0;
  }
  printf("%d\\n", c[hd][wd]);
}


Error: TLE

Fixed submission:

/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout << "\\n";
#define ll long long int
#define REP(i, a, n) for (i = a; i <= n; i++)
#define F(i, a, b) for (i = a; i < b; i++)
#define RF(i, b, a) for (i = b; i >= a; i--)
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
using namespace std;
const ll mod = 1e16;
const ll MAXN = 200200;
ll i, j, mask, test;
int main() {
  IOS
      /*#ifndef ONLINE_JUDGE
         freopen("input.txt","r",stdin);
         freopen("output.txt","w",stdout);
      #endif // ONLINE_JUDGE*/
      ll h,
      w, a, b, c, d;
  cin >> h >> w >> a >> b >> c >> d;
  char t[h + 2][w + 2];
  F(i, 0, h) {
    F(j, 0, w) { cin >> t[i][j]; }
  }
  ll v[h + 2][w + 2], vis[h + 2][w + 2];
  memset(v, -1, sizeof(v));
  memset(vis, 0, sizeof(vis));
  a--;
  b--;
  c--;
  d--;
  queue<pair<ll, ll>> q;
  q.push(make_pair(a, b));
  v[a][b] = 0;
  ll xc[] = {0, 0, -1, 1};
  ll yc[] = {1, -1, 0, 0};
  while (!q.empty()) {
    pair<ll, ll> p = q.front();
    vis[p.first][p.second] = 0;
    q.pop();
    F(i, 0, 4) {
      ll u = p.first + xc[i], g = p.second + yc[i];
      if (u < 0 || u >= h || g < 0 || g >= w || t[u][g] == '#')
        continue;
      if (v[u][g] == -1 || v[u][g] > v[p.first][p.second]) {
        v[u][g] = v[p.first][p.second];
        if (vis[u][g] == 0) {
          q.push({u, g});
          vis[u][g] = 1;
        }
      }
    }
    F(i, p.first - 2, p.first + 3) {
      F(j, p.second - 2, p.second + 3) {
        // cout<<i<<" "<<j;nl
        if (i < 0 || i >= h || j < 0 || j >= w || t[i][j] == '#')
          continue;
        if (v[i][j] == -1 || v[i][j] > v[p.first][p.second] + 1) {
          v[i][j] = v[p.first][p.second] + 1;
          if (vis[i][j] == 0) {
            q.push({i, j});
            vis[i][j] = 1;
          }
        }
      }
    }
  }
  /*F(i,0,h)
  {
      F(j,0,w)
      {
          cout<<v[i][j]<<" ";
      }
      nl
  }
  */
  cout << v[c][d];
  return 0;
}