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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pcc pair<char, char>
#define pdd pair<double, double>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define flsh fflush(stdout)
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1000000000007

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_tree;

const int N = 1005;
ll a[N][N];
ll ans[N][N];
ll vis[N][N];
ll n, m;

bool in(ll x, ll y) {
  if (x < 0 || y < 0) {
    return false;
  }
  if (x > n || y > m) {
    return false;
  }
  return true;
}

struct comp {
  bool operator()(const pair<ll, pll> &a, const pair<ll, pll> &b) {
    return a.first < b.first;
  }
};

void bfs(ll x, ll y, ll xx) {

  vis[x][y] = 1;
  ans[x][y] = xx;
  ll i, j;
  ans[x][y] = 0;
  deque<pll> q;

  q.push_front(mp(x, y));
  vis[x][y] = 1;
  while (!q.empty()) {

    pll tt = q.front();
    q.pop_front();
    ll posx = tt.first, posy = tt.second;

    ll lol = ans[posx][posy];
    ll dx[] = {1, -1, 0, 0};
    ll dy[] = {0, 0, 1, -1};
    for (i = 0; i < 4; i++) {
      ll posxx = posx + dx[i];
      ll posyy = posy + dy[i];
      if (in(posxx, posyy)) {
        if (ans[posxx][posyy] <= lol)
          continue;
        if (!a[posxx][posyy])
          continue;
        q.push_front(mp(posxx, posyy));
        ans[posxx][posyy] = lol;
      }
    }
    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        ll posxx = posx + i;
        ll posyy = posy + j;
        if (in(posxx, posyy)) {
          if (!a[posxx][posyy])
            continue;
          if (ans[posxx][posyy] <= lol) {
            continue;
          }
          q.push_back(mp(posxx, posyy));
          ans[posxx][posyy] = lol + 1;
        }
      }
    }
  }
}

void solve() {
  cin >> n >> m;
  ll i, j;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string s;
  for (i = 0; i < n; i++) {
    cin >> s;
    for (j = 0; j < m; j++) {
      ans[i][j] = 1e18;
      if (s[j] == '.') {
        a[i][j] = 1;
      } else {
        a[i][j] = 0;
      }
    }
  }
  bfs(x1, y1, 0);
  ll res = 1e18;
  res = min(res, ans[x2][y2]);
  // for (i = 0; i < n; i++)
  // {
  //     for (j = 0; j < m; j++)
  //     {
  //         // cout << ans[i][j] << " ";
  //     }
  //     // cout << "\\n";
  // }
  if (res == 1e18) {
    cout << "-1\\n";
    return;
  }
  cout << res << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);

  ll t = 1;

  // cin>>t;

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pcc pair<char, char>
#define pdd pair<double, double>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define flsh fflush(stdout)
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1000000000007

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_tree;

const int N = 1005;
ll a[N][N];
ll ans[N][N];
ll vis[N][N];
ll n, m;

bool in(ll x, ll y) {
  if (x < 0 || y < 0) {
    return false;
  }
  if (x > n || y > m) {
    return false;
  }
  return true;
}

// struct comp{
//     bool operator() (const pair<ll,pll> &a,const pair<ll,pll> &b)
//     {
//         return a.first<b.first;
//     }
// };

void bfs(ll x, ll y, ll xx) {

  vis[x][y] = 1;
  ll i, j;
  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>> q;
  q.push(mp(xx, mp(x, y)));
  while (!q.empty()) {

    pair<ll, pll> tt = q.top();
    q.pop();
    ll posx = tt.second.first, posy = tt.second.second;
    ll lol = tt.first;
    ans[posx][posy] = min(ans[posx][posy], lol);
    if (in(posx + 1, posy)) {
      if (a[posx + 1][posy]) {
        if (!vis[posx + 1][posy]) {
          q.push(mp(lol, mp(posx + 1, posy)));
          vis[posx + 1][posy] = 1;
        }
      }
    }
    if (in(posx, posy + 1)) {

      if (a[posx][posy + 1]) {
        if (!vis[posx][posy + 1]) {
          q.push(mp(lol, mp(posx, posy + 1)));
          vis[posx][posy + 1] = 1;
        }
      }
    }
    if (in(posx - 1, posy)) {
      if (a[posx - 1][posy]) {
        if (!vis[posx - 1][posy]) {
          q.push(mp(lol, mp(posx - 1, posy)));
          vis[posx - 1][posy] = 1;
        }
      }
    }
    if (in(posx, posy - 1)) {
      if (a[posx][posy - 1]) {
        if (!vis[posx][posy - 1]) {
          q.push(mp(lol, mp(posx, posy - 1)));
          vis[posx][posy - 1] = 1;
        }
      }
    }

    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        ll posxx = posx + i;
        ll posyy = posy + j;
        if (in(posxx, posyy)) {
          if (a[posxx][posyy]) {
            // if (!vis[posxx][posyy])
            // {
            q.push(mp(lol + 1, mp(posxx, posyy)));
            vis[posxx][posyy] = 1;
            // }
          }
        }
      }
    }
  }
}

void solve() {
  cin >> n >> m;
  ll i, j;
  ll x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  string s;
  for (i = 0; i < n; i++) {
    cin >> s;
    for (j = 0; j < m; j++) {
      ans[i][j] = 1e18;
      if (s[j] == '.') {
        a[i][j] = 1;
      } else {
        a[i][j] = 0;
      }
    }
  }
  bfs(x1, y1, 0);
  ll res = 1e18;
  res = min(res, ans[x2][y2]);
  // for (i = 0; i < n; i++)
  // {
  //     for (j = 0; j < m; j++)
  //     {
  //         // cout << ans[i][j] << " ";
  //     }
  //     // cout << "\\n";
  // }
  if (res == 1e18) {
    cout << "-1\\n";
    return;
  }
  cout << res << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);

  ll t = 1;

  // cin>>t;

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

  return 0;
}