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

#include <bits/stdc++.h>

#define INF 1e15
#define EPS 1e-9
#define MOD 1000000007
#define Pi 3.14159265358979323846264338327950288419716939937510

#define umap unordered_map
#define uset unordered_set
#define fi first
#define se second
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define maxE(a, n) *max_element(a, a + n);
#define minE(a, n) *min_element(a, a + n);
#define all(x) x.begin(), x.end()

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define fori(i, a, n) for (ll i = a; i < n; i++)
#define ford(i, a, n) for (ll i = n - 1; i >= a; i--)

ll gcd(ll a, ll b) {
  ll temp;
  while (b > 0) {
    temp = a % b;
    a = b;
    b = temp;
  }
  return a;
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll power(ll x, ll b, ll m) {
  ll p = 1;
  while (b > 0) {
    if (b & 1) {
      p = p * x;
      p %= m;
    }
    b >>= 1;
    x *= x;
    x %= m;
  }
  return p % m;
}

using namespace std;

vector<vector<char>> grid(1001, vector<char>(1001));
vector<vector<bool>> visited(1001, vector<bool>(1001));

ll h, w;

ll cost = 0;

bool isSafe(ll i, ll j) {
  return i >= 0 && i < h && j >= 0 && j < w && grid[i][j] != '#';
}

bool bfs(pll start, pll end) {

  deque<pair<pll, ll>> q;
  q.push_front({start, 0});

  while (!q.empty()) {
    pair<pll, ll> front = q.front();
    q.pop_front();
    visited[front.fi.fi][front.fi.se] = true;
    if (front.fi == end) {
      cost = front.se;
      return true;
    }

    bool flag = false;

    if (isSafe(front.fi.fi + 1, front.fi.se) &&
        visited[front.fi.fi + 1][front.fi.se] != true) {
      q.push_front({{front.fi.fi + 1, front.fi.se}, front.se});
      flag = true;
    }

    if (isSafe(front.fi.fi - 1, front.fi.se) &&
        visited[front.fi.fi - 1][front.fi.se] != true) {
      q.push_front({{front.fi.fi - 1, front.fi.se}, front.se});
      flag = true;
    }

    if (isSafe(front.fi.fi, front.fi.se + 1) &&
        visited[front.fi.fi][front.fi.se + 1] != true) {
      q.push_front({{front.fi.fi, front.fi.se + 1}, front.se});
      flag = true;
    }

    if (isSafe(front.fi.fi, front.fi.se - 1) &&
        visited[front.fi.fi][front.fi.se - 1] != true) {
      q.push_front({{front.fi.fi, front.fi.se - 1}, front.se});
      flag = true;
    }

    ll lowr = max((ll)0, front.fi.fi - 2);
    ll highr = min(h - 1, front.fi.fi + 2);
    ll lowc = max((ll)0, front.fi.se - 2);
    ll highc = min(w - 1, front.fi.se + 2);

    for (int i = lowr; i <= highr; i++) {
      for (int j = lowc; j <= highc; j++) {
        if (grid[i][j] != '#' && visited[i][j] != true)
          q.push_back({{i, j}, front.se + 1});
      }
    }
  }

  return false;
}

void solve() {

  cin >> h >> w;

  pll magic_pos;
  cin >> magic_pos.fi >> magic_pos.se;
  magic_pos.fi = magic_pos.fi - 1;
  magic_pos.se = magic_pos.se - 1;

  pll final_pos;
  cin >> final_pos.fi >> final_pos.se;
  final_pos.fi = final_pos.fi - 1;
  final_pos.se = final_pos.se - 1;

  fori(i, 0, h) fori(j, 0, w) cin >> grid[i][j];

  fori(i, 0, h) fori(j, 0, w) visited[i][j] = false;

  bool op = bfs(magic_pos, final_pos);
  if (op)
    cout << cost << endl;
  else
    cout << -1 << endl;
}

int main() {

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

  fast;
  ll t = 1;

  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define INF 1e15
#define EPS 1e-9
#define MOD 1000000007
#define Pi 3.14159265358979323846264338327950288419716939937510

#define umap unordered_map
#define uset unordered_set
#define fi first
#define se second
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define maxE(a, n) *max_element(a, a + n);
#define minE(a, n) *min_element(a, a + n);
#define all(x) x.begin(), x.end()

#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define fori(i, a, n) for (ll i = a; i < n; i++)
#define ford(i, a, n) for (ll i = n - 1; i >= a; i--)

ll gcd(ll a, ll b) {
  ll temp;
  while (b > 0) {
    temp = a % b;
    a = b;
    b = temp;
  }
  return a;
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll power(ll x, ll b, ll m) {
  ll p = 1;
  while (b > 0) {
    if (b & 1) {
      p = p * x;
      p %= m;
    }
    b >>= 1;
    x *= x;
    x %= m;
  }
  return p % m;
}

using namespace std;

ll cost = 0;

bool isSafe(ll n, ll m, ll i, ll j, vector<vector<char>> grid) {
  return i >= 0 && i < n && j >= 0 && j < m && grid[i][j] != '#';
}

bool bfs(vector<vector<char>> grid, pll start, pll end,
         vector<vector<bool>> &visited) {
  ll n = grid.size();
  ll m = grid[0].size();

  deque<pair<pll, ll>> q;
  q.push_front({start, 0});

  while (!q.empty()) {
    pair<pll, ll> front = q.front();
    q.pop_front();
    visited[front.fi.fi][front.fi.se] = true;
    if (front.fi == end) {
      cost = front.se;
      return true;
    }

    bool flag = false;

    if (isSafe(grid.size(), grid[0].size(), front.fi.fi + 1, front.fi.se,
               grid) &&
        visited[front.fi.fi + 1][front.fi.se] != true) {
      q.push_front({{front.fi.fi + 1, front.fi.se}, front.se});
      flag = true;
    }

    if (isSafe(grid.size(), grid[0].size(), front.fi.fi - 1, front.fi.se,
               grid) &&
        visited[front.fi.fi - 1][front.fi.se] != true) {
      q.push_front({{front.fi.fi - 1, front.fi.se}, front.se});
      flag = true;
    }

    if (isSafe(grid.size(), grid[0].size(), front.fi.fi, front.fi.se + 1,
               grid) &&
        visited[front.fi.fi][front.fi.se + 1] != true) {
      q.push_front({{front.fi.fi, front.fi.se + 1}, front.se});
      flag = true;
    }

    if (isSafe(grid.size(), grid[0].size(), front.fi.fi, front.fi.se - 1,
               grid) &&
        visited[front.fi.fi][front.fi.se - 1] != true) {
      q.push_front({{front.fi.fi, front.fi.se - 1}, front.se});
      flag = true;
    }

    if (flag == false) {
      ll lowr = max((ll)0, front.fi.fi - 2);
      ll highr = min(n - 1, front.fi.fi + 2);
      ll lowc = max((ll)0, front.fi.se - 2);
      ll highc = min(m - 1, front.fi.se + 2);

      for (int i = lowr; i <= highr; i++) {
        for (int j = lowc; j <= highc; j++) {
          pll t(i, j);
          if (t == end) {
            cost = front.se + 1;
            return true;
          }
          if (grid[i][j] != '#' && visited[i][j] != true)
            q.push_back({{i, j}, front.se + 1});
        }
      }
    }
  }

  return false;
}

void solve() {

  ll h, w;
  cin >> h >> w;

  pll magic_pos;
  cin >> magic_pos.fi >> magic_pos.se;
  magic_pos.fi = magic_pos.fi - 1;
  magic_pos.se = magic_pos.se - 1;

  pll final_pos;
  cin >> final_pos.fi >> final_pos.se;
  final_pos.fi = final_pos.fi - 1;
  final_pos.se = final_pos.se - 1;

  vector<vector<char>> grid(h, vector<char>(w));
  fori(i, 0, h) fori(j, 0, w) cin >> grid[i][j];

  vector<vector<bool>> visited(h, vector<bool>(w));
  fori(i, 0, h) fori(j, 0, w) visited[i][j] = false;

  // fori(i,0,h){
  // 	fori(j,0,w)
  // 		cout<<grid[i][j]<<" " ;
  // 	cout<<endl;
  // }
  bool op = bfs(grid, magic_pos, final_pos, visited);
  if (op)
    cout << cost << endl;
  else
    cout << -1 << endl;
}

int main() {

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

  fast;
  ll t = 1;

  while (t--) {
    solve();
  }
}