Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1);

int H, W;
bool movable(vector<string> &maze, vector<vector<int>> &memo, int x, int y,
             int X, int Y) {
  if (X < 0 || X >= W || Y < 0 || Y >= H) {
    return false;
  }
  if (maze.at(Y).at(X) == '#') {
    return false;
  }
  if (memo.at(Y).at(X) != -1) {
    return false;
  }
  return true;
}

int main() {
  cin >> H >> W;
  int ans = 0;
  vector<string> maze(H);
  rep(i, H) { cin >> maze.at(i); }
  rep(i, H * W) {
    rep(j, H * W) {
      int sx = i % W, sy = i / W, gx = j % W, gy = j / W;
      if (maze.at(sy).at(sx) == '#' || maze.at(gy).at(gx) == '#') {
        continue;
      }
      queue<vector<int>> next;
      vector<vector<int>> memo(H, vector<int>(W, -1));
      memo.at(sy).at(sx) = 0;
      next.push({sx, sy});
      while (!next.empty()) {
        int x = next.front().at(0), y = next.front().at(1);
        // cout<<x<<" "<<y<<endl;
        next.pop();
        if (x == gx && y == gy) {
          break;
        }

        if (movable(maze, memo, x, y, x + 1, y)) {
          memo.at(y).at(x + 1) = memo.at(y).at(x) + 1;
          next.push({x + 1, y});
        }
        if (movable(maze, memo, x, y, x - 1, y)) {
          memo.at(y).at(x - 1) = memo.at(y).at(x) + 1;
          next.push({x - 1, y});
        }
        if (movable(maze, memo, x, y, x, y + 1)) {
          memo.at(y + 1).at(x) = memo.at(y).at(x) + 1;
          next.push({x, y + 1});
        }
        if (movable(maze, memo, x, y, x, y - 1)) {
          memo.at(y - 1).at(x) = memo.at(y).at(x) + 1;
          next.push({x, y - 1});
        }
      }
      ans = max(ans, memo.at(gy).at(gx));
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1);

int H, W;
bool movable(vector<string> &maze, vector<vector<int>> &memo, int x, int y,
             int X, int Y) {
  if (X < 0 || X >= W || Y < 0 || Y >= H) {
    return false;
  }
  if (maze.at(Y).at(X) == '#') {
    return false;
  }
  if (memo.at(Y).at(X) != -1) {
    return false;
  }
  return true;
}

int main() {
  cin >> H >> W;
  int ans = 0;
  vector<string> maze(H);
  rep(i, H) { cin >> maze.at(i); }
  rep(i, H * W) {
    for (int j = 0; j < i; j++) {
      int sx = i % W, sy = i / W, gx = j % W, gy = j / W;
      if (maze.at(sy).at(sx) == '#' || maze.at(gy).at(gx) == '#') {
        continue;
      }
      queue<vector<int>> next;
      vector<vector<int>> memo(H, vector<int>(W, -1));
      memo.at(sy).at(sx) = 0;
      next.push({sx, sy});
      while (!next.empty()) {
        int x = next.front().at(0), y = next.front().at(1);
        // cout<<x<<" "<<y<<endl;
        next.pop();
        if (x == gx && y == gy) {
          break;
        }

        if (movable(maze, memo, x, y, x + 1, y)) {
          memo.at(y).at(x + 1) = memo.at(y).at(x) + 1;
          next.push({x + 1, y});
        }
        if (movable(maze, memo, x, y, x - 1, y)) {
          memo.at(y).at(x - 1) = memo.at(y).at(x) + 1;
          next.push({x - 1, y});
        }
        if (movable(maze, memo, x, y, x, y + 1)) {
          memo.at(y + 1).at(x) = memo.at(y).at(x) + 1;
          next.push({x, y + 1});
        }
        if (movable(maze, memo, x, y, x, y - 1)) {
          memo.at(y - 1).at(x) = memo.at(y).at(x) + 1;
          next.push({x, y - 1});
        }
      }
      ans = max(ans, memo.at(gy).at(gx));
    }
  }
  cout << ans << endl;

  return 0;
}