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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> adj(h, vector<char>(w));
  rep(i, h) rep(j, w) { cin >> adj[i][j]; }
  auto valid = [&](int x, int y) -> bool {
    return (x < h && y < w && x >= 0 && y >= 0);
  };

  queue<pair<int, int>> Q;
  int memo[h][w], warcost = 0;
  rep(i, h) rep(j, w) memo[i][j] = -1;
  Q.push(make_pair(ch, cw));
  memo[ch][cw] = warcost;
  int dou = 0;
  while (!Q.empty()) {
    vector<pair<int, int>> now;
    while (!Q.empty()) {
      pair<int, int> p = Q.front();
      Q.pop();
      now.emplace_back(make_pair(p.first, p.second));
      if (p.first == dh && p.second == dw) {
        dou = 1;
        break;
      }
      for (int i = -1; i <= 1; i++) {
        int x = p.first + i, y = p.second;
        if (valid(x, y) && memo[x][y] == -1 && adj[x][y] == '.') {
          Q.push(make_pair(x, y));
          memo[x][y] = warcost;
        }
      }
      for (int i = -1; i <= 1; i++) {
        int x = p.first, y = p.second + i;
        if (valid(x, y) && memo[x][y] == -1 && adj[x][y] == '.') {
          Q.push(make_pair(x, y));
          memo[x][y] = warcost;
        }
      }
    }
    if (dou == 1)
      break;
    warcost++;
    // warpでたどり着けるもののうち、前のcostまでで訪問しきっていないところをQに格納
    rep(i, now.size()) {
      pair<int, int> p = now[i];
      for (int j = p.first - 2; j <= p.first + 2; j++)
        for (int k = p.second - 2; k <= p.second + 2; k++) {
          if (valid(j, k) && memo[j][k] == -1 && adj[j][k] == '.') {
            Q.push(make_pair(j, k));
            memo[j][k] = warcost;
          }
        }
    }
  }
  cout << memo[dh][dw] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<vector<char>> adj(h, vector<char>(w));
  rep(i, h) rep(j, w) { cin >> adj[i][j]; }
  auto valid = [&](int x, int y) -> bool {
    return (x < h && y < w && x >= 0 && y >= 0);
  };

  queue<pair<int, int>> Q;
  int memo[h][w], warcost = 0;
  rep(i, h) rep(j, w) memo[i][j] = -1;
  Q.push(make_pair(ch, cw));
  int dou = 0;
  while (!Q.empty()) {
    vector<pair<int, int>> now;
    while (!Q.empty()) {
      pair<int, int> p = Q.front();
      Q.pop();
      memo[p.first][p.second] = warcost;
      now.emplace_back(make_pair(p.first, p.second));
      if (p.first == dh && p.second == dw) {
        dou = 1;
        break;
      }
      for (int i = -1; i <= 1; i++) {
        int x = p.first + i, y = p.second;
        if (valid(x, y) && memo[x][y] == -1 && adj[x][y] == '.')
          Q.push(make_pair(x, y));
      }
      for (int i = -1; i <= 1; i++) {
        int x = p.first, y = p.second + i;
        if (valid(x, y) && memo[x][y] == -1 && adj[x][y] == '.')
          Q.push(make_pair(x, y));
      }
    }
    if (dou == 1)
      break;
    warcost++;
    // warpでたどり着けるもののうち、前のcostまでで訪問しきっていないところをQに格納
    rep(i, now.size()) {
      pair<int, int> p = now[i];
      for (int j = p.first - 2; j <= p.first + 2; j++)
        for (int k = p.second - 2; k <= p.second + 2; k++) {
          if (valid(j, k) && memo[j][k] == -1 && adj[j][k] == '.')
            Q.push(make_pair(j, k));
        }
    }
  }
  cout << memo[dh][dw] << endl;
  return 0;
}