Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define UP(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define UU(a, b, c) for (ll(a) = (b); (a) <= (c); (a)++)
#define DN(a, b, c) for (ll(a) = (b); (a) > (c); (a)--)
#define DU(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)

int r, c, sx, sy, ex, ey;
char grid[3005][3005];

void reset() {}

void input() {
  cin >> r >> c;
  cin >> sy >> sx;
  cin >> ey >> ex;
  UU(j, 1, r) {
    UU(i, 1, c) { cin >> grid[j][i]; }
  }
}

int mx[] = {1, -1, 0, 0};
int my[] = {0, 0, 1, -1};

bool vis[3005][3005] = {0};
int djikstra() {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {sx, sy}});

  while (!pq.empty()) {
    int cost = pq.top().fi, x = pq.top().se.fi, y = pq.top().se.se;
    pq.pop();
    vis[y][x] = 1;
    if (y == ey && x == ex) {
      return cost;
    }
    UU(j, -2, 2) {
      UU(i, -2, 2) {
        int tx = x + i, ty = y + j;
        if (tx < 0 || tx > c || ty < 0 || ty > r) {
          continue;
        } else if (vis[ty][tx] == 0 && grid[ty][tx] == '.') {
          if (abs(j) + abs(i) == 1) {
            pq.push({cost, {tx, ty}});
          } else {
            pq.push({cost + 1, {tx, ty}});
          }
        }
      }
    }
  }
  return -1;
}

void solve() { cout << djikstra() << endl; }

void LetsRock() { solve(); }

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

  input();
  reset();
  LetsRock();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define UP(a, b, c) for (ll(a) = (b); (a) < (c); (a)++)
#define UU(a, b, c) for (ll(a) = (b); (a) <= (c); (a)++)
#define DN(a, b, c) for (ll(a) = (b); (a) > (c); (a)--)
#define DU(a, b, c) for (ll(a) = (b); (a) >= (c); (a)--)

int r, c, sx, sy, ex, ey;
char grid[3005][3005];

void reset() {}

void input() {
  cin >> r >> c;
  cin >> sy >> sx;
  cin >> ey >> ex;
  UU(j, 1, r) {
    UU(i, 1, c) { cin >> grid[j][i]; }
  }
}

int mx[] = {1, -1, 0, 0};
int my[] = {0, 0, 1, -1};

bool vis[3005][3005] = {0};
int djikstra() {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {sx, sy}});

  while (!pq.empty()) {
    int cost = pq.top().fi, x = pq.top().se.fi, y = pq.top().se.se;
    pq.pop();
    if (vis[y][x] == 0) {
      vis[y][x] = 1;
      if (y == ey && x == ex) {
        return cost;
      }
      UU(j, -2, 2) {
        UU(i, -2, 2) {
          int tx = x + i, ty = y + j;
          if (tx < 0 || tx > c || ty < 0 || ty > r) {
            continue;
          } else if (vis[ty][tx] == 0 && grid[ty][tx] == '.') {
            if (abs(j) + abs(i) == 1) {
              pq.push({cost, {tx, ty}});
            } else {
              pq.push({cost + 1, {tx, ty}});
            }
          }
        }
      }
    }
  }
  return -1;
}

void solve() { cout << djikstra() << endl; }

void LetsRock() { solve(); }

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

  input();
  reset();
  LetsRock();

  return 0;
}