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

/*
CF ID: Ankit717
LANG: C++
*/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define mod 1000000007
#define maxn 200005
#define f(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rf(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define pb push_back
#define pf push_front
#define nl cout << "\\n";

const ll INF = 1e9L + 5;

struct Data {
  ll p;
  ll h;
};

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

bool comp(pll &a, pll &b) {
  if (a.first == b.first) {
    return a.second > b.second;
  } else {
    return a.first < b.first;
  }
}

// vector<pll> adj[maxn];
// vector<ll> par;
// vector<bool> vis;

ll gcd(ll a, ll b) {
  while (a != 0) {
    ll temp = a;
    a = b % a;
    b = temp;
  }

  return b;
}

// bool prime[maxn],near[maxn];
// void sieve(){
// 	f(i,0,maxn+1){
// 		prime[i]=true;
// 	}
// 	prime[0]=falsrime[1]=false;
// 	for(ll i=2;i*i<=maxn;i++){
// 		if(prime[i]){
// 			for(j=i*i;j<=maxn;j+=i){
// 				prime[j]=false;
// 			}
// 		}
// 	}
// }

ll x[24] = {1, -1, 0,  0, 1, 1, -1, -1, 2,  2,  -2, -2,
            0, 0,  -2, 2, 2, 2, -1, 1,  -2, -2, -1, 1};
ll y[24] = {0, 0,  1, -1, 1,  -1, 1, -1, 2,  -2, 2,  -2,
            2, -2, 0, 0,  -1, 1,  2, 2,  -1, 1,  -2, -2};

void solve() {
  ll h, w;
  cin >> h >> w;

  ll ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;

  ll dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;

  string s[h];
  f(i, 0, h) { cin >> s[i]; }

  ll dis[h][w];
  // memset(dis,INF,sizeof(dis));

  f(i, 0, h) {
    f(j, 0, w) {
      dis[i][j] = INF;
      // cout << dis[i][j]<<" ";
    }
  }

  deque<pll> q;
  q.pf({ch, cw});
  dis[ch][cw] = 0;
  pll p;

  while (!q.empty()) {
    p = q.front();
    q.pop_front();

    ll lx = p.first, ly = p.second;
    for (ll i = 0; i < 4; i++) {
      if (lx + x[i] < h && ly + y[i] < w && lx + x[i] >= 0 && ly + y[i] >= 0) {
        if (dis[lx][ly] < dis[lx + x[i]][ly + y[i]] &&
            s[lx + x[i]][ly + y[i]] == '.') {
          q.pf({lx + x[i], ly + y[i]});
          dis[lx + x[i]][ly + y[i]] = dis[lx][ly];
        }
      }
    }

    for (ll i = 4; i < 24; i++) {
      if (lx + x[i] < h && ly + y[i] < w && lx + x[i] >= 0 && ly + y[i] >= 0) {
        if (dis[lx][ly] + 1 < dis[lx + x[i]][ly + y[i]] &&
            s[lx + x[i]][ly + y[i]] == '.') {
          q.pb({lx + x[i], ly + y[i]});
          dis[lx + x[i]][ly + y[i]] = dis[lx][ly] + 1;
        }
      }
    }
  }

  if (dis[dh][dw] != INF && s[dh][dw] != '#') {
    cout << dis[dh][dw];
  } else {
    cout << -1;
  }
}

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

  // ll t;
  // cin >> t;
  // f(i,0,t){
  solve();
  //}
}


Error: TLE

Fixed submission:

/*
CF ID: Ankit717
LANG: C++
*/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define mod 1000000007
#define maxn 200005
#define f(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rf(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define pb push_back
#define pf push_front
#define nl cout << "\\n";

const ll INF = 1e9L + 5;

struct Data {
  ll p;
  ll h;
};

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

bool comp(pll &a, pll &b) {
  if (a.first == b.first) {
    return a.second > b.second;
  } else {
    return a.first < b.first;
  }
}

// vector<pll> adj[maxn];
// vector<ll> par;
// vector<bool> vis;

ll gcd(ll a, ll b) {
  while (a != 0) {
    ll temp = a;
    a = b % a;
    b = temp;
  }

  return b;
}

// bool prime[maxn],near[maxn];
// void sieve(){
// 	f(i,0,maxn+1){
// 		prime[i]=true;
// 	}
// 	prime[0]=falsrime[1]=false;
// 	for(ll i=2;i*i<=maxn;i++){
// 		if(prime[i]){
// 			for(j=i*i;j<=maxn;j+=i){
// 				prime[j]=false;
// 			}
// 		}
// 	}
// }

ll x[24] = {1, -1, 0,  0, 1, 1, -1, -1, 2,  2,  -2, -2,
            0, 0,  -2, 2, 2, 2, -1, 1,  -2, -2, -1, 1};
ll y[24] = {0, 0,  1, -1, 1,  -1, 1, -1, 2,  -2, 2,  -2,
            2, -2, 0, 0,  -1, 1,  2, 2,  -1, 1,  -2, -2};

void solve() {
  ll h, w;
  cin >> h >> w;

  ll ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;

  ll dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;

  string s[h];
  f(i, 0, h) { cin >> s[i]; }

  queue<pll> q;
  bool vis[h][w];
  memset(vis, false, sizeof(vis));
  ll dis[h][w];
  memset(dis, INF, sizeof(dis));

  q.push({ch, cw});
  vis[ch][cw] = true;
  dis[ch][cw] = 0;

  while (!q.empty()) {
    pll temp = q.front();
    q.pop();

    f(i, 0, 4) {
      if (temp.first + x[i] < h && temp.second + y[i] < w &&
          temp.first + x[i] >= 0 && temp.second + y[i] >= 0) {
        if (!vis[temp.first + x[i]][temp.second + y[i]] &&
            s[temp.first + x[i]][temp.second + y[i]] == '.') {
          vis[temp.first + x[i]][temp.second + y[i]] = true;
          q.push({temp.first + x[i], temp.second + y[i]});
          dis[temp.first + x[i]][temp.second + y[i]] =
              min(dis[temp.first + x[i]][temp.second + y[i]],
                  dis[temp.first][temp.second]);
        } else if (s[temp.first + x[i]][temp.second + y[i]] == '.') {
          if (dis[temp.first + x[i]][temp.second + y[i]] >
              dis[temp.first][temp.second]) {
            dis[temp.first + x[i]][temp.second + y[i]] =
                dis[temp.first][temp.second];
            q.push({temp.first + x[i], temp.second + y[i]});
          }
        }
      }
    }

    f(i, 4, 24) {
      if (temp.first + x[i] < h && temp.second + y[i] < w &&
          temp.first + x[i] >= 0 && temp.second + y[i] >= 0) {
        if (!vis[temp.first + x[i]][temp.second + y[i]] &&
            s[temp.first + x[i]][temp.second + y[i]] == '.') {
          vis[temp.first + x[i]][temp.second + y[i]] = true;
          q.push({temp.first + x[i], temp.second + y[i]});
          dis[temp.first + x[i]][temp.second + y[i]] =
              min(dis[temp.first + x[i]][temp.second + y[i]],
                  dis[temp.first][temp.second] + 1);
        } else if (s[temp.first + x[i]][temp.second + y[i]] == '.') {
          if (dis[temp.first + x[i]][temp.second + y[i]] >
              dis[temp.first][temp.second] + 1) {
            dis[temp.first + x[i]][temp.second + y[i]] =
                dis[temp.first][temp.second] + 1;
            q.push({temp.first + x[i], temp.second + y[i]});
          }
        }
      }
    }
  }

  if (vis[dh][dw]) {
    cout << dis[dh][dw];
  } else {
    cout << -1;
  }
}

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

  // ll t;
  // cin >> t;
  // f(i,0,t){
  solve();
  //}
}