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

// 　　＿＿
// 　／　<＠ﾌ
//  ｜(ﾉﾉハ))
//  ﾉ从ﾟヮﾟ从
// 　ﾉ｜ｿﾉГ|つ author:hotarunx
// 〈_ﾉ^^^ヽ|
// 　~~tｧtｧ~
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define int long long
constexpr int INF = 1000000000 + 8;
constexpr array<int, 4> DY = {0, 1, 0, -1}, DX = {1, 0, -1, 0};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--, cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--, dw--;

  vector<string> grid(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }

  vector<vector<int>> cost(h, vector<int>(w, INF));
  cost[ch][cw] = 0;

  deque<pair<int, int>> q;
  q.push_back({ch, cw});

  while (!q.empty()) {
    const int y = q.front().first;
    const int x = q.front().second;
    q.pop_front();

    for (int dy = -2; dy <= 2; dy++) {
      for (int dx = -2; dx <= 2; dx++) {
        const int ny = y + dy, nx = x + dx;
        int ncost = cost[y][x];
        if (abs(dy) + abs(dx) > 1)
          ncost++;

        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (grid[ny][nx] == '#')
          continue;
        if (ncost >= cost[ny][nx])
          continue;

        if (abs(dy) + abs(dx) > 1)
          q.push_back({ny, nx});
        else
          q.push_front({ny, nx});
        cost[ny][nx] = ncost;
      }
    }
  }

  const int ans = (cost[dh][dw] != INF ? cost[dh][dw] : -1);
  cout << ans << endl;
}


Error: TLE

Fixed submission:

// 　　＿＿
// 　／　<＠ﾌ
//  ｜(ﾉﾉハ))
//  ﾉ从ﾟヮﾟ从
// 　ﾉ｜ｿﾉГ|つ author:hotarunx
// 〈_ﾉ^^^ヽ|
// 　~~tｧtｧ~
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define int long long
constexpr int INF = 1000000000 + 8;
constexpr array<int, 4> DY = {0, 1, 0, -1}, DX = {1, 0, -1, 0};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--, cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--, dw--;

  vector<string> grid(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }

  vector<vector<int>> cost(h, vector<int>(w, INF));
  cost[ch][cw] = 0;

  queue<pair<int, int>> q;
  q.push({ch, cw});

  while (!q.empty()) {
    const int y = q.front().first;
    const int x = q.front().second;
    q.pop();

    for (int i = 0; i < 4; i++) {
      const int ny = y + DY[i], nx = x + DX[i];
      int ncost = cost[y][x];

      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (grid[ny][nx] == '#')
        continue;
      if (ncost >= cost[ny][nx])
        continue;

      q.push({ny, nx});
      cost[ny][nx] = ncost;
    }

    for (int dy = -2; dy <= 2; dy++) {
      for (int dx = -2; dx <= 2; dx++) {
        const int ny = y + dy, nx = x + dx;
        int ncost = cost[y][x] + 1;
        if (abs(dy) + abs(dx) <= 1)
          continue;

        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (grid[ny][nx] == '#')
          continue;
        if (ncost >= cost[ny][nx])
          continue;

        q.push({ny, nx});
        cost[ny][nx] = ncost;
      }
    }
  }

  const int ans = (cost[dh][dw] != INF ? cost[dh][dw] : -1);
  cout << ans << endl;
}