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

// 解説見た
#include <bits/stdc++.h>
typedef long long lint;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;

  // lint h = 1000;
  // lint w = 1000;
  // lint ch = 1;
  // lint cw = 1;
  // lint dh = 1000;
  // lint dw = 1000;

  vector<string> s(h);
  rep(i, h) cin >> s[i];

  // bfs
  //  <y,x>,cnt
  vector<vector<int>> cnt(h, vector<int>(w, 100000000));
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      hq;
  // cnt[ch-1][cw-1] = 0;
  hq.push(make_pair(0, make_pair(ch - 1, cw - 1)));
  vector<int> nex = {0, 0, -1, 1};
  vector<int> ney = {-1, 1, 0, 0};
  while (not hq.empty()) {
    auto t = hq.top();
    hq.pop();
    int cost = t.first;
    int y = t.second.first;
    int x = t.second.second;
    // cout << y << ' ' << x << endl;
    if (cnt[y][x] != 100000000)
      continue;
    cnt[y][x] = cost;
    rep(yyy, 5) {
      rep(xxx, 5) {
        if (yyy == 3 && xxx == 3)
          continue;
        int iy = 2 - yyy;
        int ix = 2 - xxx;
        int ny = y + iy;
        int nx = x + ix;
        if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.') {
          // auto old = cnt[ny][nx];
          // cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]+1);
          // if (old != cnt[ny][nx]) que.push(make_pair(ny,nx));
          if (cnt[ny][nx] > cnt[y][x] + 1)
            hq.push(make_pair(cnt[y][x] + 1, make_pair(ny, nx)));
        }
      }
    }

    rep(i, 4) {
      int ny = y + ney[i];
      int nx = x + nex[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.') {
        // auto old = cnt[ny][nx];
        // cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]);
        // if (old != cnt[ny][nx]) que.push(make_pair(ny,nx));
        if (cnt[ny][nx] > cnt[y][x])
          hq.push(make_pair(cnt[y][x], make_pair(ny, nx)));
      }
    }
  }
  int ans = cnt[dh - 1][dw - 1];
  if (ans == 100000000)
    ans = -1;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;

  // lint h = 1000;
  // lint w = 1000;
  // lint ch = 1;
  // lint cw = 1;
  // lint dh = 1000;
  // lint dw = 1000;

  vector<string> s(h);
  rep(i, h) cin >> s[i];

  // bfs
  //  <y,x>,cnt
  vector<vector<int>> cnt(h, vector<int>(w, 100000000));
  queue<pair<int, int>> que;
  cnt[ch - 1][cw - 1] = 0;
  que.push(make_pair(ch - 1, cw - 1));
  vector<int> nex = {0, 0, -1, 1};
  vector<int> ney = {-1, 1, 0, 0};
  while (not que.empty()) {
    auto t = que.front();
    que.pop();
    int y = t.first;
    int x = t.second;
    rep(yyy, 5) {
      rep(xxx, 5) {
        if (yyy == 3 && xxx == 3)
          continue;
        int iy = 2 - yyy;
        int ix = 2 - xxx;
        int ny = y + iy;
        int nx = x + ix;
        if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.') {
          auto old = cnt[ny][nx];
          cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x] + 1);
          if (old != cnt[ny][nx])
            que.push(make_pair(ny, nx));
        }
      }
    }

    rep(i, 4) {
      int ny = y + ney[i];
      int nx = x + nex[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.') {
        auto old = cnt[ny][nx];
        cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]);
        if (old != cnt[ny][nx])
          que.push(make_pair(ny, nx));
      }
    }
  }
  int ans = cnt[dh - 1][dw - 1];
  if (ans == 100000000)
    ans = -1;
  cout << ans << endl;
}