Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define _GLIBCXX_DEBUG
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vvd = vector<vd>;
using vvvi = vector<vvi>;
using vvvll = vector<vvll>;
using vvvd = vector<vvd>;

const double pi = acos(-1);
const ll MOD = 1e9 + 7;
const ll INF = (1LL << 60);

int main() {
  int h, w;
  cin >> h >> w;
  char graph[h][w];
  rep(i, h) rep(j, w) cin >> graph[i][j];
  int ans = 0;
  rep(i, h) {
    rep(j, w) {
      if (graph[i][j] == '#')
        continue;
      else {
        vvi d(h, vi(w, 1000));
        d[i][j] = 0;
        queue<pair<int, int>> q;
        q.push({i, j});
        while (q.size() != 0) {
          auto p1 = q.front();
          q.pop();
          vector<pair<int, int>> move;
          move.push_back(make_pair(1, 0));
          move.push_back(make_pair(-1, 0));
          move.push_back(make_pair(0, 1));
          move.push_back(make_pair(0, -1));
          for (auto p : move) {
            int dx = p.fi, dy = p.se;
            int x = dx + p1.fi, y = dy + p1.se;
            if (x < 0 || y < 0 || x >= h || y >= w)
              continue;
            else if (graph[x][y] == '#')
              continue;
            else if (d[x][y] < d[p1.fi][p1.se] + 1)
              continue;
            else {
              d[x][y] = d[p1.fi][p1.se] + 1;
              q.push({x, y});
            }
          }
        }
        rep(k, h) {
          rep(l, w) {
            if (d[k][l] != 1000) {
              ans = max(ans, d[k][l]);
            }
          }
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define _GLIBCXX_DEBUG
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vvd = vector<vd>;
using vvvi = vector<vvi>;
using vvvll = vector<vvll>;
using vvvd = vector<vvd>;

const double pi = acos(-1);
const ll MOD = 1e9 + 7;
const ll INF = (1LL << 60);

int main() {
  int h, w;
  cin >> h >> w;
  char graph[h][w];
  rep(i, h) rep(j, w) cin >> graph[i][j];
  int ans = 0;
  rep(i, h) {
    rep(j, w) {
      if (graph[i][j] == '#')
        continue;
      else {
        vvi d(h, vi(w, 1000));
        d[i][j] = 0;
        queue<pair<int, int>> q;
        q.push({i, j});
        while (q.size() != 0) {
          auto p1 = q.front();
          q.pop();
          vector<pair<int, int>> move;
          move.push_back(make_pair(1, 0));
          move.push_back(make_pair(-1, 0));
          move.push_back(make_pair(0, 1));
          move.push_back(make_pair(0, -1));
          for (auto p : move) {
            int dx = p.fi, dy = p.se;
            int x = dx + p1.fi, y = dy + p1.se;
            if (x < 0 || y < 0 || x >= h || y >= w)
              continue;
            else if (graph[x][y] == '#')
              continue;
            else if (d[x][y] <= d[p1.fi][p1.se] + 1)
              continue;
            else {
              d[x][y] = d[p1.fi][p1.se] + 1;
              q.push({x, y});
            }
          }
        }
        rep(k, h) {
          rep(l, w) {
            if (d[k][l] != 1000) {
              ans = max(ans, d[k][l]);
            }
          }
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}