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

#include <bits/stdc++.h>
#define INF 1001001001
typedef long long ll;
using namespace std;
int ch, cw, dh, dw;
int h, w;
vector<int> g[1000010];
bool isr[1000][1000];
int label[1000][1000];
vector<pair<int, int>> labelv[1000010];
char s[1000][1000];
void dfs(int xx, int yy, int la) {
  if (0 > xx || xx >= w || 0 > yy || yy >= h || s[yy][xx] == '#' ||
      isr[yy][xx]) {
    return;
  }
  isr[yy][xx] = true;
  label[yy][xx] = la;
  labelv[la].push_back(make_pair(xx, yy));
  dfs(xx + 1, yy, la);
  dfs(xx - 1, yy, la);
  dfs(xx, yy + 1, la);
  dfs(xx, yy - 1, la);
}
vector<int> dist(1000010, -1);
void bfs(int st) {
  dist[st] = 0;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int n = q.front();
    for (auto v : g[n]) {
      if (dist[v] == -1) {
        dist[v] = dist[n] + 1;
        q.push(v);
      }
    }
    q.pop();
  }
}
int main() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int lab = 1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dfs(j, i, lab);
      lab++;
    }
  }
  for (int i = 1; i < 1000010; i++) {
    int sz = labelv[i].size();
    if (sz == 0)
      continue;
    set<int> ss;
    for (int j = 0; j < sz; j++) {
      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          int nx = labelv[i][j].first + k;
          int ny = labelv[i][j].second + l;
          if (0 > nx || nx >= w || 0 > ny || ny >= h)
            continue;
          if (label[ny][nx] >= 1) {
            ss.insert(label[ny][nx]);
          }
        }
      }
    }
    for (auto v : ss) {
      g[i].push_back(v);
      g[v].push_back(i);
    }
  }
  bfs(label[ch][cw]);
  if (dist[label[dh][dw]] == -1)
    printf("-1\\n");
  else
    printf("%d\\n", dist[label[dh][dw]]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1001001001
typedef long long ll;
using namespace std;
int ch, cw, dh, dw;
int h, w;
int dist[1000][1000];
char s[1000][1000];
void bfs() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dist[i][j] = INF;
    }
  }
  // queue<pair<int, int>> q;
  vector<pair<int, int>> q(10000100);
  int vx[4] = {0, 1, 0, -1};
  int vy[4] = {1, 0, -1, 0};
  dist[ch][cw] = 0;
  int head = 0, tail = 0;
  q[tail] = make_pair(cw, ch);
  tail++;
  while (head != tail) {
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        auto np = q[head];
        int nx = np.first + i;
        int ny = np.second + j;
        if (0 > nx || nx >= w || 0 > ny || ny >= h)
          continue;
        bool isM = false;
        for (int k = 0; k < 4; k++) {
          if (i == vx[k] && j == vy[k]) {
            isM = true;
            break;
          }
        }
        if (isM) {
          if (dist[ny][nx] > dist[np.second][np.first] && s[ny][nx] == '.') {
            dist[ny][nx] = dist[np.second][np.first];
            q[tail] = make_pair(nx, ny);
            tail++;
          }
        } else {
          if (dist[ny][nx] > dist[np.second][np.first] + 1 &&
              s[ny][nx] == '.') {
            dist[ny][nx] = dist[np.second][np.first] + 1;
            q[tail] = make_pair(nx, ny);
            tail++;
          }
        }
      }
    }
    head++;
  }
}
int main() {
  scanf("%d%d%d%d%d%d", &h, &w, &ch, &cw, &dh, &dw);
  // cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  for (int i = 0; i < h; i++) {
    scanf("%s", s[i]);
  }
  bfs();
  // for (int i = 0; i < h; i++) {
  // 	for (int j = 0; j < w; j++) {
  // 		cout << dist[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  if (dist[dh][dw] == INF)
    printf("-1\\n");
  else
    printf("%d\\n", dist[dh][dw]);
  return 0;
}