prithivMLmods/Coder-Stat huggingface dataset:
Problem p02803 (C++)

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

const int INF = 1e8;
int H, W;
int blankMap[20][20];

vector<pair<int, int>> steps = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int bfs(int initX, int initY) {
  int highestCost = 0;
  int simulateMap[20][20];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      simulateMap[i][j] = blankMap[i][j];
    }
  }

  queue<tuple<int, int, int>> route; // cost, x, y;

  route.push(make_tuple(0, initX, initY));

  while (!route.empty()) {
    int cost, x, y;
    tie(cost, x, y) = route.front();
    route.pop();
    simulateMap[y][x] = min(simulateMap[y][x], cost);
    highestCost = max(highestCost, cost);

    for (auto step : steps) {
      int dx = step.first;
      int dy = step.second;

      if (0 <= x + dx && x + dx < W && 0 <= y + dy && y + dy < H &&
          cost + 1 < simulateMap[y + dy][x + dx]) {
        route.push(make_tuple(cost + 1, x + dx, y + dy));
      }
    }
  }
  return highestCost;
}

int main() {
  cin >> H >> W;

  string S[H];
  for (int i = 0; i < H; i++) {
    cin >> S[i];
    for (int j = 0; j < W; j++) {
      blankMap[i][j] = (S[i][j] == '#') ? -1 : INF;
    }
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#')
        continue;

      ans = max(ans, bfs(j, i));
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int INF = 1e8;
int H, W;
int blankMap[20][20];

vector<pair<int, int>> steps = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int bfs(int initX, int initY) {
  int highestCost = 0;
  int simulateMap[20][20];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      simulateMap[i][j] = blankMap[i][j];
    }
  }

  queue<tuple<int, int, int>> route; // cost, x, y;

  route.push(make_tuple(0, initX, initY));

  while (!route.empty()) {
    int cost, x, y;
    tie(cost, x, y) = route.front();
    route.pop();

    if (simulateMap[y][x] <= cost)
      continue;

    simulateMap[y][x] = cost;
    highestCost = max(highestCost, cost);

    for (auto step : steps) {
      int dx = step.first;
      int dy = step.second;

      if (0 <= x + dx && x + dx < W && 0 <= y + dy && y + dy < H &&
          cost + 1 < simulateMap[y + dy][x + dx]) {
        route.push(make_tuple(cost + 1, x + dx, y + dy));
      }
    }
  }
  return highestCost;
}

int main() {
  cin >> H >> W;

  string S[H];
  for (int i = 0; i < H; i++) {
    cin >> S[i];
    for (int j = 0; j < W; j++) {
      blankMap[i][j] = (S[i][j] == '#') ? -1 : INF;
    }
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#')
        continue;

      ans = max(ans, bfs(j, i));
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02803 (C++)

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

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  int ans = 0;
  rep(i, H) {
    rep(j, W) {
      if (S[i][j] == '#')
        continue;
      Graph dist(H, vector<int>(W, -1));
      queue<pair<int, int>> que;

      dist[i][j] = 0;
      que.push(make_pair(i, j));

      while (!que.empty()) {
        int x = que.front().first;
        int y = que.front().second;
        rep(dir, 4) {
          int n_x = x + dx[dir];
          int n_y = y + dy[dir];
          if (n_x < 0 || n_x >= H || n_y < 0 || n_y >= W)
            continue;
          if (S[n_x][n_y] == '#')
            continue;
          if (dist[n_x][n_y] == -1) {
            que.push(make_pair(n_x, n_y));
            dist[n_x][n_y] = dist[x][y] + 1;
          }
        }
      }
      rep(ix, H) {
        rep(jy, W) {
          if (dist[ix][jy] == -1)
            continue;
          ans = max(ans, dist[ix][jy]);
        }
      }
    }
  }
  cout << ans << endl;
}


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

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  rep(i, H) cin >> S[i];

  int ans = 0;
  rep(i, H) {
    rep(j, W) {
      if (S[i][j] == '#')
        continue;
      Graph dist(H, vector<int>(W, -1));
      queue<pair<int, int>> que;

      dist[i][j] = 0;
      que.push(make_pair(i, j));

      while (!que.empty()) {
        int x = que.front().first;
        int y = que.front().second;
        que.pop();
        rep(dir, 4) {
          int n_x = x + dx[dir];
          int n_y = y + dy[dir];
          if (n_x < 0 || n_x >= H || n_y < 0 || n_y >= W)
            continue;
          if (S[n_x][n_y] == '#')
            continue;
          if (dist[n_x][n_y] == -1) {
            que.push(make_pair(n_x, n_y));
            dist[n_x][n_y] = dist[x][y] + 1;
          }
        }
      }
      rep(ix, H) {
        rep(jy, W) {
          if (dist[ix][jy] == -1)
            continue;
          ans = max(ans, dist[ix][jy]);
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using xy = pair<int, int>;
using Graph = vector<vector<xy>>;

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> S(H);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char x;
      cin >> x;
      S.at(i).push_back(x);
    }
  }

  int dh[4] = {1, 0, -1, 0};
  int dw[4] = {0, 1, 0, -1};

  int Ans = -1;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S.at(i).at(j) != '#') {
        vector<vector<int>> d(H, vector<int>(W, -1));
        queue<xy> que;
        d.at(i).at(j) = 0;
        xy p = make_pair(i, j);
        que.push(p);
        int An = 0;
        while (!que.empty()) {
          p = que.front();
          que.pop();
          for (int k = 0; k < 4; i++) {
            int nh = p.first + dh[k];
            int nw = p.second + dw[k];
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) {
              continue;
            }
            if (S.at(nh).at(nw) == '#') {
              continue;
            }
            if (d.at(nh).at(nw) >= 0) {
              continue;
            }
            d.at(nh).at(nw) = d.at(p.first).at(p.second) + 1;
            An = d.at(nh).at(nw);
            que.push(make_pair(nh, nw));
          }
        }
        if (An > Ans) {
          Ans = An;
        }
      }
    }
  }
  cout << Ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using xy = pair<int, int>;
using Graph = vector<vector<xy>>;

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> S(H);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char x;
      cin >> x;
      S.at(i).push_back(x);
    }
  }

  int dh[4] = {1, 0, -1, 0};
  int dw[4] = {0, 1, 0, -1};

  int Ans = -1;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S.at(i).at(j) != '#') {
        vector<vector<int>> d(H, vector<int>(W, -1));
        queue<xy> que;
        d.at(i).at(j) = 0;
        xy p = make_pair(i, j);
        que.push(p);
        int An = 0;
        while (!que.empty()) {
          p = que.front();
          que.pop();
          for (int k = 0; k < 4; k++) {
            int nh = p.first + dh[k];
            int nw = p.second + dw[k];
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) {
              continue;
            }
            if (S.at(nh).at(nw) == '#') {
              continue;
            }
            if (d.at(nh).at(nw) >= 0) {
              continue;
            }
            d.at(nh).at(nw) = d.at(p.first).at(p.second) + 1;
            An = d.at(nh).at(nw);
            que.push(make_pair(nh, nw));
          }
        }
        if (An > Ans) {
          Ans = An;
        }
      }
    }
  }
  cout << Ans << endl;
}
Problem p02803 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> S(h);
  rep(i, h) cin >> S.at(i);

  vector<int> dx = {0, -1, 0, 1};
  vector<int> dy = {1, 0, -1, 0};
  deque<pair<int, int>> next_masu;

  int ans = 1;
  rep(i, h) {
    rep(j, w) {
      if (S.at(i).at(j) == '#')
        continue;
      vector<vector<int>> cnt(h, vector<int>(w, 1000000000));
      cnt.at(i).at(j) = 0;
      rep(k, 4) {
        int n_i, n_j;
        n_i = i + dy[k];
        n_j = j + dx[k];
        if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1)
          continue;
        if (S.at(n_i).at(n_j) == '#')
          continue;
        next_masu.push_back(make_pair(n_i, n_j));
        cnt.at(n_i).at(n_j) = 1;
      }

      while (!next_masu.empty()) {
        auto now_masu = next_masu.front();
        next_masu.pop_front();
        rep(k, 4) {
          int n_i, n_j;
          n_i = now_masu.first + dy[k];
          n_j = now_masu.second + dx[k];
          if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1)
            continue;
          if (S.at(n_i).at(n_j) == '#')
            continue;
          if (cnt.at(n_i).at(n_j) <= cnt.at(now_masu.first).at(now_masu.second))
            continue;
          next_masu.push_back(make_pair(n_i, n_j));
          cnt.at(n_i).at(n_j) = cnt.at(now_masu.first).at(now_masu.second) + 1;

          ans = max(ans, cnt.at(n_i).at(n_j));
        }
      }
    }
  }
  cout << ans << endl;
}

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> S(h);
  rep(i, h) cin >> S.at(i);

  vector<int> dx = {0, -1, 0, 1};
  vector<int> dy = {1, 0, -1, 0};
  deque<pair<int, int>> next_masu;

  int ans = 1;
  rep(i, h) {
    rep(j, w) {
      if (S.at(i).at(j) == '#')
        continue;
      vector<vector<int>> cnt(h, vector<int>(w, 1000000000));
      cnt.at(i).at(j) = 0;
      rep(k, 4) {
        int n_i, n_j;
        n_i = i + dy[k];
        n_j = j + dx[k];
        if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1)
          continue;
        if (S.at(n_i).at(n_j) == '#')
          continue;
        next_masu.push_back(make_pair(n_i, n_j));
        cnt.at(n_i).at(n_j) = 1;
      }

      while (!next_masu.empty()) {
        auto now_masu = next_masu.front();
        next_masu.pop_front();
        rep(k, 4) {
          int n_i, n_j;
          n_i = now_masu.first + dy[k];
          n_j = now_masu.second + dx[k];
          if (n_i < 0 || n_i > h - 1 || n_j < 0 || n_j > w - 1)
            continue;
          if (S.at(n_i).at(n_j) == '#')
            continue;
          if (cnt.at(n_i).at(n_j) <=
              cnt.at(now_masu.first).at(now_masu.second) + 1)
            continue;
          next_masu.push_back(make_pair(n_i, n_j));
          cnt.at(n_i).at(n_j) = cnt.at(now_masu.first).at(now_masu.second) + 1;

          ans = max(ans, cnt.at(n_i).at(n_j));
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02803 (C++)

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

int ans = 0;
int H, W;

void bfs(vector<string> &S, int sy, int sx, int ey, int ex) {
  vector<int> vx = {0, 1, 0, -1};
  vector<int> vy = {1, 0, -1, 0};
  queue<int> quex;
  queue<int> quey;
  quex.push(sx);
  quey.push(sy);
  vector<vector<int>> dis(H, vector<int>(W, 1e9));
  dis.at(sy).at(sx) = 0;
  while (!quex.empty() && !quey.empty()) {
    int x = quex.front();
    int y = quey.front();
    quex.pop();
    quey.pop();
    for (int i = 0; i < 4; i++) {
      int next_x = x + vx.at(i);
      int next_y = y + vy.at(i);
      if (next_x < 0 || next_x >= W || next_y < 0 || next_y >= H)
        continue;
      if (S.at(next_y).at(next_x) == '#')
        continue;
      if (dis.at(next_y).at(next_x) != 1e9)
        continue;
      quex.push(next_x);
      quey.push(next_y);
      dis.at(next_y).at(next_x) = dis.at(y).at(x) + 1;
    }
  }
  if (dis.at(ey).at(ex) != 1e9)
    ans = max(ans, dis.at(ey).at(ex));
}

int main() {
  cin >> H >> W;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S.at(i);

  for (int st_i = 0; st_i < H; st_i++) {
    for (int st_j = 0; st_j < W; st_j++) {
      for (int en_i = 0; en_i < H; en_i++) {
        for (int en_j = 0; en_j < W; en_j++) {
          if (S.at(st_i).at(st_j) == '#')
            continue;
          if (S.at(en_i).at(en_j) == '#')
            continue;
          bfs(S, st_i, st_j, en_i, en_j);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int ans = 0;
int H, W;

void bfs(vector<string> &S, int sy, int sx, int ey, int ex) {
  vector<int> vx = {0, 1, 0, -1};
  vector<int> vy = {1, 0, -1, 0};
  queue<int> quex;
  queue<int> quey;
  quex.push(sx);
  quey.push(sy);
  vector<vector<int>> dis(H, vector<int>(W, 1e9));
  dis.at(sy).at(sx) = 0;
  while (!quex.empty() && !quey.empty()) {
    int x = quex.front();
    int y = quey.front();
    quex.pop();
    quey.pop();
    for (int i = 0; i < 4; i++) {
      int next_x = x + vx.at(i);
      int next_y = y + vy.at(i);
      if (next_x < 0 || next_x >= W || next_y < 0 || next_y >= H)
        continue;
      if (S.at(next_y).at(next_x) == '#')
        continue;
      if (dis.at(next_y).at(next_x) != 1e9)
        continue;
      quex.push(next_x);
      quey.push(next_y);
      dis.at(next_y).at(next_x) = dis.at(y).at(x) + 1;
    }
  }
  if (dis.at(ey).at(ex) != 1e9)
    ans = max(ans, dis.at(ey).at(ex));
}

int main() {
  cin >> H >> W;
  vector<string> S(H);
  for (int i = 0; i < H; i++)
    cin >> S.at(i);

  for (int st_i = 0; st_i < H; st_i++) {
    for (int st_j = 0; st_j < W; st_j++) {
      for (int en_i = 0; en_i < H; en_i++) {
        for (int en_j = st_j; en_j < W; en_j++) {
          if (S.at(st_i).at(st_j) == '#')
            continue;
          if (S.at(en_i).at(en_j) == '#')
            continue;
          bfs(S, st_i, st_j, en_i, en_j);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 5;
int mp[23][23];
char str[23][23];
int book[23][23];
int ans;
int gx, gy;
int dx[5] = {-1, 1, 0, 0};
int dy[5] = {0, 0, -1, 1};
int n, m;
int qx[4005], qy[4005], step[4005];
int bfs(int sx, int sy, int gx, int gy) {

  memset(book, 0, sizeof(book));
  int head = 0, tail = 0;
  book[sx][sy] = 1;
  qx[head] = sx, qy[tail] = sy, step[tail++] = 0;
  while (head < tail) {
    int x = qx[head], y = qy[head];
    if (x == gx && y == gy)
      return step[head];
    int nowx, nowy;
    for (int i = 0; i < 4; i++) {
      nowx = x + dx[i], nowy = y + dy[i];
      if (nowx < 0 || nowx >= n || nowy < 0 || nowy >= m)
        continue;
      if (book[nowx][nowy] == 0 && mp[nowx][nowy] == 0) {
        qx[tail] = nowx, qy[tail] = nowy, step[tail] = step[head] + 1;
        tail++;
      }
    }
    head++;
  }
  return 0;
}
int main() {
  int i, j;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    scanf("%s", str[i]);
    for (j = 0; j < m; j++) {
      mp[i][j] = str[i][j] == '#';
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (mp[i][j])
        continue;
      for (int l = 0; l < n; l++) {
        for (int k = 0; k < m; k++) {
          if (mp[l][k])
            continue;
          ans = max(ans, bfs(i, j, l, k));
          //                    printf("%d %d %d %d %d\n", i, j, l, k,
          //                    bfs(i,j,l,k));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
const int maxn = 1e5 + 5;
int mp[23][23];
char str[23][23];
int book[23][23];
int ans;
int gx, gy;
int dx[5] = {-1, 1, 0, 0};
int dy[5] = {0, 0, -1, 1};
int n, m;
int qx[4005], qy[4005], step[4005];
int bfs(int sx, int sy, int gx, int gy) {

  memset(book, 0, sizeof(book));
  int head = 0, tail = 0;
  book[sx][sy] = 1;
  qx[head] = sx, qy[tail] = sy, step[tail++] = 0;
  while (head < tail) {
    int x = qx[head], y = qy[head];
    if (x == gx && y == gy)
      return step[head];
    int nowx, nowy;
    for (int i = 0; i < 4; i++) {
      nowx = x + dx[i], nowy = y + dy[i];
      if (nowx < 0 || nowx >= n || nowy < 0 || nowy >= m)
        continue;
      if (book[nowx][nowy] == 0 && mp[nowx][nowy] == 0) {
        book[nowx][nowy] = 1;
        qx[tail] = nowx, qy[tail] = nowy, step[tail] = step[head] + 1;
        tail++;
      }
    }
    head++;
  }
  return 0;
}
int main() {
  int i, j;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    scanf("%s", str[i]);
    for (j = 0; j < m; j++) {
      mp[i][j] = str[i][j] == '#';
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (mp[i][j])
        continue;
      for (int l = 0; l < n; l++) {
        for (int k = 0; k < m; k++) {
          if (mp[l][k])
            continue;
          ans = max(ans, bfs(i, j, l, k));
          //                    printf("%d %d %d %d %d\n", i, j, l, k,
          //                    bfs(i,j,l,k));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int I[20][20];
int D[400][400];
int main() {
  ios::sync_with_stdio(false);
  int H, W;
  string S[20];
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int N = 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.') {
        I[i][j] = N++;
      }
    }
  }
  int INF = 100000;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      D[i][j] = INF;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.') {
        int k = I[i][j];
        D[k][k] = 0;
        if (i > 0) {
          int l = I[i - 1][j];
          D[k][l] = D[l][k] = 1;
        }
        if (j > 0) {
          int l = I[i][j - 1];
          D[k][l] = D[l][k] = 1;
        }
        if (i < H - 1) {
          int l = I[i + 1][j];
          D[k][l] = D[l][k] = 1;
        }
        if (j < W - 1) {
          int l = I[i][j + 1];
          D[k][l] = D[l][k] = 1;
        }
      }
    }
  }
  for (int k = 1; k < N; k++) {
    for (int i = 1; i < N; i++) {
      for (int j = 1; j < N; j++) {
        D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
      }
    }
  }
  int ret = 0;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < N; j++) {
      if (D[i][j] < INF) {
        ret = max(ret, D[i][j]);
      }
    }
  }
  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int I[20][20];
int D[405][405];
int main() {
  ios::sync_with_stdio(false);
  int H, W;
  string S[20];
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int N = 1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.') {
        I[i][j] = N++;
      }
    }
  }
  int INF = 100000;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      D[i][j] = INF;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.') {
        int k = I[i][j];
        D[k][k] = 0;
        if (i > 0) {
          int l = I[i - 1][j];
          D[k][l] = D[l][k] = 1;
        }
        if (j > 0) {
          int l = I[i][j - 1];
          D[k][l] = D[l][k] = 1;
        }
        if (i < H - 1) {
          int l = I[i + 1][j];
          D[k][l] = D[l][k] = 1;
        }
        if (j < W - 1) {
          int l = I[i][j + 1];
          D[k][l] = D[l][k] = 1;
        }
      }
    }
  }
  for (int k = 1; k < N; k++) {
    for (int i = 1; i < N; i++) {
      for (int j = 1; j < N; j++) {
        D[i][j] = min(D[i][j], D[i][k] + D[k][j]);
      }
    }
  }
  int ret = 0;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < N; j++) {
      if (D[i][j] < INF) {
        ret = max(ret, D[i][j]);
      }
    }
  }
  cout << ret << endl;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
int h, w;
std::vector<std::vector<bool>> map;

struct pairs {
  int first;
  int second;
  int depth;
};

std::vector<std::pair<int, int>> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
inline int bfs(int start_h, int start_w) {
  int aaa = 0;
  std::vector<std::vector<bool>> seen(h + 2, std::vector<bool>(w + 2));
  std::queue<pairs> que;
  seen[start_h][start_w] = true;
  que.push({start_h, start_w, 0});
  while (que.size()) {
    auto cur = que.front();
    que.pop();
    aaa = std::max(cur.depth, aaa);
    for (int i = 0; i < 4; i++) {
      if (!seen[cur.first + dir[i].first][cur.second + dir[i].second] &&
          map[cur.first + dir[i].first][cur.second + dir[i].second]) {
        que.push({cur.first + dir[i].first, cur.second + dir[i].second,
                  cur.depth + 1});
        seen[cur.first][cur.second] = true;
      }
    }
  }
  return aaa;
}
int main() {
  std::cin >> h >> w;
  map.resize(h + 2, std::vector<bool>(w + 2));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char a;
      std::cin >> a;
      map[i][j] = a == '.';
    }
  }
  int max = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (map[i][j])
        max = std::max(bfs(i, j), max);
    }
  }
  std::cout << max << std::endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
int h, w;
std::vector<std::vector<bool>> map;

struct pairs {
  int first;
  int second;
  int depth;
};

std::vector<std::pair<int, int>> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
inline int bfs(int start_h, int start_w) {
  int aaa = 0;
  std::vector<std::vector<bool>> seen(h + 2, std::vector<bool>(w + 2));
  std::queue<pairs> que;
  seen[start_h][start_w] = true;
  que.push({start_h, start_w, 0});
  while (que.size()) {
    auto cur = que.front();
    que.pop();
    aaa = std::max(cur.depth, aaa);
    for (int i = 0; i < 4; i++) {
      if (!seen[cur.first + dir[i].first][cur.second + dir[i].second] &&
          map[cur.first + dir[i].first][cur.second + dir[i].second]) {
        que.push({cur.first + dir[i].first, cur.second + dir[i].second,
                  cur.depth + 1});
        seen[cur.first + dir[i].first][cur.second + dir[i].second] = true;
      }
    }
  }
  return aaa;
}
int main() {
  std::cin >> h >> w;
  map.resize(h + 2, std::vector<bool>(w + 2));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char a;
      std::cin >> a;
      map[i][j] = a == '.';
    }
  }
  int max = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (map[i][j])
        max = std::max(bfs(i, j), max);
    }
  }
  std::cout << max << std::endl;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using ll = long long;
using lli = long long int;

lli h, w;
std::vector<std::string> vec;
std::vector<std::vector<lli>> visited;
std::string s;
std::vector<int> x_s;
std::vector<int> y_s;

int main() {
  lli ans = 0;
  x_s = {0, 0, 1, -1};
  y_s = {1, -1, 0, 0};
  std::cin >> h >> w;
  vec.assign(h, "a");
  for (int i = 0; i < h; i++) {
    std::cin >> s;
    vec[i] = s;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vec[i][j] == '.') {
        visited.assign(h, std::vector<lli>(w, 0));
        std::queue<std::vector<lli>> q;
        q.push({i, j, 0});
        while (!q.empty()) {
          auto point = q.front();
          lli x = point[1];
          lli y = point[0];
          if (point[2] > ans) {
            ans = point[2];
          }
          visited[y][x] = 1;
          for (int k = 0; k < 4; k++) {
            if (y + y_s[k] >= 0 && x + x_s[k] >= 0 && y + y_s[k] < h &&
                x + x_s[k] < w && vec[y + y_s[k]][x + x_s[k]] != '#' &&
                visited[y + y_s[k]][x + x_s[k]] != 1) {
              q.push({y + y_s[k], x + x_s[k], point[2] + 1});
            }
          }
          q.pop();
        }
      }
    }
  }
  std::cout << ans << std::endl;
}


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

using ll = long long;
using lli = long long int;

lli h, w;
std::vector<std::string> vec;
std::vector<std::vector<lli>> visited;
std::string s;
std::vector<int> x_s;
std::vector<int> y_s;

int main() {
  lli ans = 0;
  x_s = {0, 0, 1, -1};
  y_s = {1, -1, 0, 0};
  std::cin >> h >> w;
  vec.assign(h, "a");
  for (int i = 0; i < h; i++) {
    std::cin >> s;
    vec[i] = s;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (vec[i][j] == '.') {
        visited.assign(h, std::vector<lli>(w, 0));
        std::queue<std::vector<lli>> q;
        q.push({i, j, 0});
        while (!q.empty()) {
          auto point = q.front();
          lli x = point[1];
          lli y = point[0];
          if (visited[y][x] == 1) {
            q.pop();
            continue;
          }
          if (point[2] > ans) {
            ans = point[2];
          }
          visited[y][x] = 1;
          for (int k = 0; k < 4; k++) {
            if (y + y_s[k] >= 0 && x + x_s[k] >= 0 && y + y_s[k] < h &&
                x + x_s[k] < w && vec[y + y_s[k]][x + x_s[k]] != '#' &&
                visited[y + y_s[k]][x + x_s[k]] != 1) {
              q.push({y + y_s[k], x + x_s[k], point[2] + 1});
            }
          }
          q.pop();
        }
      }
    }
  }
  std::cout << ans << std::endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
// D - Maze Master
#include <bits/stdc++.h>
using namespace std;
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
using vi = vector<int>;
using vvi = vector<vi>;

// BFS (幅優先探索)
// 文字列迷路Sについて、S[sh][sw]から各マスまでの最短コストをdist[h][w]に求める
#define HMAX 20
#define WMAX 20
#define INF 100000
vvi dist(HMAX + 1,
         vi(WMAX + 1,
            INF)); // 外周を壁で囲む (-1:未訪問, 0~<INF:訪問済&コスト, INF:壁)
void bfs(int sh, int sw) {
  int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
  dist[sh][sw] = 0; // 最短コスト
  queue<int> qh, qw;
  qh.push(sh), qw.push(sw);
  while (!qh.empty()) {
    int ph = qh.front(), pw = qw.front();
    qh.pop(), qw.pop();
    rp(k, 0, 4) { // 四方向
      int h = ph + dy[k], w = pw + dx[k];
      if (dist[h][w] > -1)
        continue;                    // 壁・訪問済み
      dist[h][w] = dist[ph][pw] + 1; // 最短コスト
      qh.push(h), qw.push(w);
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  rp(h, 1, H + 1) {
    string s;
    cin >> s;
    rp(w, 1, W + 1) if (s[w - 1] == '.') dist[h][w] = -1; // 未訪問
  }

  int ans = 0;
  vvi dist_temp = dist;
  rp(h, 1, H + 1) {
    rp(w, 1, W + 1) {
      if (dist_temp[h][w] < 0) {
        dist = dist_temp;
        bfs(h, w);
        for (vi v : dist)
          for (int d : v)
            ans = max(ans, d == INF ? -1 : d); // 壁は未訪問とみなす
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
// D - Maze Master
#include <bits/stdc++.h>
using namespace std;
#define rp(i, s, e) for (int i = (s); i < (e); ++i)
using vi = vector<int>;
using vvi = vector<vi>;

// BFS (幅優先探索)
// 文字列迷路Sについて、S[sh][sw]から各マスまでの最短コストをdist[h][w]に求める
#define HMAX 20
#define WMAX 20
#define INF 100000
vvi dist(HMAX + 2,
         vi(WMAX + 2,
            INF)); // 外周を壁で囲む (-1:未訪問, 0~<INF:訪問済&コスト, INF:壁)
void bfs(int sh, int sw) {
  int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
  dist[sh][sw] = 0; // 最短コスト
  queue<int> qh, qw;
  qh.push(sh), qw.push(sw);
  while (!qh.empty()) {
    int ph = qh.front(), pw = qw.front();
    qh.pop(), qw.pop();
    rp(k, 0, 4) { // 四方向
      int h = ph + dy[k], w = pw + dx[k];
      if (dist[h][w] > -1)
        continue;                    // 壁・訪問済み
      dist[h][w] = dist[ph][pw] + 1; // 最短コスト
      qh.push(h), qw.push(w);
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  rp(h, 1, H + 1) {
    string s;
    cin >> s;
    rp(w, 1, W + 1) if (s[w - 1] == '.') dist[h][w] = -1; // 未訪問
  }

  int ans = 0;
  vvi dist_temp = dist;
  rp(h, 1, H + 1) {
    rp(w, 1, W + 1) {
      if (dist_temp[h][w] < 0) {
        dist = dist_temp;
        bfs(h, w);
        for (vi v : dist)
          for (int d : v)
            ans = max(ans, d == INF ? -1 : d); // 壁は未訪問とみなす
      }
    }
  }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i, a, n) for (int i = a; i < n; i++)

int n, m;
char a[21][21];
int d[401];
int dist[21][21];
int used[21][21];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int res = -2e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  cin >> n >> m;
  forn(i, 0, n) forn(j, 0, m) cin >> a[i][j];
  forn(i, 0, n) {
    forn(j, 0, m) {
      memset(dist, -1, sizeof(dist));
      memset(used, 0, sizeof(used));
      if (a[i][j] == '#')
        continue;
      int qh = 0, qt = 0;
      d[qt++] = i;
      d[qt++] = j;
      used[i][j] = 1;
      dist[i][j] = 0;
      while (qh < qt) {
        int x = d[qh++];
        int y = d[qh++];
        forn(k, 0, 4) {
          int nx = x + dx[k];
          int ny = y + dy[k];
          if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '#')
            continue;
          if (dist[nx][ny] == -1) {
            d[qt++] = nx;
            d[qt++] = ny;
            dist[nx][ny] = dist[x][y] + 1;
          }
        }
      }
      forn(r, 0, n) forn(c, 0, m) res = max(res, dist[r][c]);
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i, a, n) for (int i = a; i < n; i++)

int n, m;
char a[21][21];
int d[40001];
int dist[21][21];
int used[21][21];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int res = -2e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  cin >> n >> m;
  forn(i, 0, n) forn(j, 0, m) cin >> a[i][j];
  forn(i, 0, n) {
    forn(j, 0, m) {
      memset(dist, -1, sizeof(dist));
      memset(used, 0, sizeof(used));
      if (a[i][j] == '#')
        continue;
      int qh = 0, qt = 0;
      d[qt++] = i;
      d[qt++] = j;
      used[i][j] = 1;
      dist[i][j] = 0;
      while (qh < qt) {
        int x = d[qh++];
        int y = d[qh++];
        forn(k, 0, 4) {
          int nx = x + dx[k];
          int ny = y + dy[k];
          if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '#')
            continue;
          if (dist[nx][ny] == -1) {
            d[qt++] = nx;
            d[qt++] = ny;
            dist[nx][ny] = dist[x][y] + 1;
          }
        }
      }
      forn(r, 0, n) forn(c, 0, m) res = max(res, dist[r][c]);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02803 (C++)

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

using namespace std;

char maze[23][23];
bool visited[23][23];
int level[23][23];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int h, w;
int hx, hy;

int bfs(int fx, int fy) {
  memset(visited, 0, sizeof(visited));
  memset(level, 0, sizeof(level));
  int mx = 0;
  queue<pair<int, int>> q;
  q.push({fx, fy});
  visited[fx][fy] = 1;
  level[fx][fy] = 0;

  while (!q.empty()) {
    int px = q.front().first;
    int py = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = px + dx[i];
      int y = py + dy[i];
      if (!visited[x][y] && maze[x][y] == '.' && x >= 0 && y >= 0 && x < h &&
          y < w) {
        level[x][y] = level[px][py] + 1;
        visited[x][y] = 1;
        q.push({x, y});
        if (mx < level[x][y]) {
          mx = level[x][y];
          hx = x;
          hy = y;
        }
      }
    }
  }
  return mx;
}

int main() {

  cin >> h >> w;
  int x, y;
  bool f = 1;
  int ans = 0;
  queue<pair<int, int>> nodes;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> maze[i][j];
      if (f && maze[i][j] == '.') {
        nodes.push({i, j});
      }
    }
  }
  for (int i = 0; !nodes.empty(); i++) {
    x = nodes.front().first;
    y = nodes.front().second;
    ans = max(ans, bfs(x, y));
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

char maze[23][23];
bool visited[23][23];
int level[23][23];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int h, w;
int hx, hy;

int bfs(int fx, int fy) {
  memset(visited, 0, sizeof(visited));
  memset(level, 0, sizeof(level));
  int mx = 0;
  queue<pair<int, int>> q;
  q.push({fx, fy});
  visited[fx][fy] = 1;
  level[fx][fy] = 0;

  while (!q.empty()) {
    int px = q.front().first;
    int py = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int x = px + dx[i];
      int y = py + dy[i];
      if (!visited[x][y] && maze[x][y] == '.' && x >= 0 && y >= 0 && x < h &&
          y < w) {
        level[x][y] = level[px][py] + 1;
        visited[x][y] = 1;
        q.push({x, y});
        if (mx < level[x][y]) {
          mx = level[x][y];
          hx = x;
          hy = y;
        }
      }
    }
  }
  return mx;
}

int main() {

  cin >> h >> w;
  int x, y;
  bool f = 1;
  int ans = 0;
  queue<pair<int, int>> nodes;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> maze[i][j];
      if (f && maze[i][j] == '.') {
        nodes.push({i, j});
      }
    }
  }
  for (int i = 0; !nodes.empty(); i++) {
    x = nodes.front().first;
    y = nodes.front().second;
    nodes.pop();
    ans = max(ans, bfs(x, y));
  }

  cout << ans << endl;

  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int H, W;
vector<string> v;
int d[30][30];
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    v.push_back(S);
  }
  for (int i = 0; i < H * W; i++) {
    for (int j = 0; j < H * W; j++) {
      int ix = i % W, iy = i / W;
      int jx = j % W, jy = j / W;
      if (abs(ix - jx) + abs(iy - jy) == 1 && v[iy][ix] == '.' &&
          v[jy][jx] == '.') {
        d[i][j] = d[j][i] = 1;
      } else if (i == j) {
        d[i][j] = d[j][i] = 0;
      } else {
        d[i][j] = d[j][i] = INT_MAX / 3;
      }
    }
  }
  for (int k = 0; k < H * W; k++) {
    for (int i = 0; i < H * W; i++) {
      for (int j = 0; j < H * W; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (v[i][j] == '.') {
        for (int ii = 0; ii < H; ii++) {
          for (int jj = 0; jj < W; jj++) {
            if (v[ii][jj] == '.') {
              ans = max(ans, d[ii * W + jj][i * W + j]);
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int H, W;
vector<string> v;
int d[410][410];
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    v.push_back(S);
  }
  for (int i = 0; i < H * W; i++) {
    for (int j = 0; j < H * W; j++) {
      int ix = i % W, iy = i / W;
      int jx = j % W, jy = j / W;
      if (abs(ix - jx) + abs(iy - jy) == 1 && v[iy][ix] == '.' &&
          v[jy][jx] == '.') {
        d[i][j] = d[j][i] = 1;
      } else if (i == j) {
        d[i][j] = d[j][i] = 0;
      } else {
        d[i][j] = d[j][i] = INT_MAX / 3;
      }
    }
  }
  for (int k = 0; k < H * W; k++) {
    for (int i = 0; i < H * W; i++) {
      for (int j = 0; j < H * W; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (v[i][j] == '.') {
        for (int ii = 0; ii < H; ii++) {
          for (int jj = 0; jj < W; jj++) {
            if (v[ii][jj] == '.') {
              ans = max(ans, d[ii * W + jj][i * W + j]);
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
/*
 * ID: juryc
 * PROG: Maze Master
 * LANG: C++
 */
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> v(h);
  for (string &i : v)
    cin >> i;
  int val = h * w;
  vector<vector<int>> dp(val, vector<int>(val, 0x3f3f3f3f));
  for (int i = 0; i < h * w; i++)
    dp[i][i] = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] == '#')
        continue;
      int arx[] = {-1, 1, 0, 0};
      int ary[] = {0, 0, -1, 1};
      for (int k = 0; k < 4; k++) {
        int nx = i + arx[k], ny = j + ary[k];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (v[nx][ny] == '#')
          dp[i * h + j][nx * h + ny] = 0x3f3f3f3f;
        else
          dp[i * h + j][nx * h + ny] = 1;
      }
    }
  }
  for (int k = 0; k < val; k++) {
    for (int i = 0; i < val; i++) {
      for (int j = 0; j < val; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  int mx = 0;
  for (int i = 0; i < val; i++)
    for (int j = 0; j < val; j++)
      if (dp[i][j] != 0x3f3f3f3f)
        mx = max(mx, dp[i][j]);
  cout << mx << "\n";
  return 0;
}


Fixed submission:
/*
 * ID: juryc
 * PROG: Maze Master
 * LANG: C++
 */
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<string> v(h);
  for (string &i : v)
    cin >> i;
  int val = h * w;
  vector<vector<int>> dp(val, vector<int>(val, 0x3f3f3f3f));
  for (int i = 0; i < h * w; i++)
    dp[i][i] = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (v[i][j] == '#')
        continue;
      int arx[] = {-1, 1, 0, 0};
      int ary[] = {0, 0, -1, 1};
      for (int k = 0; k < 4; k++) {
        int nx = i + arx[k], ny = j + ary[k];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (v[nx][ny] == '#')
          dp[i * w + j][nx * w + ny] = 0x3f3f3f3f;
        else {
          dp[i * w + j][nx * w + ny] = 1;
        }
      }
    }
  }
  for (int k = 0; k < val; k++) {
    for (int i = 0; i < val; i++) {
      for (int j = 0; j < val; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  int mx = 0;
  for (int i = 0; i < val; i++)
    for (int j = 0; j < val; j++)
      if (dp[i][j] != 0x3f3f3f3f)
        mx = max(mx, dp[i][j]);
  cout << mx << "\n";
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;

int main() {
  int h, w;
  cin >> h >> w;
  int n = h * w;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int d[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = INF;
    }
    d[i][i] = 0;
  }
  const int dx[] = {-1, 0, 0, 1};
  const int dy[] = {0, -1, 1, 0};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k];
        int ny = j + dy[k];
        if (nx < 0 || h <= nx || ny < 0 || w <= ny || s[nx][ny] == '#')
          continue;
        d[h * i + j][h * nx + ny] = 1;
      }
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (d[i][j] != INF) {
        ans = max(ans, d[i][j]);
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;

int main() {
  int h, w;
  cin >> h >> w;
  int n = h * w;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int d[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = INF;
    }
    d[i][i] = 0;
  }
  const int dx[] = {-1, 0, 0, 1};
  const int dy[] = {0, -1, 1, 0};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      for (int k = 0; k < 4; k++) {
        int nx = i + dx[k];
        int ny = j + dy[k];
        if (nx < 0 || h <= nx || ny < 0 || w <= ny || s[nx][ny] == '#')
          continue;
        d[w * i + j][w * nx + ny] = 1;
      }
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (d[i][j] != INF) {
        ans = max(ans, d[i][j]);
      }
    }
  }
  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;

typedef pair<int, int> P;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, sx, sy;
  cin >> H >> W;
  char s[H][W];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> s[i][j];
    }
  }

  int ans = 0;
  for (int m = 0; m < H; m++) {
    for (int n = 0; n < W; n++) {
      if (s[m][n] == '#')
        continue;
      sx = m;
      sy = n;
      int dist[25][25];
      fill((int *)dist, (int *)(dist + H * W), INF);
      queue<P> que;
      dist[sx][sy] = 0;
      que.push(P(sx, sy));
      while (!que.empty()) {
        P p = que.front();
        que.pop();
        int x = p.first;
        int y = p.second;

        for (int i = 0; i < 4; ++i) {
          int nx = x + dx[i], ny = y + dy[i];
          if (nx < 0 || nx >= H || ny < 0 || ny >= W)
            continue;
          if (s[nx][ny] == '#')
            continue;

          if (dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            que.push(P(nx, ny));
            ans = max(ans, dist[nx][ny]);
          }
        }
      }
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;

typedef pair<int, int> P;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, sx, sy;
  cin >> H >> W;
  char s[H][W];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> s[i][j];
    }
  }

  int ans = 0;
  for (int m = 0; m < H; m++) {
    for (int n = 0; n < W; n++) {
      if (s[m][n] == '#')
        continue;
      sx = m;
      sy = n;
      int dist[25][25];
      fill((int *)dist, (int *)(dist + H), INF);
      queue<P> que;
      dist[sx][sy] = 0;
      que.push(P(sx, sy));
      while (!que.empty()) {
        P p = que.front();
        que.pop();
        int x = p.first;
        int y = p.second;

        for (int i = 0; i < 4; ++i) {
          int nx = x + dx[i], ny = y + dy[i];
          if (nx < 0 || nx >= H || ny < 0 || ny >= W)
            continue;
          if (s[nx][ny] == '#')
            continue;

          if (dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            que.push(P(nx, ny));
            ans = max(ans, dist[nx][ny]);
          }
        }
      }
    }
  }

  cout << ans << endl;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;

int n, m;
string g[21];
bool vis[21][21];

bool valid(int x, int y) {
  return 0 <= x && x < n && 0 <= y && y < m && !vis[x][y] && g[x][y] == '.';
}

int bfs(int x, int y) {
  int ans = 0;
  queue<pair<pii, int>> q;
  q.push({{x, y}, 0});
  while (!q.empty()) {
    pii atual = q.front().F;
    vis[atual.F][atual.S] = true;
    int d = q.front().S;
    q.pop();
    if (valid(atual.F + 1, atual.S))
      q.push({{atual.F + 1, atual.S}, d + 1});
    if (valid(atual.F - 1, atual.S))
      q.push({{atual.F - 1, atual.S}, d + 1});
    if (valid(atual.F, atual.S + 1))
      q.push({{atual.F, atual.S + 1}, d + 1});
    if (valid(atual.F, atual.S - 1))
      q.push({{atual.F, atual.S - 1}, d + 1});
    ans = max(ans, d);
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  FOR(i, n) cin >> g[i];

  int ans = 0;
  FOR(i, n) {
    FOR(j, m) {
      if (g[i][j] == '.') {
        memset(vis, false, sizeof(vis));
        ans = max(ans, bfs(i, j));
      }
    }
  }

  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;

int n, m;
string g[21];
bool vis[21][21];

bool valid(int x, int y) {
  return 0 <= x && x < n && 0 <= y && y < m && !vis[x][y] && g[x][y] == '.';
}

int bfs(int x, int y) {
  int ans = 0;
  queue<pair<pii, int>> q;
  q.push({{x, y}, 0});
  while (!q.empty()) {
    pii atual = q.front().F;
    // cout << atual.F << ' ' << atual.S << '\n';
    if (vis[atual.F][atual.S]) {
      q.pop();
      continue;
    }
    vis[atual.F][atual.S] = true;
    int d = q.front().S;
    q.pop();
    if (valid(atual.F + 1, atual.S))
      q.push({{atual.F + 1, atual.S}, d + 1});
    if (valid(atual.F - 1, atual.S))
      q.push({{atual.F - 1, atual.S}, d + 1});
    if (valid(atual.F, atual.S + 1))
      q.push({{atual.F, atual.S + 1}, d + 1});
    if (valid(atual.F, atual.S - 1))
      q.push({{atual.F, atual.S - 1}, d + 1});
    ans = max(ans, d);
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  FOR(i, n) cin >> g[i];

  int ans = 0;
  FOR(i, n) {
    FOR(j, m) {
      if (g[i][j] == '.') {
        memset(vis, false, sizeof(vis));
        ans = max(ans, bfs(i, j));
      }
    }
  }

  cout << ans << '\n';

  return 0;
}
Problem p02803 (Python)

Buggy submission (status: Runtime Error):
from scipy.sparse.csgraph import dijkstra, csgraph_from_dense
import numpy as np

H, W = map(int, input().split())
S = [input() for _ in range(H)]

v_id_dict = {}
dista = np.ones((20, 20)) * np.inf

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            v_id_dict[i, j] = len(v_id_dict)

            if (i > 0) and (S[i - 1][j] == "."):
                dista[v_id_dict[(i, j)], v_id_dict[(i - 1, j)]] = 1

            if (j > 0) and (S[i][j - 1] == "."):
                dista[v_id_dict[(i, j)], v_id_dict[(i, j - 1)]] = 1

dist_mat = dijkstra(
    csgraph_from_dense(dista[: len(v_id_dict), : len(v_id_dict)]),
    directed=False,
    unweighted=False,
)

print(int(dist_mat.max()))


Fixed submission:
from scipy.sparse.csgraph import dijkstra, csgraph_from_dense
import numpy as np

H, W = map(int, input().split())
S = [input() for _ in range(H)]

v_id_dict = {}
dista = np.ones((400, 400)) * np.inf

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            v_id_dict[i, j] = len(v_id_dict)

            if (i > 0) and (S[i - 1][j] == "."):
                dista[v_id_dict[(i, j)], v_id_dict[(i - 1, j)]] = 1

            if (j > 0) and (S[i][j - 1] == "."):
                dista[v_id_dict[(i, j)], v_id_dict[(i, j - 1)]] = 1

dist_mat = dijkstra(
    csgraph_from_dense(dista[: len(v_id_dict), : len(v_id_dict)]),
    directed=False,
    unweighted=False,
)

print(int(dist_mat.max()))