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

#include <bits/stdc++.h>
using namespace std;
const int nax = 1e3 + 3;
using pi = pair<int, int>;
#define x first
#define y second

pair<int, int> operator+(const pi &a, const pi &b) {
  return make_pair(a.x + b.x, a.y + b.y);
}
// 0-1 BFS is Dijkstra.
bool bor[nax][nax];
int lvl[nax][nax];
int n, m;
bool safe(int i, int j) {
  return !(i < 0 or j < 0 or i > n - 1 or j > m - 1 or !bor[i][j]);
}
vector<pair<int, int>> pts(int i, int j) {
  vector<pi> ret;
  for (int x = i - 2; x <= i + 2; x++) {
    for (int y = j - 2; y <= j + 2; y++) {
      if (!safe(x, y))
        continue;
      ret.emplace_back(x, y);
    }
  }
  return ret;
}

void _read() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
}

int main() {
  _read();
  cin >> n >> m;

  pi src, des;
  cin >> src.x >> src.y >> des.x >> des.y;
  src.x--, src.y--, des.x--, des.y--;

  for (int i = 0; i < n; i++) {
    char c;
    for (int j = 0; j < m; j++) {
      cin >> c;
      bor[i][j] = (c == '.');
      lvl[i][j] = (int)1e9 + 7;
    }
  }

  deque<pair<int, int>> q;
  q.push_back(src);
  lvl[src.x][src.y] = 0;
  while (!q.empty()) {
    pi u = q.front();
    q.pop_front();
    for (pi dir : (vector<pi>){{1, 0}, {0, 1}, {-1, 0}, {0, -1}}) {
      pi v = u + dir;
      if (!safe(v.x, v.y))
        continue;
      int x = v.x, y = v.y;
      if (lvl[x][y] > lvl[u.x][u.y]) {
        lvl[x][y] = lvl[u.x][u.y];
        q.push_front(v);
      }
    }
    vector<pi> r = pts(u.x, u.y);
    for (pi v : r) {
      if (lvl[u.x][u.y] + 1 < lvl[v.x][v.y]) {
        lvl[v.x][v.y] = lvl[u.x][u.y] + 1;
        q.push_back(v);
      }
    }
  }
  int ans = lvl[des.x][des.y];
  cout << (ans == (int)1e9 + 7 ? -1 : ans) << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int nax = 1e3 + 3;
using pi = pair<int, int>;
#define x first
#define y second

pair<int, int> operator+(const pi &a, const pi &b) {
  return make_pair(a.x + b.x, a.y + b.y);
}
// 0-1 BFS is Dijkstra.
bool bor[nax][nax];
int lvl[nax][nax];
int n, m;
bool safe(int i, int j) {
  return !(i < 0 or j < 0 or i > n - 1 or j > m - 1 or !bor[i][j]);
}
vector<pair<int, int>> pts(int i, int j) {
  vector<pi> ret;
  for (int x = i - 2; x <= i + 2; x++) {
    for (int y = j - 2; y <= j + 2; y++) {
      if (!safe(x, y))
        continue;
      ret.emplace_back(x, y);
    }
  }
  return ret;
}

void _read() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
}

int main() {
  _read();
  cin >> n >> m;

  pi src, des;
  cin >> src.x >> src.y >> des.x >> des.y;
  src.x--, src.y--, des.x--, des.y--;

  for (int i = 0; i < n; i++) {
    char c;
    for (int j = 0; j < m; j++) {
      cin >> c;
      bor[i][j] = (c == '.');
      lvl[i][j] = (int)1e9 + 7;
    }
  }

  deque<pair<int, int>> q;
  q.push_back(src);
  lvl[src.x][src.y] = 0;
  while (!q.empty()) {
    pi u = q.front();
    q.pop_front();
    for (pi dir : (vector<pi>){{1, 0}, {0, 1}, {-1, 0}, {0, -1}}) {
      pi v = u + dir;
      if (!safe(v.x, v.y))
        continue;
      int x = v.x, y = v.y;
      if (lvl[x][y] > lvl[u.x][u.y]) {
        lvl[x][y] = lvl[u.x][u.y];
        q.push_back(v);
      }
    }
    vector<pi> r = pts(u.x, u.y);
    for (pi v : r) {
      if (lvl[u.x][u.y] + 1 < lvl[v.x][v.y]) {
        lvl[v.x][v.y] = lvl[u.x][u.y] + 1;
        q.push_back(v);
      }
    }
  }
  int ans = lvl[des.x][des.y];
  cout << (ans == (int)1e9 + 7 ? -1 : ans) << '\\n';
  return 0;
}