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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

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

const int LIT = 1e9 + 7;
int n, m;
char arr[1001][1001];
int bfs_visited[1001][1001];

struct pos {
  int x;
  int y;
  int cnt;
  bool operator<(pos a) const { return cnt > a.cnt; }
};

void solve() {
  int h, w;
  pair<int, int> start, end;
  cin >> h >> w >> start.first >> start.second >> end.first >> end.second;

  for (int i = 1; i <= h; i++) {
    string x;
    cin >> x;
    for (int j = 1; j <= w; j++) {
      arr[i][j] = x[j - 1];
      bfs_visited[i][j] = LIT;
    }
  }

  priority_queue<pos> q;
  q.push({start.second, start.first, 0});
  bfs_visited[start.first][start.second] = 0;
  while (!q.empty()) {
    pos cur = q.top();
    q.pop();

    for (int i = 0; i < 4; i++) {
      int nx = cur.x + dx[i];
      int ny = cur.y + dy[i];

      if (nx < 1 || ny < 1 || nx > w || ny > h || arr[ny][nx] == '#')
        continue;
      if (bfs_visited[ny][nx] > bfs_visited[cur.y][cur.x]) {
        bfs_visited[ny][nx] = bfs_visited[cur.y][cur.x];
        q.push({nx, ny, cur.cnt});
      }
    }

    for (int j = -2; j <= 2; j++) {
      for (int i = -2; i <= 2; i++) {
        if (i == 0 && j == 0)
          continue;
        int nx = cur.x + i;
        int ny = cur.y + j;

        if (nx < 1 || ny < 1 || nx > w || ny > h || arr[ny][nx] == '#')
          continue;
        if (bfs_visited[ny][nx] > bfs_visited[cur.y][cur.x] + 1) {
          bfs_visited[ny][nx] = bfs_visited[cur.y][cur.x] + 1;
          q.push({nx, ny, cur.cnt + 1});
        }
      }
    }
  }

  if (bfs_visited[end.first][end.second] == LIT)
    cout << -1;
  else
    cout << bfs_visited[end.first][end.second];
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

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

const int LIT = 1e9 + 7;
int n, m;
char arr[1001][1001];
int bfs_visited[1001][1001];

struct pos {
  int x;
  int y;
  int cnt;
};

void solve() {
  int h, w;
  pair<int, int> start, end;
  cin >> h >> w >> start.first >> start.second >> end.first >> end.second;

  for (int i = 1; i <= h; i++) {
    string x;
    cin >> x;
    for (int j = 1; j <= w; j++) {
      arr[i][j] = x[j - 1];
      bfs_visited[i][j] = LIT;
    }
  }

  queue<pos> q;
  q.push({start.second, start.first, 0});
  bfs_visited[start.first][start.second] = 0;
  while (!q.empty()) {
    pos cur = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      int nx = cur.x + dx[i];
      int ny = cur.y + dy[i];

      if (nx < 1 || ny < 1 || nx > w || ny > h || arr[ny][nx] == '#')
        continue;
      if (bfs_visited[ny][nx] > bfs_visited[cur.y][cur.x]) {
        bfs_visited[ny][nx] = bfs_visited[cur.y][cur.x];
        q.push({nx, ny, cur.cnt});
      }
    }

    for (int j = -2; j <= 2; j++) {
      for (int i = -2; i <= 2; i++) {
        if (i == 0 && j == 0)
          continue;
        int nx = cur.x + i;
        int ny = cur.y + j;

        if (nx < 1 || ny < 1 || nx > w || ny > h || arr[ny][nx] == '#')
          continue;
        if (bfs_visited[ny][nx] > bfs_visited[cur.y][cur.x] + 1) {
          bfs_visited[ny][nx] = bfs_visited[cur.y][cur.x] + 1;
          q.push({nx, ny, cur.cnt + 1});
        }
      }
    }
  }

  if (bfs_visited[end.first][end.second] == LIT)
    cout << -1;
  else
    cout << bfs_visited[end.first][end.second];
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}