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

// g++ t.cpp -o t && t < p.txt
// d>p.txt&&t<p.txt&&t2<p.txt

#include <algorithm> // みんな大好きソートと二分探索
#include <bitset>
#include <ctime>
#include <functional> // 関数を変数に入れる子
#include <iostream>   // プログラムの耳と口
#include <map>        // setの妹 これまた優秀
#include <queue>      // きゅーちゃん、だいすき
#include <random>     // 乱択さん
#include <set>        // 値の取得・挿入・削除を高速に
#include <string>     // to_string
#include <vector>     // 超有能配列秘書

#define DB cerr << "D" << endl
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; };
using P = pair<ll, ll>;
const ll M = 1e9 + 7;
ll mod_pow(ll x, ll a) {
  ll an = 1;
  while (a > 0) {
    if (a & 1)
      an = an * x % M;
    x = x * x % M;
    a >>= 1;
  }
  return an;
}
void add(ll &x, ll y) {
  x += y;
  x %= M;
};
void mul(ll &x, ll y) {
  x *= y;
  x %= M;
};
template <typename T, typename U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
};
template <typename T, typename U> void chmin(T &x, U y) {
  if (x > y)
    x = y;
}
bool vaild(int x, int y, int hh, int ww) {
  return 0 <= x && x < hh && 0 <= y && y < ww;
}
int keta(ll a) {
  int res = 0;
  while (a > 0)
    res += a % 10, a /= 10;
  return res;
}
const int up[] = {1, -1, 0, 0}, lf[] = {0, 0, 1, -1};

struct tup {
  int x, y, c;
  bool operator<(const tup tt) const {
    if (c != tt.c)
      return c < tt.c;
    else if (x != tt.x)
      return x < tt.x;
    else
      return y < tt.y;
  }
  bool operator>(const tup tt) const {
    if (c != tt.c)
      return c > tt.c;
    else if (x != tt.x)
      return x > tt.x;
    else
      return y > tt.y;
  }
};

int main() {
  int h, w;
  cin >> h >> w;
  int x0, y0, x1, y1;
  cin >> x0 >> y0 >> x1 >> y1;
  x0--;
  y0--;
  x1--;
  y1--;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int cs[h][w];
  fill(cs[0], cs[h], INF);
  cs[x0][y0] = 0;
  priority_queue<tup, vector<tup>, greater<tup>> que;
  que.push({x0, y0, 0});
  while (!que.empty()) {
    tup tp = que.top();
    que.pop();
    int x = tp.x;
    int y = tp.y;
    int cc = tp.c;
    if (tp.c > cs[x][y])
      continue;
    for (int i = 0; i < 4; i++) {
      int tx = x + up[i], ty = y + lf[i];
      if (vaild(tx, ty, h, w) && s[tx][ty] == '.' && cc < cs[tx][ty]) {
        // cout<<tx<<" "<<ty<<" "<<cc<<endl;
        cs[tx][ty] = cc;
        que.push({tx, ty, cc});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (abs(i) + abs(j) > 5)
          continue;
        int tx = x + i, ty = y + j;
        if (vaild(tx, ty, h, w) && s[tx][ty] == '.' && cc + 1 < cs[tx][ty]) {
          cs[tx][ty] = cc + 1;
          que.push({tx, ty, cc + 1});
        }
      }
    }
  }

  if (cs[x1][y1] == INF) {
    cout << -1 << endl;
  } else {
    cout << cs[x1][y1] << endl;
  }
}
// ・配列の大きさok？　・priority_queueはgreater？　・debug消した？
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……

// Thank you for making problems and running the contest


Error: TLE

Fixed submission:

// g++ t.cpp -o t && t < p.txt
// d>p.txt&&t<p.txt&&t2<p.txt

#include <algorithm> // みんな大好きソートと二分探索
#include <bitset>
#include <ctime>
#include <functional> // 関数を変数に入れる子
#include <iostream>   // プログラムの耳と口
#include <map>        // setの妹 これまた優秀
#include <queue>      // きゅーちゃん、だいすき
#include <random>     // 乱択さん
#include <set>        // 値の取得・挿入・削除を高速に
#include <string>     // to_string
#include <vector>     // 超有能配列秘書

#define DB cerr << "D" << endl
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; };
using P = pair<ll, ll>;
const ll M = 1e9 + 7;
ll mod_pow(ll x, ll a) {
  ll an = 1;
  while (a > 0) {
    if (a & 1)
      an = an * x % M;
    x = x * x % M;
    a >>= 1;
  }
  return an;
}
void add(ll &x, ll y) {
  x += y;
  x %= M;
};
void mul(ll &x, ll y) {
  x *= y;
  x %= M;
};
template <typename T, typename U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
};
template <typename T, typename U> void chmin(T &x, U y) {
  if (x > y)
    x = y;
}
bool vaild(int x, int y, int hh, int ww) {
  return 0 <= x && x < hh && 0 <= y && y < ww;
}
int keta(ll a) {
  int res = 0;
  while (a > 0)
    res += a % 10, a /= 10;
  return res;
}
const int up[] = {1, -1, 0, 0}, lf[] = {0, 0, 1, -1};

struct tup {
  int x, y, c;
  bool operator<(const tup tt) const {
    if (c != tt.c)
      return c < tt.c;
    else if (x != tt.x)
      return x < tt.x;
    else
      return y < tt.y;
  }
  bool operator>(const tup tt) const {
    if (c != tt.c)
      return c > tt.c;
    else if (x != tt.x)
      return x > tt.x;
    else
      return y > tt.y;
  }
};

int main() {
  int h, w;
  cin >> h >> w;
  int x0, y0, x1, y1;
  cin >> x0 >> y0 >> x1 >> y1;
  x0--;
  y0--;
  x1--;
  y1--;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int cs[h][w];
  fill(cs[0], cs[h], INF);
  cs[x0][y0] = 0;
  queue<tup> que;
  que.push({x0, y0, 0});
  while (!que.empty()) {
    tup tp = que.front();
    que.pop();
    int x = tp.x;
    int y = tp.y;
    int cc = tp.c;
    if (tp.c > cs[x][y])
      continue;
    for (int i = 0; i < 4; i++) {
      int tx = x + up[i], ty = y + lf[i];
      if (vaild(tx, ty, h, w) && s[tx][ty] == '.' && cc < cs[tx][ty]) {
        // cout<<tx<<" "<<ty<<" "<<cc<<endl;
        cs[tx][ty] = cc;
        que.push({tx, ty, cc});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (abs(i) + abs(j) > 5)
          continue;
        int tx = x + i, ty = y + j;
        if (vaild(tx, ty, h, w) && s[tx][ty] == '.' && cc + 1 < cs[tx][ty]) {
          cs[tx][ty] = cc + 1;
          que.push({tx, ty, cc + 1});
        }
      }
    }
  }

  if (cs[x1][y1] == INF) {
    cout << -1 << endl;
  } else {
    cout << cs[x1][y1] << endl;
  }
}
// ・配列の大きさok？　・priority_queueはgreater？　・debug消した？
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……

// Thank you for making problems and running the contest