Problem p02803 (C++)

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

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPV(iter, v)                                                          \
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

int main() {
  ll H, W;
  cin >> H >> W;
  vector<string> S;
  REP(i, H) {
    string s;
    cin >> s;
    S.push_back(s);
  }

  map<pair<ll, ll>, vector<pair<ll, ll>>> G;
  vector<pair<ll, ll>> road;
  REP(i, H) {
    REP(j, W) {
      pair<ll, ll> p = make_pair(i, j);
      if (S[i][j] == '.') {
        if (i - 1 >= 0)
          if (S[i - 1][j] == '.')
            G[p].push_back(make_pair(i - 1, j));
        if (j - 1 >= 0)
          if (S[i][j - 1] == '.')
            G[p].push_back(make_pair(i, j - 1));
        if (i + 1 < H)
          if (S[i + 1][j] == '.')
            G[p].push_back(make_pair(i + 1, j));
        if (j + 1 < W)
          if (S[i][j + 1] == '.')
            G[p].push_back(make_pair(i, j + 1));
        road.push_back(p);
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i < road.size() - 1; ++i) {
    for (ll j = i + 1; j < road.size(); ++j) {
      pair<ll, ll> st = road[i];
      bool isDestination = false;
      queue<pair<ll, ll>> q;
      q.push(st);
      map<pair<ll, ll>, bool> visited;
      REPV(iter, road) visited[*iter] = false;
      map<pair<ll, ll>, ll> dist;
      dist[st] = 0;
      while (!q.empty()) {
        pair<ll, ll> p = q.front();
        q.pop();
        visited[p] = true;
        REPV(iter, G[p]) {
          if (!visited[*iter]) {
            visited[*iter] = true;
            q.push(*iter);
            dist[*iter] = dist[p] + 1;
            if (*iter == road[j]) {
              isDestination = true;
              break;
            }
          }
        }
        if (isDestination) {
          ans = max(ans, dist[road[j]]);
          break;
        }
      }
    }
  }

  cout << ans << endl;
}


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

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPV(iter, v)                                                          \
  for (auto(iter) = (v).begin(); (iter) != (v).end(); ++(iter))
#define ALL(v) (v).begin(), (v).end()

#define MOD 1000000007

using namespace std;

typedef long long ll;

int main() {
  ll H, W;
  cin >> H >> W;
  vector<string> S;
  REP(i, H) {
    string s;
    cin >> s;
    S.push_back(s);
  }

  map<pair<ll, ll>, vector<pair<ll, ll>>> G;
  vector<pair<ll, ll>> road;
  REP(i, H) {
    REP(j, W) {
      pair<ll, ll> p = make_pair(i, j);
      if (S[i][j] == '.') {
        if (i - 1 >= 0)
          if (S[i - 1][j] == '.')
            G[p].push_back(make_pair(i - 1, j));
        if (j - 1 >= 0)
          if (S[i][j - 1] == '.')
            G[p].push_back(make_pair(i, j - 1));
        if (i + 1 < H)
          if (S[i + 1][j] == '.')
            G[p].push_back(make_pair(i + 1, j));
        if (j + 1 < W)
          if (S[i][j + 1] == '.')
            G[p].push_back(make_pair(i, j + 1));
        road.push_back(p);
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i < road.size(); ++i) {
    pair<ll, ll> st = road[i];
    bool isDestination = false;
    queue<pair<ll, ll>> q;
    q.push(st);
    map<pair<ll, ll>, bool> visited;
    REPV(iter, road) visited[*iter] = false;
    map<pair<ll, ll>, ll> dist;
    dist[st] = 0;
    while (!q.empty()) {
      pair<ll, ll> p = q.front();
      q.pop();
      visited[p] = true;
      REPV(iter, G[p]) {
        if (!visited[*iter]) {
          visited[*iter] = true;
          q.push(*iter);
          dist[*iter] = dist[p] + 1;
          ans = max(ans, dist[*iter]);
        }
      }
    }
  }

  cout << ans << endl;
}