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

#include <bits/stdc++.h>
using namespace std;
#define P pair<int, int>
int main() {
#define int long long
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> vec(H);
  vector<vector<int>> tree(H * W, vector<int>(0));
  for (int i = 0; i < H; i++) {
    cin >> vec.at(i);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (k >= 0 && k < H && l >= 0 && l < W) {
            if (vec.at(k).at(l) == '.') {
              tree.at(i * W + j).push_back(k * W + l);
            }
          }
        }
      }
    }
  }
  vector<int> dist(H * W, -1);
  vector<int> determined(H * W, 0);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(make_pair(0, (ch - 1) * W + cw - 1));
  dist.at((ch - 1) * W + cw - 1) = 0;
  while (!que.empty()) {
    int v = que.top().second;
    int p = que.top().first;
    que.pop();
    if (determined.at(v) == 1) {
      continue;
    }
    determined.at(v) = 1;
    for (int i = 0; i < (int)tree.at(v).size(); i++) {
      int nv = tree.at(v).at(i);
      if (determined.at(nv) == 1) {
        continue;
      }
      int memo = dist.at(v);
      int sa = nv - v;
      if (sa == 1 || sa == -1 || sa == W || sa == -W) {
      } else {
        memo++;
      }
      if (dist.at(nv) != -1 && memo >= dist.at(nv)) {
        continue;
      }
      if (dist.at(nv) == -1) {
        dist.at(nv) = memo;
        que.push(make_pair(memo, nv));
        // cout<<v+1<<" "<<nv+1<<" "<<dist.at(nv)<<endl;
      }
      if (dist.at(nv) != -1 && memo < dist.at(nv)) {
        dist.at(nv) = memo;
        que.push(make_pair(memo, nv));
        // cout<<v+1<<" "<<nv+1<<" "<<dist.at(nv)<<endl;
      }
      dist.at(nv) = memo;
    }
  }
  cout << dist.at((dh - 1) * W + dw - 1) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
#define int long long
  int H, W;
  cin >> H >> W;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<vector<char>> vec(H, vector<char>(W));
  vector<vector<int>> tree(H * W, vector<int>(0));
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    for (int j = 0; j < W; j++) {
      vec.at(i).at(j) = S.at(j);
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {

      for (int k = i - 2; k <= i + 2; k++) {
        for (int l = j - 2; l <= j + 2; l++) {
          if (k >= 0 && k < H && l >= 0 && l < W) {
            if (vec.at(k).at(l) == '.') {
              tree.at(i * W + j).push_back(k * W + l);
            }
          }
        }
      }
    }
  }
  vector<int> DIST(H * W, -1);
  int cc = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (DIST.at(i * W + j) != -1) {
        continue;
      }
      queue<int> que;
      que.push(i * W + j);
      DIST.at(i * W + j) = 0;
      while (!que.empty()) {
        int v = que.front();
        que.pop();
        int check = 0;
        for (int k = 0; k < (int)tree.at(v).size(); k++) {
          int nv = tree.at(v).at(k);
          // cout<<v<<" "<<nv<<endl;
          int sa = nv - v;
          if (sa == 1 || sa == -1 || sa == W || sa == -W) {
          } else {
            continue;
          }
          if (DIST.at(nv) != -1) {
            continue;
          }
          // cout<<v<<" "<<nv<<endl;
          que.push(nv);
          DIST.at(nv) = cc;
        }
      }
      cc++;
    }
  }
  vector<int> dist(H * W, -1);
  queue<int> que;
  que.push((ch - 1) * W + cw - 1);
  dist.at((ch - 1) * W + cw - 1) = 0;
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (int i = 0; i < (int)tree.at(v).size(); i++) {
      int nv = tree.at(v).at(i);
      int memo = dist.at(v);
      int sa = nv - v;
      if (sa == 1 || sa == -1 || sa == W || sa == -W) {
      } else {
        memo++;
        if (DIST.at(v) == DIST.at(nv)) {
          continue;
        }
      }
      if (dist.at(nv) != -1 && memo >= dist.at(nv)) {
        continue;
      }
      dist.at(nv) = memo;
      que.push(nv);
    }
  }
  cout << dist.at((dh - 1) * W + dw - 1) << endl;
}