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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int H, W;
vector<string> G;
vector<vector<int>> d;
vector<vector<bool>> used;
int x[4] = {1, -1, 0, 0};
int y[4] = {0, 0, 1, -1};
int nexth, nextw;
bool check;
queue<pair<int, iint>> q;
queue<iint> qq;
vector<vector<bool>> uused;

void dfs(int ch, int cw, int times) {
  d[ch][cw] = times;
  used[ch][cw] = true;
  for (int i = 0; i < 4; i++) {
    if (ch + x[i] < H && ch + x[i] >= 0 && cw + y[i] < W && cw + y[i] >= 0) {
      if (used[ch + x[i]][cw + y[i]])
        continue;
      d[ch + x[i]][cw + y[i]] = times;
      used[ch + x[i]][cw + y[i]] = true;
      dfs(ch + x[i], cw + y[i], times);
    }
  }
}

void ddfs(int ch, int cw, int times) {
  uused[ch][cw] = true;
  for (int i = -2; i < 3; i++) {
    for (int j = -2; j < 3; j++) {
      if (ch + i < H && ch + i >= 0 && cw + j < W && cw + j >= 0) {
        if (used[ch + i][cw + j])
          continue;
        d[ch + i][cw + j] = times + 1;
        used[ch + i][cw + j] = true;
        q.push(make_pair(times + 1, make_pair(ch + i, cw + j)));
      }
    }
  }
  for (int i = 0; i < 4; i++) {
    if (ch + x[i] < H && ch + x[i] >= 0 && cw + y[i] < W && cw + y[i] >= 0) {
      if (uused[ch + x[i]][cw + y[i]] || G[ch + x[i]][cw + y[i]] == '#')
        continue;
      ddfs(ch + x[i], cw + y[i], times);
    }
  }
}

void big(int times) {
  check = false;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (d[i][j] == times) {
        for (int k = -2; k < 3; k++) {
          for (int l = -2; l < 3; l++) {
            if (i + k < H && i + k >= 0 && j + l < W && j + l >= 0) {
              if (used[i + k][j + l])
                continue;
              d[i + k][j + l] = times + 1;
              used[i + k][j + l] = true;
              q.push(make_pair(times + 1, make_pair(i + k, j + l)));
            }
          }
        }
      }
    }
  }
}

int main() {
  int ch, cw, dh, dw;
  cin >> H >> W;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  G.resize(H);
  d.resize(H);
  used.resize(H);
  uused.resize(H);
  for (int i = 0; i < H; i++) {
    d[i].resize(W);
    used[i].resize(W);
    uused[i].resize(W);
    fill(ALL(d[i]), -1);
    fill(ALL(used[i]), false);
    fill(ALL(uused[i]), false);
  }
  for (int i = 0; i < H; i++) {
    cin >> G[i];
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (G[i][j] == '#') {
        used[i][j] = true;
      }
    }
  }

  // for (int i = 0; i < H; i++) {
  //     for (int j = 0; j < W; j++) {
  //         if(used[i][j]){
  //             printf("0 ");
  //         }
  //         else{
  //             printf("1 ");
  //         }
  //     }
  //     cout << endl;
  // }

  check = true;
  int nowh, noww;
  q.push(make_pair(0, make_pair(ch, cw)));
  int tt, hh, ww;

  int t = 0;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    tt = v.first;
    hh = v.second.first;
    ww = v.second.second;
    qq.push(make_pair(hh, ww));
    dfs(hh, ww, tt);
    if (q.empty()) {
      while (!qq.empty()) {
        auto x = qq.front();
        qq.pop();
        hh = x.first;
        ww = x.second;
        ddfs(hh, ww, t);
      }
      t++;
    }
    if (used[dh][dw])
      break;
  }

  printf("%d\\n", d[dh][dw]);
  // for (int i = 0; i < H; i++) {
  //     for (int j = 0; j < W; j++) {
  //         printf("%d ", d[i][j]);

  //     }
  //     cout << endl;
  // }

  // for (int i = 0; i < H; i++) {
  //     for (int j = 0; j < W; j++) {
  //         if(uused[i][j]){
  //             printf("0 ");
  //         }
  //         else{
  //             printf("1 ");
  //         }
  //     }
  //     cout << endl;
  // }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> iint;
typedef pair<ll, ll> llll;
#define ALL(x) (x).begin(), (x).end()
const ll zero = 0;
const ll INF = 3000000000000000000; // 10^18
const int inINF = 1000000000;       // 10^9
const ll MOD = 1000000007;          // 10^9+7
const ll MOD2 = 998244353;
void Yes() { printf("Yes\\n"); }
void No() { printf("No\\n"); }
void YES() { printf("YES\\n"); }
void NO() { printf("NO\\n"); }

int H, W;
vector<string> G;
vector<vector<int>> d;
vector<vector<bool>> used;
int x[4] = {1, -1, 0, 0};
int y[4] = {0, 0, 1, -1};
int nexth, nextw;
bool check;
queue<pair<int, iint>> q;

void dfs(int ch, int cw, int times) {
  d[ch][cw] = times;
  used[ch][cw] = true;
  for (int i = 0; i < 4; i++) {
    if (ch + x[i] < H && ch + x[i] >= 0 && cw + y[i] < W && cw + y[i] >= 0) {
      if (used[ch + x[i]][cw + y[i]])
        continue;
      d[ch + x[i]][cw + y[i]] = times;
      used[ch + x[i]][cw + y[i]] = true;
      dfs(ch + x[i], cw + y[i], times);
    }
  }
}

void big(int times) {
  check = false;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (d[i][j] == times) {
        for (int k = -2; k < 3; k++) {
          for (int l = -2; l < 3; l++) {
            if (i + k < H && i + k >= 0 && j + l < W && j + l >= 0) {
              if (used[i + k][j + l])
                continue;
              d[i + k][j + l] = times + 1;
              used[i + k][j + l] = true;
              q.push(make_pair(times + 1, make_pair(i + k, j + l)));
            }
          }
        }
      }
    }
  }
}

int main() {
  int ch, cw, dh, dw;
  cin >> H >> W;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  G.resize(H);
  d.resize(H);
  used.resize(H);
  for (int i = 0; i < H; i++) {
    d[i].resize(W);
    used[i].resize(W);
    fill(ALL(d[i]), -1);
    fill(ALL(used[i]), false);
  }
  for (int i = 0; i < H; i++) {
    cin >> G[i];
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (G[i][j] == '#') {
        used[i][j] = true;
      }
    }
  }

  // for (int i = 0; i < H; i++) {
  //     for (int j = 0; j < W; j++) {
  //         if(used[i][j]){
  //             printf("0 ");
  //         }
  //         else{
  //             printf("1 ");
  //         }
  //     }
  //     cout << endl;
  // }

  check = true;
  int nowh, noww;
  q.push(make_pair(0, make_pair(ch, cw)));
  int tt, hh, ww;

  int t = 0;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    tt = v.first;
    hh = v.second.first;
    ww = v.second.second;
    dfs(hh, ww, tt);
    if (q.empty()) {
      big(t);
      t++;
    }
    if (used[dh][dw])
      break;
  }

  printf("%d\\n", d[dh][dw]);
  // for (int i = 0; i < H; i++) {
  //     for (int j = 0; j < W; j++) {
  //         printf("%d ", d[i][j]);

  //     }
  //     cout << endl;
  // }
}