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

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
const int MOD = 1000000007;
typedef pair<long long, long long> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int H, W;
  cin >> H >> W;
  int wh, ww;
  cin >> wh >> ww;
  wh--;
  ww--;
  int gh, gw;
  cin >> gh >> gw;
  gh--;
  gw--;
  vector<vector<char>> ban(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> ban.at(i).at(j);
    }
  }
  deque<pair<int, pair<int, int>>> Q;
  Q.push_front(make_pair(0, make_pair(wh, ww)));
  vector<vector<bool>> seen(H, vector<bool>(W));
  int ans = inf;
  vector<vector<int>> mini(H, vector<int>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      mini.at(i).at(j) = inf;
  while (!Q.empty()) {
    pair<int, pair<int, int>> p = Q.front();
    Q.pop_front();
    int nh = p.second.first, nw = p.second.second, cost = p.first;
    if (nh == gh && nw == gw) {
      ans = min(ans, cost);
    }
    mini.at(nh).at(nw) = min(mini.at(nh).at(nw), cost);
    cost = min(cost, mini.at(nh).at(nw));
    seen.at(nh).at(nw) = true;
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (i == j || i == -1 && j == 1 || i == 1 && j == -1)
          continue;
        if (nh + i < 0 || nh + i >= H || nw + j < 0 || nw + j >= W)
          continue;
        if (ban.at(nh + i).at(nw + j) == '#')
          continue;
        if (seen.at(nh + i).at(nw + j))
          continue;
        Q.push_front(make_pair(cost, make_pair(nh + i, nw + j)));
        seen.at(nh + i).at(nw + j) = true;
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (nh + i < 0 || nh + i >= H || nw + j < 0 || nw + j >= W)
          continue;
        if (ban.at(nh + i).at(nw + j) == '#')
          continue;
        if (seen.at(nh + i).at(nw + j))
          continue;
        Q.push_back(make_pair(cost + 1, make_pair(nh + i, nw + j)));
        // seen.at(nh+i).at(nw+j) = true;
      }
    }
  }
  if (ans == inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
const int MOD = 1000000007;
typedef pair<long long, long long> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int H, W;
  cin >> H >> W;
  int wh, ww;
  cin >> wh >> ww;
  wh--;
  ww--;
  int gh, gw;
  cin >> gh >> gw;
  gh--;
  gw--;
  vector<vector<char>> ban(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> ban.at(i).at(j);
    }
  }
  deque<pair<int, pair<int, int>>> Q;
  Q.push_front(make_pair(0, make_pair(wh, ww)));
  vector<vector<bool>> seen(H, vector<bool>(W));
  int ans = inf;
  vector<vector<int>> mini(H, vector<int>(W));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      mini.at(i).at(j) = inf;
  while (!Q.empty()) {
    pair<int, pair<int, int>> p = Q.front();
    Q.pop_front();
    int nh = p.second.first, nw = p.second.second, cost = p.first;
    if (nh == gh && nw == gw) {
      ans = min(ans, cost);
    }
    mini.at(nh).at(nw) = min(mini.at(nh).at(nw), cost);
    cost = min(cost, mini.at(nh).at(nw));
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (i == j)
          continue;
        if (nh + i < 0 || nh + i >= H || nw + j < 0 || nw + j >= W)
          continue;
        if (ban.at(nh + i).at(nw + j) == '#')
          continue;
        if (seen.at(nh + i).at(nw + j))
          continue;
        Q.push_front(make_pair(cost, make_pair(nh + i, nw + j)));
        seen.at(nh + i).at(nw + j) = true;
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (nh + i < 0 || nh + i >= H || nw + j < 0 || nw + j >= W)
          continue;
        if (ban.at(nh + i).at(nw + j) == '#')
          continue;
        if (seen.at(nh + i).at(nw + j))
          continue;
        Q.push_back(make_pair(cost + 1, make_pair(nh + i, nw + j)));
        // seen.at(nh+i).at(nw+j) = true;
      }
    }
  }
  if (ans == inf) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}