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

// #pragma optimization_level 3
#include <bits/stdc++.h>
#define f first
#define s second
#define sym(s) s = "#" + s + "#";
#define all(x) (x).begin(), (x).end()
#define alll(x, n) x + 1, x + n + 1
#define newl cout << "\\n";
#define foo(i, a, n) for (ll i = (a); i <= n; i++)
#define deb1(a) cout << a << "\\n";
#define deb2(a, b) cout << a << " " << b << "\\n";
#define deb3(a, b, c) cout << a << " " << b << " " << c << "\\n";
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\\n";
#define debp(a) cout << a.f << " " << a.s << "\\n";
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " ";                                                         \\
  newl;
#define debm(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << "{" << it.f << "," << it.s << "}, ";                               \\
  newl;
#define deb1d(a, n)                                                            \\
  foo(i, 1, n) cout << a[i] << " ";                                            \\
  newl;
#define deb2d(a, n, m)                                                         \\
  foo(i, 1, n) {                                                               \\
    foo(j, 1, m) { cout << a[i][j] << " "; }                                   \\
    newl;                                                                      \\
  }

using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e+9 + 7;
const ll INF = LLONG_MAX;
const int N = (int)1e+3 + 8;

ll n, m, stx, sty, enx, eny;
string s[N];
ll a[N][N], vis[N * N];
ll grp;
set<ll> adj[N * N];
ll res = INF;

ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll dx25[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
             0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
ll dy25[] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
             1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};

void dfs(ll x, ll y) {
  a[x][y] = grp;
  for (int k = 0; k < 4; k++) {
    int dx = x + dx4[k];
    int dy = y + dy4[k];
    if (s[dx][dy] == '.' && a[dx][dy] == 0) {
      dfs(dx, dy);
    }
  }
}

void bfs() {
  queue<pair<int, int>> q;
  q.push({a[stx][sty], 0});
  vis[a[stx][sty]] = 1;
  while (q.size()) {
    pair<int, int> p = q.front();
    q.pop();
    ll v = p.f, d = p.s;
    if (v == a[enx][eny]) {
      res = d;
      return;
    }
    for (auto to : adj[v]) {
      if (vis[to] == 0) {
        vis[to] = 1;
        q.push({to, d + 1});
      }
    }
  }
}

void MAIN(int tc) {
  cin >> n >> m;
  cin >> stx >> sty;
  cin >> enx >> eny;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = "#" + s[i] + "#";
  }
  s[0] = s[n + 1] = string(m + 2, '#');
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 0 && s[i][j] == '.') {
        grp += 1;
        dfs(i, j);
      }
    }
  }

  for (int x = 1; x <= n; x++) {
    for (int y = 1; y <= m; y++) {
      if (s[x][y] == '#')
        continue;
      for (int k = 0; k < 25; k++) {
        int dx = x + dx25[k];
        int dy = y + dy25[k];
        if (dx >= 1 && dx <= n && dy >= 1 && dy <= m) {
          if (s[dx][dy] == '.' && a[x][y] != a[dx][dy]) {
            adj[a[x][y]].insert(a[dx][dy]);
            adj[a[dx][dy]].insert(a[x][y]);
          }
        }
      }
    }
  }

  // deb2d(a,n,m);
  bfs();

  if (res == INF)
    res = -1;
  deb1(res);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed;
  cout << setprecision(10);

  int test_cases = 1;
  // cin>>test_cases;
  for (int i = 1; i <= test_cases; i++) {
    MAIN(i);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()

using ll = long long;
using ld = long double;
using namespace std;

const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ll N = 2e3 + 5;

ll n, m, x, y, dx, dy, vis[N][N], d[N][N];
char a[N][N];

ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};

ll ans = inf;

ll valid(ll x, ll y) {
  if (x >= 0 and x < n and y >= 0 and y < m and a[x][y] == '.')
    return 1;
  else
    return 0;
}

struct cmp {
  bool operator()(const pair<pair<int, int>, int> &a,
                  const pair<pair<int, int>, int> &b) const {
    return a.second < b.second;
  }
};

void bfs(ll x, ll y) {
  priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>,
                 cmp>
      q;
  memset(d, -1, sizeof(d));

  q.push({{x, y}, 0});
  vis[x][y] = 1;
  d[x][y] = 0;

  while (!q.empty()) {
    pair<pair<ll, ll>, ll> p = q.top();
    q.pop();

    ll x1 = p.first.first, y1 = p.first.second;
    // cout << p.first.first << " " << p.first.second << " " << p.second <<
    // "\\n";

    if (p.first.first == dx and p.first.second == dy) {
      ans = min(ans, p.second);
    }

    for (ll k = 0; k < 4; k++) {
      ll i = x1 + dx4[k], j = y1 + dy4[k];
      if (valid(i, j) and (d[i][j] == -1 or d[i][j] > d[x1][y1])) {
        vis[i][j] = 1;
        d[i][j] = d[x1][y1];
        q.push({{i, j}, d[i][j]});
      }
    }

    for (ll k : {-2, -1, 0, 1, 2}) {
      for (ll l : {-2, -1, 0, 1, 2}) {
        ll i = x1 + k, j = y1 + l;
        if (valid(i, j) and (d[i][j] == -1 or d[i][j] > d[x1][y1] + 1)) {
          vis[i][j] = 1;
          d[i][j] = d[x1][y1] + 1;
          q.push({{i, j}, d[i][j]});
        }
      }
    }
  }
}

void code() {
  cin >> n >> m;
  cin >> x >> y >> dx >> dy;
  x--, y--, dx--, dy--;

  int f = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#')
        f = 1;
    }
  }

  if (!f) {
    cout << "0\\n";
    return;
  }
  bfs(x, y);

  cout << (ans == inf ? -1 : ans) << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll t = 1;
  // cin>>t;

  while (t-- > 0) {
    code();
  }
}