Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)

const int INF = 1001001001;
vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, 1, 0, -1};

int h, w;
vector<string> s;
vector<int> steps;

void bfs(pair<int, int> st, pair<int, int> gl) {
  // stからの距離
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<pair<int, int>> q;

  auto push = [&](pair<int, int> now, int d) {
    int y = now.first;
    int x = now.second;
    if (dist.at(y).at(x) != INF)
      return;
    dist.at(y).at(x) = d;
    q.push(now);
  };
  push(st, 0);
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();

    int y = now.first;
    int x = now.second;
    int d = dist.at(y).at(x);

    rep(i, 4) {
      int nowy = y + dy.at(i);
      int nowx = x + dx.at(i);
      if (nowy >= 0 && nowy < h && nowx >= 0 && nowx < w &&
          s.at(nowy).at(nowx) != '#') {
        push(make_pair(nowy, nowx), d + 1);
      }
    }
    int gy = gl.first;
    int gx = gl.second;
    if (dist.at(gy).at(gx) != INF) {
      steps.push_back(dist.at(gy).at(gx));
    }
  }
}

int main() {
  cin >> h >> w;
  s.resize(h);
  rep(i, h) cin >> s.at(i);

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s.at(i).at(j) == '.') {
        for (int k = 0; k < h; ++k) {
          for (int l = 0; l < w; ++l) {
            if (k == i && l == j)
              continue; // スタートとゴールは一致しない
            if (s.at(k).at(l) == '#')
              continue; // 壁はゴールにならない
            pair<int, int> st = make_pair(i, j);
            pair<int, int> gl = make_pair(k, l);
            bfs(st, gl);
          }
        }
      }
    }
  }
  sort(steps.begin(), steps.end());
  cout << steps.back() << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)

const int INF = 1001001001;
vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, 1, 0, -1};

int h, w;
vector<string> s;
vector<int> steps;

void bfs(pair<int, int> st, pair<int, int> gl) {
  // stからの距離
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<pair<int, int>> q;

  auto push = [&](pair<int, int> now, int d) {
    int y = now.first;
    int x = now.second;
    if (dist.at(y).at(x) != INF)
      return;
    dist.at(y).at(x) = d;
    q.push(now);
  };
  push(st, 0);
  while (!q.empty()) {
    pair<int, int> now = q.front();
    q.pop();

    int y = now.first;
    int x = now.second;
    int d = dist.at(y).at(x);

    rep(i, 4) {
      int nowy = y + dy.at(i);
      int nowx = x + dx.at(i);
      if (nowy >= 0 && nowy < h && nowx >= 0 && nowx < w &&
          s.at(nowy).at(nowx) != '#') {
        push(make_pair(nowy, nowx), d + 1);
      }
    }
    int gy = gl.first;
    int gx = gl.second;
    if (dist.at(gy).at(gx) != INF) {
      steps.push_back(dist.at(gy).at(gx));
      return;
    }
  }
}

int main() {
  cin >> h >> w;
  s.resize(h);
  rep(i, h) cin >> s.at(i);

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s.at(i).at(j) == '.') {
        for (int k = 0; k < h; ++k) {
          for (int l = 0; l < w; ++l) {
            if (k == i && l == j)
              continue; // スタートとゴールは一致しない
            if (s.at(k).at(l) == '#')
              continue; // 壁はゴールにならない
            pair<int, int> st = make_pair(i, j);
            pair<int, int> gl = make_pair(k, l);
            bfs(st, gl);
          }
        }
      }
    }
  }
  sort(steps.begin(), steps.end());
  cout << steps.back() << endl;
  return 0;
}