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

#include <bits/stdc++.h>
#define newl "\\n"
using namespace std;
using lli = long long int;
using vvint = vector<vector<int>>;
using pii = pair<int, int>;
const int mod = 1e9 + 7;

const int N = 2e5 + 1;

int h, w;
pair<int, int> s, d;
vector<string> a;
vector<vector<pair<int, int>>> adj;
vector<int> dist;

void bfs01(int src) {
  dist[src] = 0;
  queue<int> q0;
  queue<pair<int, int>> q1;
  vector<bool> vis(h * w, 0);
  q0.push(src);
  int count = 0;
  int count1 = 0;
  while (!q0.empty() or !q1.empty()) {
    // if(count > 785399) break;
    ++count;
    // cout << q0.size() << " " << q1.size() << newl;
    while (!q0.empty()) {
      // if(count >= 785394) cout << ++count1 << newl;
      int u = q0.front();
      q0.pop();
      if (vis[u])
        continue;
      vis[u] = 1;
      for (auto v : adj[u]) {
        if (!vis[v.first]) {
          int vv = v.first;
          int dd = v.second;
          if (dd == 1)
            q1.push({vv, dist[u] + 1});
          else {
            q0.push(vv);
            dist[vv] = dist[u];
          }
        }
      }
    }
    if (!q1.empty()) {
      auto u1 = q1.front();
      int u = u1.first;
      q1.pop();
      if (vis[u])
        continue;
      vis[u] = 1;
      dist[u] = u1.second;
      for (auto v : adj[u]) {
        if (!vis[v.first]) {
          int vv = v.first;
          int dd = v.second;
          if (dd == 1)
            q1.push({vv, dist[u] + 1});
          else {
            q0.push(vv);
            dist[vv] = dist[u];
          }
        }
      }
    }
  }
  return;
}

void solve() {
  //	for(int i = 0;i < 1000;++i){
  //		for(int j = 0;j < 1000;++j){
  //			int x = rand();
  //			if(x & 1) cout << "#";
  //			else cout << ".";
  //		}
  //		cout << newl;
  //	}
  cin >> h >> w;
  cin >> s.first >> s.second;
  cin >> d.first >> d.second;
  --s.first, --s.second, --d.first, --d.second;

  dist = vector<int>(h * w, -1);
  a = vector<string>(h);
  adj = vector<vector<pair<int, int>>>(h * w);

  for (int i = 0; i < h; ++i)
    cin >> a[i];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (a[i][j] != '#') {
        if (i - 1 >= 0 and a[i - 1][j] == '.')
          adj[i * w + j].push_back({(i - 1) * w + (j), 0});
        if (i + 1 < h and a[i + 1][j] == '.')
          adj[i * w + j].push_back({(i + 1) * w + (j), 0});
        if (j - 1 >= 0 and a[i][j - 1] == '.')
          adj[i * w + j].push_back({(i)*w + (j - 1), 0});
        if (j + 1 < w and a[i][j + 1] == '.')
          adj[i * w + j].push_back({(i)*w + (j + 1), 0});
        for (int i1 = -2; i1 <= 2; ++i1) {
          for (int j1 = -2; j1 <= 2; ++j1) {
            if (i1 == 0 and j1 == 0)
              continue;
            if (i - i1 >= 0 and i - i1 < h and j - j1 >= 0 and j - j1 < w and
                a[i - i1][j - j1] == '.')
              adj[i * w + j].push_back({(i - i1) * w + j - j1, 1});
          }
        }
      }
    }
  }
  bfs01(s.first * w + s.second);
  cout << dist[d.first * w + d.second] << newl;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
#ifdef EXECUTE_LOCALLY
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
#ifdef EXECUTE_LOCALLY
  cout << setprecision(3)
       << "program time: " << (double)clock() / CLOCKS_PER_SEC << newl;
#endif
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define newl "\\n"
using namespace std;
using lli = long long int;
using vvint = vector<vector<int>>;
using pii = pair<int, int>;
const int mod = 1e9 + 7;

const int N = 2e5 + 1;

int h, w;
pair<int, int> s, d;
vector<string> a;
vector<vector<pair<int, int>>> adj;
vector<int> dist;

void bfs01(int src) {
  dist[src] = 0;
  queue<int> q0;
  queue<pair<int, int>> q1;
  vector<bool> vis(h * w, 0);
  q0.push(src);
  while (!q0.empty() or !q1.empty()) {
    while (!q0.empty()) {
      int u = q0.front();
      q0.pop();
      vis[u] = 1;
      for (auto v : adj[u]) {
        if (!vis[v.first]) {
          int vv = v.first;
          int dd = v.second;
          if (dd == 1)
            q1.push({vv, dist[u] + 1});
          else {
            q0.push(vv);
            dist[vv] = dist[u];
          }
        }
      }
    }
    if (q1.size() > 0) {
      auto u1 = q1.front();
      int u = u1.first;
      q1.pop();
      if (vis[u])
        continue;
      vis[u] = 1;
      dist[u] = u1.second;
      for (auto v : adj[u]) {
        if (!vis[v.first]) {
          int vv = v.first;
          int dd = v.second;
          if (dd == 1)
            q1.push({vv, dist[u] + 1});
          else {
            q0.push(vv);
            dist[vv] = dist[u];
          }
        }
      }
    }
  }
  return;
}

void solve() {
  cin >> h >> w;
  cin >> s.first >> s.second;
  cin >> d.first >> d.second;
  --s.first, --s.second, --d.first, --d.second;

  dist = vector<int>(h * w, -1);
  a = vector<string>(h);
  adj = vector<vector<pair<int, int>>>(h * w);

  for (int i = 0; i < h; ++i)
    cin >> a[i];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (a[i][j] != '#') {
        if (i - 1 >= 0 and a[i - 1][j] == '.')
          adj[i * w + j].push_back({(i - 1) * w + (j), 0});
        if (i + 1 < h and a[i + 1][j] == '.')
          adj[i * w + j].push_back({(i + 1) * w + (j), 0});
        if (j - 1 >= 0 and a[i][j - 1] == '.')
          adj[i * w + j].push_back({(i)*w + (j - 1), 0});
        if (j + 1 < w and a[i][j + 1] == '.')
          adj[i * w + j].push_back({(i)*w + (j + 1), 0});
        for (int i1 = -2; i1 <= 2; ++i1) {
          for (int j1 = -2; j1 <= 2; ++j1) {
            if (i1 == 0 and j1 == 0)
              continue;
            if (i - i1 >= 0 and i - i1 < h and j - j1 >= 0 and j - j1 < w and
                a[i - i1][j - j1] == '.')
              adj[i * w + j].push_back({(i - i1) * w + j - j1, 1});
          }
        }
      }
    }
  }
  bfs01(s.first * w + s.second);
  cout << dist[d.first * w + d.second] << newl;
  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
#ifdef EXECUTE_LOCALLY
  freopen("in.txt", "r", stdin);
#endif
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
#ifdef EXECUTE_LOCALLY
  cout << setprecision(3)
       << "program time: " << (double)clock() / CLOCKS_PER_SEC << newl;
#endif
  return 0;
}