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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

#define pb push_back
#define ff first
#define ss second
#define INF int(2e9) + 9
#define BIG_INF ll(9e18)
#define SIZE 1001
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define len(v) int((v).size())
#define BACK(v) ll((v).back())
#define sqr(x) (x) * (x)
#define cube(x) (x) * (x) * (x)
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout);

ll binpow(ll a, ll b, ll MOD = BIG_INF) {
  ll res = 1;

  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
    a %= MOD;
    res %= MOD;
  }

  return res % MOD;
}

const ll mod = 1e9 + 7;

int T = 1;
int n, m;
char a[SIZE][SIZE];

int ans[SIZE][SIZE];

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

struct comp {
  bool operator()(const pair<pii, int> &x, const pair<pii, int> &y) {
    return x.ss > y.ss;
  }
};

void solve() {
  int sx, sy, ex, ey;
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      ans[i][j] = INF;
  sx--;
  sy--;
  ex--;
  ey--;

  priority_queue<pair<pii, int>, vector<pair<pii, int>>, comp> q;
  q.push({{sx, sy}, 0});

  while (q.size()) {
    pair<pii, int> cur = q.top();
    q.pop();
    int x = cur.ff.ff, y = cur.ff.ss;
    int d = cur.ss;
    if (d >= ans[x][y])
      continue;

    ans[x][y] = d;

    for (int i = 0; i < 4; i++) {
      int curx = x + dx[i], cury = y + dy[i];

      if (curx < 0 || curx >= n || cury < 0 || cury >= m ||
          a[curx][cury] == '#')
        continue;

      int curans = 0;
      curans += ans[x][y];

      if (curans < ans[curx][cury]) {
        q.push({{curx, cury}, curans});
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int curx = x + i, cury = y + j;
        if (curx < 0 || curx >= n || cury < 0 || cury >= m ||
            a[curx][cury] == '#')
          continue;
        int curans = 1;
        curans += ans[x][y];

        if (curans < ans[curx][cury]) {
          q.push({{curx, cury}, curans});
        }
      }
    }
  }
  //    for(int i = 0; i < n; i++)
  //    {
  //        for(int j = 0; j < m; j++)
  //            cout << ans[i][j] << ' ';
  //        cout << "\\n";
  //    }

  if (ans[ex][ey] == INF)
    ans[ex][ey] = -1;
  cout << ans[ex][ey];
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //    filein;fileout;
  //
  //    cin >> T;

  while (T--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

#define pb push_back
#define ff first
#define ss second
#define INF int(2e9) + 9
#define BIG_INF ll(9e18)
#define SIZE 1001
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define len(v) int((v).size())
#define BACK(v) ll((v).back())
#define sqr(x) (x) * (x)
#define cube(x) (x) * (x) * (x)
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout);

ll binpow(ll a, ll b, ll MOD = BIG_INF) {
  ll res = 1;

  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
    a %= MOD;
    res %= MOD;
  }

  return res % MOD;
}

const ll mod = 1e9 + 7;

int T = 1;
int n, m;
char a[SIZE][SIZE];

int ans[SIZE][SIZE];

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void solve() {
  int sx, sy, ex, ey;
  cin >> n >> m >> sx >> sy >> ex >> ey;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      ans[i][j] = INF;
  sx--;
  sy--;
  ex--;
  ey--;

  queue<pair<pii, int>> q;
  q.push({{sx, sy}, 0});

  //
  while (q.size()) {
    pair<pii, int> cur = q.front();
    q.pop();
    int x = cur.ff.ff, y = cur.ff.ss;
    int d = cur.ss;
    if (d >= ans[x][y])
      continue;

    ans[x][y] = d;

    for (int i = 0; i < 4; i++) {
      int curx = x + dx[i], cury = y + dy[i];

      if (curx < 0 || curx >= n || cury < 0 || cury >= m ||
          a[curx][cury] == '#')
        continue;

      int curans = 0;
      curans += ans[x][y];

      if (curans < ans[curx][cury]) {
        q.push({{curx, cury}, curans});
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int curx = x + i, cury = y + j;
        if (curx < 0 || curx >= n || cury < 0 || cury >= m ||
            a[curx][cury] == '#')
          continue;
        int curans = 1;
        curans += ans[x][y];

        if (curans < ans[curx][cury]) {
          q.push({{curx, cury}, curans});
        }
      }
    }
  }
  //    for(int i = 0; i < n; i++)
  //    {
  //        for(int j = 0; j < m; j++)
  //            cout << ans[i][j] << ' ';
  //        cout << "\\n";
  //    }

  if (ans[ex][ey] == INF)
    ans[ex][ey] = -1;
  cout << ans[ex][ey];
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //    filein;fileout;
  //
  //    cin >> T;

  while (T--) {
    solve();
  }
  return 0;
}