Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

const int inf = 10000;

int main() {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> vec(h + 2, vector<char>(w + 2, '#'));
  for (int x = 1; x <= h; ++x)
    for (int y = 1; y <= w; ++y)
      cin >> vec[x][y];

  vector<vector<int>> ans((h + 2) * (w + w),
                          vector<int>((h + 2) * (w + 2), inf));

  for (int x = 1; x <= h; ++x) {
    for (int y = 1; y <= w; ++y) {
      for (int d = 0; d < 4; ++d) {
        int dx = vector<int>({0, 0, -1, 1})[d];
        int dy = vector<int>({-1, 1, 0, 0})[d];
        int idx1 = (x) * (w + 2) + (y);
        int idx2 = (x + dx) * (w + 2) + (y + dy);
        if (vec[x][y] == '.' && vec[x + dx][y + dy] == '.')
          ans[idx1][idx2] = ans[idx2][idx1] = 1;
        ans[idx1][idx1] = 0;
      }
    }
  }

  for (int k = 1; k < (h + 2) * (w + 2); ++k) {
    for (int i = 1; i < (h + 2) * (w + 2); ++i) {
      for (int j = 1; j < (h + 2) * (w + 2); ++j) {
        int x1 = i / (w + 2);
        int y1 = i % (w + 2);
        int xm = k / (w + 2);
        int ym = k % (w + 2);
        int x2 = j / (w + 2);
        int y2 = j % (w + 2);
        int idx1 = x1 * (w + 2) + y1;
        int idxm = xm * (w + 2) + ym;
        int idx2 = x2 * (w + 2) + y2;
        if (ans[idx1][idxm] + ans[idxm][idx2] < ans[idx1][idx2])
          ans[idx1][idx2] = ans[idx2][idx1] = ans[idx1][idxm] + ans[idxm][idx2];
      }
    }
  }

  int vmax = 0;
  for (auto &seq : ans) {
    for (auto &v : seq) {
      if (v < inf && vmax < v)
        vmax = v;
    }
  }

  cout << vmax << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

const int inf = 10000;

int main() {
  int h, w;
  cin >> h >> w;

  vector<vector<char>> vec(h + 2, vector<char>(w + 2, '#'));
  for (int x = 1; x <= h; ++x)
    for (int y = 1; y <= w; ++y)
      cin >> vec[x][y];

  vector<vector<int>> ans((h + 2) * (w + 2),
                          vector<int>((h + 2) * (w + 2), inf));

  for (int x = 1; x <= h; ++x) {
    for (int y = 1; y <= w; ++y) {
      for (int d = 0; d < 4; ++d) {
        int dx = vector<int>({0, 0, -1, 1})[d];
        int dy = vector<int>({-1, 1, 0, 0})[d];
        int idx1 = (x) * (w + 2) + (y);
        int idx2 = (x + dx) * (w + 2) + (y + dy);
        if (vec[x][y] == '.' && vec[x + dx][y + dy] == '.')
          ans[idx1][idx2] = ans[idx2][idx1] = 1;
        ans[idx1][idx1] = 0;
      }
    }
  }

  for (int k = 1; k < (h + 2) * (w + 2); ++k) {
    for (int i = 1; i < (h + 2) * (w + 2); ++i) {
      for (int j = 1; j < (h + 2) * (w + 2); ++j) {
        int x1 = i / (w + 2);
        int y1 = i % (w + 2);
        int xm = k / (w + 2);
        int ym = k % (w + 2);
        int x2 = j / (w + 2);
        int y2 = j % (w + 2);
        int idx1 = x1 * (w + 2) + y1;
        int idxm = xm * (w + 2) + ym;
        int idx2 = x2 * (w + 2) + y2;
        if (ans[idx1][idxm] + ans[idxm][idx2] < ans[idx1][idx2])
          ans[idx1][idx2] = ans[idx2][idx1] = ans[idx1][idxm] + ans[idxm][idx2];
      }
    }
  }

  int vmax = 0;
  for (auto &seq : ans) {
    for (auto &v : seq) {
      if (v < inf && vmax < v)
        vmax = v;
    }
  }

  cout << vmax << endl;
}