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

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const ll INF = (1LL << 50);
const int N = 1005;

int n, m;
int see[N][N];
char v[N][N];
int dz[] = {0, -1, -2, 1, 2};

bool valid(int x, int y) {
  return x >= 0 and x < n and y >= 0 and y < m and v[x][y] == '.';
}

int main() {
  fastio;
  cin >> n >> m;
  ii cord[2];
  for (int i = 0; i < 2; i++)
    cin >> cord[i].fst >> cord[i].snd;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> v[i][j];
      see[i][j] = mod;
    }
  }
  deque<ii> q;
  q.push_front({cord[0].fst - 1, cord[0].snd - 1});
  see[cord[0].fst - 1][cord[0].snd - 1] = 0;
  while (!q.empty()) {
    int x = q.front().fst, y = q.front().snd;
    q.pop_front();
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        int newx = x + dz[i], newy = y + dz[j];
        if (!valid(newx, newy))
          continue;
        bool adj = ((abs(dz[i]) + abs(dz[j])) <= 1);
        if (adj and see[newx][newy] > see[x][y]) {
          see[newx][newy] = see[x][y];
          q.push_front({newx, newy});
        }
        if (!adj and see[newx][newy] > see[x][y] + 1) {
          see[newx][newy] = see[x][y] + 1;
          q.push_back({newx, newy});
        }
      }
    }
  }
  int ans = see[cord[1].fst - 1][cord[1].snd - 1];
  cout << (ans >= mod ? -1 : ans) << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define endl '\\n'
#define snd second
#define fst first
#define fastio cin.tie(NULL), cout.sync_with_stdio(true)

typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

const int mod = 1e9 + 7;
const ll INF = (1LL << 50);
const int N = 1005;

int n, m;
int see[N][N];
char v[N][N];
int dz[] = {0, -1, -2, 1, 2};

bool valid(int x, int y) {
  return x >= 0 and x < n and y >= 0 and y < m and v[x][y] == '.';
}

int main() {
  fastio;
  cin >> n >> m;
  ii cord[2];
  for (int i = 0; i < 2; i++)
    cin >> cord[i].fst >> cord[i].snd;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> v[i][j];
      see[i][j] = mod;
    }
  }
  deque<ii> q;
  q.push_front({cord[0].fst - 1, cord[0].snd - 1});
  see[cord[0].fst - 1][cord[0].snd - 1] = 0;
  while (!q.empty()) {
    int x = q.front().fst, y = q.front().snd;
    q.pop_front();
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        int newx = x + dz[i], newy = y + dz[j];
        if (!valid(newx, newy) or see[newx][newy] <= see[x][y])
          continue;
        int adj = ((abs(dz[i]) + abs(dz[j])) <= 1);
        see[newx][newy] = see[x][y] + !(adj);
        q.push_front({newx, newy});
      }
    }
  }
  int ans = see[cord[1].fst - 1][cord[1].snd - 1];
  cout << (ans >= mod ? -1 : ans) << '\\n';
  return 0;
}