Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define mod ((ll)1e9 + 7)
#define inf ((ll)1e18 + 5)
#define maxx ((ll)1e6 + 5)
#define pb push_back
#define endl "\n"
#define time(i)                                                                \
  cout << "\nTime Elapsed " << i << " : " << 1.0 * clock() / CLOCKS_PER_SEC    \
       << " sec\n";
#define usefile                                                                \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
ll exp(ll x, ll y, ll p) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % p) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return res;
}
ll expm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % mod) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res;
}
/*
bool sv[maxx]={0};    // returns 0 if a num is prime
int sp[maxx]={0};     // gives smallest prime factor of the number
void Sieve() {for(int i= 2; i< maxx; i+= 2) sv[i]=1,sp[i] = 2;sv[2]=0;for(ll i =
3; i < maxx; i += 2){if (!sv[i]){sp[i] = i;for (ll j = i; (j*i) < maxx; j +=
2){if (!sv[j*i])sv[j*i] = true, sp[j*i] = i;}}}} ll fact[maxx]={0}; // returns
factorial of that number void facto() {fact[0]=1;fact[1]=1;for(int
i=2;i<maxx;i++)fact[i]=(fact[i-1]*i)%mod;} ll ncrm(ll n,ll r) {ll res=1;
res=fact[n]; res=(res*(expm(fact[r],mod-2)))%mod;
res=(res*(expm(fact[n-r],mod-2)))%mod; return res;} ll inv[maxx];   // returns
the modular inverse of number void inversify( ll m){inv[1]=1;for(int
i=2;i<maxx;i++){inv[i]= m- m/i*inv[m%i]%m;}} ll ncr(ll n,ll r,ll m){int
ans=1;for (int i = 0; i < r; i++){ans=ans*(n-i)%m*inv[i+1]%m;}return ans;}
*/

int adj[20][20];
int vis[20][20];
int dis[20][20];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int h, w;
bool check(int x, int y) {
  if (x < 0 || x >= h || y < 0 || y >= w || adj[x][y] == 0)
    return 0;
  return 1;
}

int bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  memset(vis, 0, sizeof(vis));
  memset(dis, 0, sizeof(dis));
  dis[sx][sy] = 0;
  int maxDis = -2;
  while (q.size()) {
    pair<int, int> u = q.front();
    q.pop();
    vis[u.first][u.second] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = u.first + dx[i];
      int ny = u.second + dy[i];
      if (check(nx, ny) && vis[nx][ny] != 1) {
        dis[nx][ny] = dis[u.first][u.second] + 1;
        maxDis = max(maxDis, dis[nx][ny]);
        q.push({nx, ny});
      }
    }
  }
  return maxDis;
}

signed main() {
  fast;
  // usefile;
  // test
  {
    cin >> h >> w;

    char ch;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> ch;
        if (ch == '.')
          adj[i][j] = 1;
      }
    }
    int mx = -1;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (adj[i][j] == 1)
          mx = max(mx, bfs(i, j));
    cout << mx;
  }
  // time("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define mod ((ll)1e9 + 7)
#define inf ((ll)1e18 + 5)
#define maxx ((ll)1e6 + 5)
#define pb push_back
#define endl "\n"
#define time(i)                                                                \
  cout << "\nTime Elapsed " << i << " : " << 1.0 * clock() / CLOCKS_PER_SEC    \
       << " sec\n";
#define usefile                                                                \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define fast                                                                   \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
ll exp(ll x, ll y, ll p) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % p) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return res;
}
ll expm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % mod) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res;
}
/*
bool sv[maxx]={0};    // returns 0 if a num is prime
int sp[maxx]={0};     // gives smallest prime factor of the number
void Sieve() {for(int i= 2; i< maxx; i+= 2) sv[i]=1,sp[i] = 2;sv[2]=0;for(ll i =
3; i < maxx; i += 2){if (!sv[i]){sp[i] = i;for (ll j = i; (j*i) < maxx; j +=
2){if (!sv[j*i])sv[j*i] = true, sp[j*i] = i;}}}} ll fact[maxx]={0}; // returns
factorial of that number void facto() {fact[0]=1;fact[1]=1;for(int
i=2;i<maxx;i++)fact[i]=(fact[i-1]*i)%mod;} ll ncrm(ll n,ll r) {ll res=1;
res=fact[n]; res=(res*(expm(fact[r],mod-2)))%mod;
res=(res*(expm(fact[n-r],mod-2)))%mod; return res;} ll inv[maxx];   // returns
the modular inverse of number void inversify( ll m){inv[1]=1;for(int
i=2;i<maxx;i++){inv[i]= m- m/i*inv[m%i]%m;}} ll ncr(ll n,ll r,ll m){int
ans=1;for (int i = 0; i < r; i++){ans=ans*(n-i)%m*inv[i+1]%m;}return ans;}
*/

int adj[20][20];
int vis[20][20];
int dis[20][20];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int h, w;
bool check(int x, int y) {
  if (x < 0 || x >= h || y < 0 || y >= w || adj[x][y] == 0)
    return 0;
  return 1;
}

int bfs(int sx, int sy) {
  queue<pair<int, int>> q;
  q.push({sx, sy});
  memset(vis, 0, sizeof(vis));
  memset(dis, 0, sizeof(dis));
  dis[sx][sy] = 0;
  int maxDis = -2;
  while (q.size()) {
    pair<int, int> u = q.front();
    q.pop();
    vis[u.first][u.second] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = u.first + dx[i];
      int ny = u.second + dy[i];
      if (check(nx, ny) && vis[nx][ny] != 1) {
        dis[nx][ny] = dis[u.first][u.second] + 1;
        maxDis = max(maxDis, dis[nx][ny]);
        q.push({nx, ny});
        vis[nx][ny] = 1;
      }
    }
  }
  return maxDis;
}

signed main() {
  fast;
  // usefile;
  // test
  {
    cin >> h >> w;

    char ch;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> ch;
        if (ch == '.')
          adj[i][j] = 1;
      }
    }
    int mx = -1;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (adj[i][j] == 1)
          mx = max(mx, bfs(i, j));
    cout << mx;
  }
  // time("");
  return 0;
}