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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i, a, b) for (ll i = (a); i < (b); i++)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); i--)
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<int, int>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define rsrt(a) sort(a.begin(), a.end(), greater<int>())
#define mem(a, n) memset(a, n, sizeof a)
#define ln(a) a.length()
#define sz(a) (ll) a.size()
#define endl "\\n"
#define pi 3.14159265358979

const ll M = 1e9 + 7;
const int P = 31;
const int MaxM = 200005;
/*__________________________________________________________________________________________________________________*/
int n, m;
int x1, y11, x2, y2;
char s[1005][1005];
int vis[1005][1005];
int st[1000005];
vector<pair<int, int>> v[1000005];
struct trip {
  int a;
  int b;
  int c;
};
void dfs(int i, int j, int k) {
  if (vis[i][j] != 0)
    return;
  vis[i][j] = k;
  v[k].pb({i, j});
  if (i + 1 < n && s[i + 1][j] == '.')
    dfs(i + 1, j, k);
  if (i - 1 >= 0 && s[i - 1][j] == '.')
    dfs(i - 1, j, k);
  if (j - 1 >= 0 && s[i][j - 1] == '.')
    dfs(i, j - 1, k);
  if (j + 1 < m && s[i][j + 1] == '.')
    dfs(i, j + 1, k);

  return;
}

void bfs(int i, int j, int val) {
  queue<trip> q;
  q.push({i, j, val});
  while (!q.empty()) {
    trip tmp = q.front();
    q.pop();
    int p = vis[tmp.a][tmp.b];
    if (st[p] != -1)
      continue;
    st[p] = tmp.c;
    for (auto it : v[p]) {
      int x = it.F, y = it.S;
      for (int i1 = x - 2; i1 <= x + 2; i1++) {
        for (int j1 = y - 2; j1 <= y + 2; j1++) {
          if (i1 >= 0 && j1 >= 0 && i1 < n && j1 < m && s[i1][j1] == '.' &&
              st[vis[i1][j1]] == -1)
            q.push({i1, j1, tmp.c + 1});
        }
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n >> m;
  cin >> x1 >> y11;
  x1--;
  y11--;
  cin >> x2 >> y2;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
    }
  }

  memset(vis, 0, sizeof(vis));
  memset(st, -1, sizeof(st));
  // f(i,0,1000005)v[i].clear();

  int k = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.' && vis[i][j] == 0) {
        k++;
        dfs(i, j, k);
      }
    }
  }

  bfs(x1, y11, 0);
  cout << st[vis[x2][y2]] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i, a, b) for (ll i = (a); i < (b); i++)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); i--)
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<int, int>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define rsrt(a) sort(a.begin(), a.end(), greater<int>())
#define mem(a, n) memset(a, n, sizeof a)
#define ln(a) a.length()
#define sz(a) (ll) a.size()
#define endl "\\n"
#define pi 3.14159265358979

const ll M = 1e9 + 7;
const int P = 31;
const int MaxM = 200005;
/*__________________________________________________________________________________________________________________*/
int n, m;
int x1, y11, x2, y2;
char s[1005][1005];
int vis[1005][1005];
map<int, int> mp;
vector<pair<int, int>> v[1000005];
struct trip {
  int a;
  int b;
  int c;
};
void dfs(int i, int j, int k) {
  if (vis[i][j] != 0)
    return;
  vis[i][j] = k;
  v[k].pb({i, j});
  if (i + 1 < n && s[i + 1][j] == '.')
    dfs(i + 1, j, k);
  if (i - 1 >= 0 && s[i - 1][j] == '.')
    dfs(i - 1, j, k);
  if (j - 1 >= 0 && s[i][j - 1] == '.')
    dfs(i, j - 1, k);
  if (j + 1 < m && s[i][j + 1] == '.')
    dfs(i, j + 1, k);

  return;
}

void bfs(int i, int j, int val) {
  queue<trip> q;
  set<int> st;
  q.push({i, j, val});
  while (!q.empty()) {
    trip tmp = q.front();
    q.pop();
    int p = vis[tmp.a][tmp.b];
    st.insert(p);
    mp[p] = tmp.c;
    for (auto it : v[p]) {
      int x = it.F, y = it.S;
      for (int i1 = x - 2; i1 <= x + 2; i1++) {
        for (int j1 = y - 2; j1 <= y + 2; j1++) {
          if (i1 >= 0 && j1 >= 0 && i1 < n && j1 < m && s[i1][j1] == '.' &&
              st.count(vis[i1][j1]) == 0)
            q.push({i1, j1, tmp.c + 1});
        }
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> m;
  cin >> x1 >> y11;
  x1--;
  y11--;
  cin >> x2 >> y2;
  x2--;
  y2--;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
    }
  }

  memset(vis, 0, sizeof(vis));
  mp.clear();
  f(i, 0, 1000005) v[i].clear();

  int k = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '.' && vis[i][j] == 0) {
        k++;
        dfs(i, j, k);
      }
    }
  }

  bfs(x1, y11, 0);
  if (mp.find(vis[x2][y2]) == mp.end())
    cout << -1 << endl;
  else
    cout << mp[vis[x2][y2]] << endl;
}