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

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

int di[] = {-1, 0, 1, 0};
int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  vector<vector<int>> dist(h, vector<int>(w, 1e9));
  vector<pair<int, int>> p;
  rep(i, h) cin >> a[i];
  rep(i, h) rep(j, w) if (a[i][j] == '#') p.push_back(make_pair(i, j));
  queue<pair<int, int>> q;
  auto update = [&](int i, int j, int x) {
    if (dist[i][j] < x)
      return;
    dist[i][j] = x;
    q.push(make_pair(i, j));
  };
  rep(i, p.size()) { update(p[i].first, p[i].second, 0); }
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop();
    rep(dir, 4) {
      int ni = i + di[dir];
      int nj = j + dj[dir];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      update(ni, nj, dist[i][j] + 1);
    }
  }
  int ans = 0;
  rep(i, h) rep(j, w) { ans = max(ans, dist[i][j]); }
  cout << ans << endl;
  return 0;
}

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

int di[] = {-1, 0, 1, 0};
int dj[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  vector<vector<int>> dist(h, vector<int>(w, 1e9));
  vector<pair<int, int>> p;
  rep(i, h) cin >> a[i];
  rep(i, h) rep(j, w) if (a[i][j] == '#') p.push_back(make_pair(i, j));
  queue<pair<int, int>> q;
  auto update = [&](int i, int j, int x) {
    if (dist[i][j] != 1e9)
      return;
    dist[i][j] = x;
    q.push(make_pair(i, j));
  };
  rep(i, p.size()) { update(p[i].first, p[i].second, 0); }
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop();
    rep(dir, 4) {
      int ni = i + di[dir];
      int nj = j + dj[dir];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      update(ni, nj, dist[i][j] + 1);
    }
  }
  int ans = 0;
  rep(i, h) rep(j, w) { ans = max(ans, dist[i][j]); }
  cout << ans << endl;
  return 0;
}
Problem p03053 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    h, w = list(map(int, readline().split()))
    a = [[True] * w for _ in range(h)]
    ql = []
    ql = collections.deque(ql)
    for i in range(h):
        s = str(readline().rstrip().decode("utf-8"))
        for j in range(w):
            if s[j] == "#":
                a[i][j] = False
                ql.append([0, i, j])

    mt = 0
    while True:
        if len(ql) != 0:
            cost, x, y = ql.popleft()
            mt = max(mt, cost)
            for xv, yv in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
                if 0 <= xv < h and 0 <= yv < w:
                    if a[xv][yv]:
                        ql.append([cost + 1, xv, yv])
                        a[i][j] = False
        else:
            break
    print(mt)


if __name__ == "__main__":
    solve()


Fixed submission:
import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    h, w = list(map(int, readline().split()))
    a = [[True] * w for _ in range(h)]
    ql = []
    ql = collections.deque(ql)
    for i in range(h):
        s = str(readline().rstrip().decode("utf-8"))
        for j in range(w):
            if s[j] == "#":
                a[i][j] = False
                ql.append([0, i, j])

    mt = 0
    while True:
        if len(ql) != 0:
            cost, x, y = ql.popleft()
            mt = max(mt, cost)
            for xv, yv in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
                if 0 <= xv < h and 0 <= yv < w:
                    if a[xv][yv]:
                        ql.append([cost + 1, xv, yv])
                        a[xv][yv] = False
        else:
            break
    print(mt)


if __name__ == "__main__":
    solve()
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
int dp[200][200];
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int h, w;
  cin >> h >> w;
  rep(i, h) {
    rep(j, w) { dp[i][j] = 1e9; }
  }
  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  queue<P> que;
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '#') {
        que.push(P(i, j));
        dp[i][j] = 0;
      }
    }
  }
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int val = dp[p.first][p.second];
    rep(i, 4) {
      if (p.first + dy[i] >= h || p.first + dy[i] < 0) {
        continue;
      }
      if (p.second + dx[i] >= w || p.second + dx[i] < 0) {
        continue;
      }
      if (dp[p.first + dy[i]][p.second + dx[i]] > val + 1) {
        dp[p.first + dy[i]][p.second + dx[i]] = val + 1;
        que.push(P(p.first + dy[i], p.second + dx[i]));
      }
    }
  }
  int mx = 0;
  rep(i, h) {
    rep(j, w) { mx = max(mx, dp[i][j]); }
  }
  cout << mx << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
int dp[2000][2000];
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int h, w;
  cin >> h >> w;
  rep(i, h) {
    rep(j, w) { dp[i][j] = 1e9; }
  }
  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  queue<P> que;
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '#') {
        que.push(P(i, j));
        dp[i][j] = 0;
      }
    }
  }
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int val = dp[p.first][p.second];
    rep(i, 4) {
      if (p.first + dy[i] >= h || p.first + dy[i] < 0) {
        continue;
      }
      if (p.second + dx[i] >= w || p.second + dx[i] < 0) {
        continue;
      }
      if (dp[p.first + dy[i]][p.second + dx[i]] > val + 1) {
        dp[p.first + dy[i]][p.second + dx[i]] = val + 1;
        que.push(P(p.first + dy[i], p.second + dx[i]));
      }
    }
  }
  int mx = 0;
  rep(i, h) {
    rep(j, w) { mx = max(mx, dp[i][j]); }
  }
  cout << mx << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<int> a(h * w);
  vector<int> q;
  vector<int> next_q;
  char tmp;
  for (int i = 0; i < h * w; i++) {
    cin >> tmp;
    if (tmp == '#') {
      a[i] = 0;
      q.push_back(i);
    } else {
      a[i] = 1;
    }
  }
  int n, i, j, cnt = 0;
  while (!q.empty()) {
    while (!q.empty()) {
      // for(int k=0;k<q.size();k++) cout<<q[k]<<" ";cout<<endl;
      n = q[0];
      q.erase(q.begin());
      i = n / w;
      j = n % w;
      if (i != 0 && a[n - w] == 1) {
        a[n - w] = 0;
        next_q.push_back(n - w);
      }
      if (i != h - 1 && a[n + w] == 1) {
        a[n + w] = 0;
        next_q.push_back(n + w);
      }
      if (j != 0 && a[n - 1] == 1) {
        a[n - 1] = 0;
        next_q.push_back(n - 1);
      }
      if (j != w - 1 && a[n + 1] == 1) {
        a[n + 1] = 0;
        next_q.push_back(n + 1);
      }
    }
    q = next_q;
    next_q.clear();
    cnt++;
  }
  cout << cnt - 1 << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<int> a(h * w);
  vector<int> q;
  vector<int> next_q;
  char tmp;
  for (int i = 0; i < h * w; i++) {
    cin >> tmp;
    if (tmp == '#') {
      a[i] = 0;
      q.push_back(i);
    } else {
      a[i] = 1;
    }
  }
  int n, i, j, cnt = 0;
  while (!q.empty()) {
    while (!q.empty()) {
      // for(int k=0;k<q.size();k++) cout<<q[k]<<" ";cout<<endl;
      n = q[q.size() - 1];
      q.pop_back();
      i = n / w;
      j = n % w;
      if (i != 0 && a[n - w] == 1) {
        a[n - w] = 0;
        next_q.push_back(n - w);
      }
      if (i != h - 1 && a[n + w] == 1) {
        a[n + w] = 0;
        next_q.push_back(n + w);
      }
      if (j != 0 && a[n - 1] == 1) {
        a[n - 1] = 0;
        next_q.push_back(n - 1);
      }
      if (j != w - 1 && a[n + 1] == 1) {
        a[n + 1] = 0;
        next_q.push_back(n + 1);
      }
    }
    q = next_q;
    next_q.clear();
    cnt++;
  }
  cout << cnt - 1 << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <queue>
#include <stdio.h>
#include <utility>
// #define large 1001
#define large 50

using namespace std;

int h, w;
char a[large][large];
int visited[large][large] = {}; // 黒からのユークリッド距離を保有
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};

queue<pair<int, int>> que; //<x,y>

void bfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    if (!(x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
          y + vecy[i] >= w) &&
        a[x + vecx[i]][y + vecy[i]] == '.') {
      a[x + vecx[i]][y + vecy[i]] = '#';
      visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
      que.push(make_pair(x + vecx[i], y + vecy[i]));
    }
  }
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++)
    scanf("%s", &a[i]);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] == '#') {
        que.push(make_pair(i, j));
      }

  while (!que.empty()) {
    pair<int, int> p;
    p = que.front();
    que.pop();
    // printf("bfs(%d,%d)\n",p.first,p.second);
    bfs(p.first, p.second);
  }
  int max = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (max < visited[i][j])
        max = visited[i][j];
  printf("%d\n", max);
}

Fixed submission:
#include <queue>
#include <stdio.h>
#include <utility>
#define large 1001
// #define large 50

using namespace std;

int h, w;
char a[large][large];
int visited[large][large] = {}; // 黒からのユークリッド距離を保有
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};

queue<pair<int, int>> que; //<x,y>

void bfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    if (!(x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
          y + vecy[i] >= w) &&
        a[x + vecx[i]][y + vecy[i]] == '.') {
      a[x + vecx[i]][y + vecy[i]] = '#';
      visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
      que.push(make_pair(x + vecx[i], y + vecy[i]));
    }
  }
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++)
    scanf("%s", &a[i]);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] == '#') {
        que.push(make_pair(i, j));
      }

  while (!que.empty()) {
    pair<int, int> p;
    p = que.front();
    que.pop();
    // printf("bfs(%d,%d)\n",p.first,p.second);
    bfs(p.first, p.second);
  }
  int max = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (max < visited[i][j])
        max = visited[i][j];
  printf("%d\n", max);
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define sz size
#define pb push_back
#define mper make_pair
#define all(x) x.begin(), x.end()
#define fe first
#define se second
#define erba continue
using namespace std;

const ll MAXN = 1e3 + 403;

int n, m;

vector<pair<int, int>> g[MAXN];

bool used[MAXN][MAXN];

string s[MAXN];

int main() {

  cin >> n >> m;

  int p = 0;

  int sum = 0;

  for (int i = 1; i <= n; ++i) {

    cin >> s[i];

    for (int j = 1; j <= m; ++j) {

      if (s[i][j - 1] == '#') {

        used[i][j] = 1;
        g[p].pb(mper(i, j));
        sum++;
      }
    }
  }

  while (1) {

    if (sum == n * m) {
      break;
    }

    p++;
    //        cout << p << ' ';

    for (int i = 0; i < g[p - 1].sz(); ++i) {

      ll x = g[p - 1][i].fe;
      ll y = g[p - 1][i].se;

      if (!used[x][y + 1] && y + 1 <= m) {

        g[p].pb(mper(x, y + 1));
        used[x][y + 1] = 1;
        sum++;
      }

      if (!used[x][y - 1] && y - 1 >= 1) {

        g[p].pb(mper(x, y - 1));
        used[x][y - 1] = 1;

        sum++;
      }

      if (!used[x + 1][y] && x + 1 <= n) {

        g[p].pb(mper(x + 1, y));
        used[x + 1][y] = 1;

        sum++;
      }

      if (!used[x - 1][y] && x - 1 >= 1) {

        g[p].pb(mper(x - 1, y));
        used[x - 1][y] = 1;
        sum++;
      }
    }
  }

  cout << p;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define sz size
#define pb push_back
#define mper make_pair
#define all(x) x.begin(), x.end()
#define fe first
#define se second
#define erba continue
using namespace std;

const ll MAXN = 1e3 + 1403;

int n, m;

vector<pair<int, int>> g[MAXN];

bool used[MAXN][MAXN];

string s[MAXN];

int main() {

  cin >> n >> m;

  int p = 0;

  int sum = 0;

  for (int i = 1; i <= n; ++i) {

    cin >> s[i];

    for (int j = 1; j <= m; ++j) {

      if (s[i][j - 1] == '#') {

        used[i][j] = 1;
        g[p].pb(mper(i, j));
        sum++;
      }
    }
  }

  while (1) {

    if (sum == n * m) {
      break;
    }

    p++;
    //        cout << p << ' ';

    for (int i = 0; i < g[p - 1].sz(); ++i) {

      ll x = g[p - 1][i].fe;
      ll y = g[p - 1][i].se;

      if (!used[x][y + 1] && y + 1 <= m) {

        g[p].pb(mper(x, y + 1));
        used[x][y + 1] = 1;
        sum++;
      }

      if (!used[x][y - 1] && y - 1 >= 1) {

        g[p].pb(mper(x, y - 1));
        used[x][y - 1] = 1;

        sum++;
      }

      if (!used[x + 1][y] && x + 1 <= n) {

        g[p].pb(mper(x + 1, y));
        used[x + 1][y] = 1;

        sum++;
      }

      if (!used[x - 1][y] && x - 1 >= 1) {

        g[p].pb(mper(x - 1, y));
        used[x - 1][y] = 1;
        sum++;
      }
    }
  }

  cout << p;

  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int h, w;
vector<string> s(1024);
queue<pair<int, int>> calc(queue<pair<int, int>> que) {
  int n = que.size();
  for (int _ = 0; _ < n; _++) {
    pair<int, int> p = que.front();
    que.pop();
    if (p.first > 0) {
      if (s[p.first - 1][p.second] == '.')
        que.push(make_pair(p.first - 1, p.second));
    }
    if (p.first < h - 1) {
      if (s[p.first + 1][p.second] == '.')
        que.push(make_pair(p.first + 1, p.second));
    }
    if (p.second > 0) {
      if (s[p.first][p.second - 1] == '.')
        que.push(make_pair(p.first, p.second - 1));
    }
    if (p.second < w - 1) {
      if (s[p.first][p.second + 1] == '.')
        que.push(make_pair(p.first, p.second + 1));
    }
  }
  n = que.size();
  for (int _ = 0; _ < n; _++) {
    pair<int, int> p = que.front();
    que.pop();
    s[p.first][p.second] = '#';
    que.push(p);
  }
  return que;
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  queue<pair<int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (s[i][j] == '#')
        que.push(make_pair(i, j));
  }
  int ans = 0;
  while (!que.empty()) {
    ans++;
    que = calc(que);
  }
  cout << ans - 1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int h, w;
vector<string> s(1024);
queue<pair<int, int>> calc(queue<pair<int, int>> que) {
  int n = que.size();
  for (int _ = 0; _ < n; _++) {
    pair<int, int> p = que.front();
    que.pop();
    if (s[p.first][p.second] != '#')
      continue;
    s[p.first][p.second] = 'x';
    if (p.first > 0) {
      if (s[p.first - 1][p.second] == '.')
        que.push(make_pair(p.first - 1, p.second));
    }
    if (p.first < h - 1) {
      if (s[p.first + 1][p.second] == '.')
        que.push(make_pair(p.first + 1, p.second));
    }
    if (p.second > 0) {
      if (s[p.first][p.second - 1] == '.')
        que.push(make_pair(p.first, p.second - 1));
    }
    if (p.second < w - 1) {
      if (s[p.first][p.second + 1] == '.')
        que.push(make_pair(p.first, p.second + 1));
    }
  }
  n = que.size();
  for (int _ = 0; _ < n; _++) {
    pair<int, int> p = que.front();
    que.pop();
    s[p.first][p.second] = '#';
    que.push(p);
  }
  return que;
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  queue<pair<int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (s[i][j] == '#')
        que.push(make_pair(i, j));
  }
  int ans = 0;
  while (!que.empty()) {
    ans++;
    que = calc(que);
  }
  cout << ans - 1 << endl;
}
Problem p03053 (C++)

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

using namespace std;
string grid[1001];
int mem[1001][1001][4];
int di[2] = {1, -1};

int n, m;

bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

int solve(int i, int j, int d1, int d2) {
  if (!valid(i, j)) {
    return 1e9;
  }
  if (grid[i][j] == '#')
    return 0;
  int &ret = mem[i][j][d1 * 2 + d2];
  if (~ret)
    return ret;
  ret = 1e9;
  ret = min(ret, solve(i + di[d1], j, d1, d2) + 1);
  ret = min(ret, solve(i, j + di[d2], d1, d2) + 1);
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  memset(mem, -1, sizeof(mem));
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int mn = 1e9;
      for (int k = 0; k < 2; k++) {
        for (int l = 0; l < 2; l++) {
          mn = min(mn, solve(i, j, k, l));
        }
      }
      res = max(res, mn);
    }
  }
  cout << res << endl;
  return 0;
}


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

using namespace std;
string grid[1001];
int mem[1001][1001][4];
int di[2] = {1, -1};

int n, m;

bool valid(int i, int j) { return i >= 0 && i < n && j >= 0 && j < m; }

int solve(int i, int j, int d1, int d2) {
  if (!valid(i, j)) {
    return 1e9;
  }
  if (grid[i][j] == '#')
    return 0;
  int &ret = mem[i][j][d1 * 2 + d2];
  if (~ret)
    return ret;
  ret = 1e9;
  ret = min(ret, solve(i + di[d1], j, d1, d2) + 1);
  ret = min(ret, solve(i, j + di[d2], d1, d2) + 1);
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("input.in", "r", stdin);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  memset(mem, -1, sizeof(mem));
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int mn = 1e9;
      for (int k = 0; k < 2; k++) {
        for (int l = 0; l < 2; l++) {
          mn = min(mn, solve(i, j, k, l));
        }
      }
      res = max(res, mn);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int A = 0;

// int cnt=0;
int W, H;
vector<string> s(1100);
int cost[1100][1100];

void bfs(int a, int b) {
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  queue<pair<int, int>> que;
  que.push(make_pair(a, b));
  cost[a][b] = 0;
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      // マスの外
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;

      if (cost[p.first][p.second] + 1 >= cost[ny][nx]) {
        continue;
      }
      if (s[ny][nx] == '#' && cost[ny][nx] != 0 && A <= 1000000) {
        A++;
        bfs(ny, nx);
        continue;
      }
      if (s[ny][nx] == '#')
        continue;
      cost[ny][nx] = cost[p.first][p.second] + 1;
      que.push(make_pair(ny, nx));
      // cnt++;
    }
  }
}

int main() {
  cin >> H >> W;
  int ans = 0;
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cost[i][j] = 2000000;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[i][j] == '#' && cost[i][j] != 0) {
        bfs(i, j);
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(ans, cost[i][j]);
    }
  }
  cout << ans << endl;
  // cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int A = 0;

// int cnt=0;
int W, H;
vector<string> s(1100);
int cost[1100][1100];

void bfs(int a, int b) {
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  queue<pair<int, int>> que;
  que.push(make_pair(a, b));
  cost[a][b] = 0;
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      // マスの外
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;

      if (cost[p.first][p.second] + 1 >= cost[ny][nx]) {
        continue;
      }
      if (s[ny][nx] == '#' && cost[ny][nx] != 0 && A <= 500000) {
        A++;
        bfs(ny, nx);
        continue;
      }
      if (s[ny][nx] == '#')
        continue;
      cost[ny][nx] = cost[p.first][p.second] + 1;
      que.push(make_pair(ny, nx));
      // cnt++;
    }
  }
}

int main() {
  cin >> H >> W;
  int ans = 0;
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cost[i][j] = 2000000;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[i][j] == '#' && cost[i][j] != 0) {
        bfs(i, j);
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(ans, cost[i][j]);
    }
  }
  cout << ans << endl;
  // cout << cnt << endl;
}
Problem p03053 (C++)

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

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;

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

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

void bfs(vector<P> &starts) {
  queue<P> q;
  for (auto s : starts) {
    q.push(s);
    steps[s.first][s.second] = 0;
  }

  while (!q.empty()) {
    int y, x;
    tie(y, x) = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int ny = y + dy[k];
      int nx = x + dx[k];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (s[ny][nx] == '#')
        continue;
      s[ny][nx] = '#';
      steps[ny][nx] = min(steps[ny][nx], steps[y][x] + 1);
      q.emplace(nx, ny);
    }
  }
  return;
}

int main() {
#ifdef MYLOCAL
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
#endif

  cin >> h >> w;
  s.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  steps.resize(h);
  for (int i = 0; i < h; i++) {
    steps[i].resize(w);
  }

  vector<P> starts;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      steps[i][j] = INF32;
      if (s[i][j] == '#') {
        starts.push_back(make_pair(i, j));
      }
    }
  }

  bfs(starts);

  int ans = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      ans = max(ans, steps[i][j]);
    }
  }

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


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

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF64 = 1LL << 60;
const int INF32 = 1 << 29;
const int MOD = 1000000007;

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

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

void bfs(vector<P> &starts) {
  queue<P> q;
  for (auto s : starts) {
    q.push(s);
    steps[s.first][s.second] = 0;
  }

  while (!q.empty()) {
    int y, x;
    tie(y, x) = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int ny = y + dy[k];
      int nx = x + dx[k];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (s[ny][nx] == '#')
        continue;
      s[ny][nx] = '#';
      steps[ny][nx] = min(steps[ny][nx], steps[y][x] + 1);
      q.emplace(ny, nx);
    }
  }
  return;
}

int main() {
#ifdef MYLOCAL
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
#endif

  cin >> h >> w;
  s.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  steps.resize(h);
  for (int i = 0; i < h; i++) {
    steps[i].resize(w);
  }

  vector<P> starts;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      steps[i][j] = INF32;
      if (s[i][j] == '#') {
        starts.push_back(make_pair(i, j));
      }
    }
  }

  bfs(starts);

  int ans = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      ans = max(ans, steps[i][j]);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define mp make_pair
int main() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      a[i][j] = -1;
    }
  }
  char s[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
    }
  }
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (s[i][j] == '#') {
        q.push(mp(i, j));
        a[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x + 1 < n && a[x + 1][y] == -1) {
      q.push(mp(x + 1, y));
      a[x + 1][y] = a[x][y] + 1;
    }
    if (x - 1 >= 0 && a[x - 1][y] == -1) {
      q.push(mp(x - 1, y));
      a[x - 1][y] = a[x][y] + 1;
    }
    if (y + 1 < m && a[x][y + 1] == -1) {
      q.push(mp(x, y + 1));
      a[x][y + 1] = a[x][y] + 1;
    }
    if (y - 1 >= 0 && a[x][y - 1] == -1) {
      q.push(mp(x, y - 1));
      a[x][y - 1] = a[x][y] + 1;
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ans = max(ans, a[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define mp make_pair
int main() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      a[i][j] = -1;
    }
  }
  char s[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
    }
  }
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == '#') {
        q.push(mp(i, j));
        a[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x + 1 < n && a[x + 1][y] == -1) {
      q.push(mp(x + 1, y));
      a[x + 1][y] = a[x][y] + 1;
    }
    if (x - 1 >= 0 && a[x - 1][y] == -1) {
      q.push(mp(x - 1, y));
      a[x - 1][y] = a[x][y] + 1;
    }
    if (y + 1 < m && a[x][y + 1] == -1) {
      q.push(mp(x, y + 1));
      a[x][y + 1] = a[x][y] + 1;
    }
    if (y - 1 >= 0 && a[x][y - 1] == -1) {
      q.push(mp(x, y - 1));
      a[x][y - 1] = a[x][y] + 1;
    }
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      ans = max(ans, a[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int h, w;
char a[1000][1000];
int d[1000][1000] = {};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
queue<P> que;
int dmax = 0;

void bfs() {
  while (que.size()) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx <= w && 0 <= ny && ny <= h && a[nx][ny] == '.' &&
          d[nx][ny] == 0) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
    }
  }
  return;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#') {
        que.push(P(i, j));
      }
    }
  }
  bfs();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dmax = max(d[i][j], dmax);
    }
  }
  cout << dmax << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int h, w;
char a[1000][1000];
int d[1000][1000] = {};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
queue<P> que;
int dmax = 0;

void bfs() {
  while (que.size()) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && a[nx][ny] == '.' &&
          d[nx][ny] == 0) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
    }
  }
  return;
}

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == '#') {
        que.push(P(i, j));
      }
    }
  }
  bfs();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dmax = max(d[i][j], dmax);
    }
  }
  cout << dmax << endl;
}
Problem p03053 (C++)

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

using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;

int nw[9] = {0, 1, 0, -1, -1, 1, 1, -1, 0};
int nh[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};

int main() {
  llong H, W;
  vector<string> A;
  queue<P> q;

  cin >> H >> W;
  A.resize(H, string(W, '.'));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == '#') {
        q.push(P(i, j));
      }
    }

  llong ans = 0;
  while (q.size()) {
    bool check = false;
    queue<P> n;
    while (q.size()) {
      P now = q.front();
      q.pop();
      for (int i = 0; i < 4; i++) {
        P next(now.first + nh[i], now.second + nw[i]);
        if (next.first < 0 || next.second < 0 || next.first >= H ||
            next.second >= W)
          continue;
        if (A[next.first][next.second] == '.') {
          A[next.first][next.second] = '#';
          n.push(next);
          check = true;
        }
      }
    }

    for (auto x : A)
      cerr << x << endl;
    cerr << endl;

    ans += check;
    swap(q, n);
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

const llong inf = 1100100100100100ll;
const llong mod = 1e9 + 7;

int nw[9] = {0, 1, 0, -1, -1, 1, 1, -1, 0};
int nh[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};

int main() {
  llong H, W;
  vector<string> A;
  queue<P> q;

  cin >> H >> W;
  A.resize(H, string(W, '.'));
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == '#') {
        q.push(P(i, j));
      }
    }

  llong ans = 0;
  while (q.size()) {
    bool check = false;
    queue<P> n;
    while (q.size()) {
      P now = q.front();
      q.pop();
      for (int i = 0; i < 4; i++) {
        P next(now.first + nh[i], now.second + nw[i]);
        if (next.first < 0 || next.second < 0 || next.first >= H ||
            next.second >= W)
          continue;
        if (A[next.first][next.second] == '.') {
          A[next.first][next.second] = '#';
          n.push(next);
          check = true;
        }
      }
    }

    ans += check;
    swap(q, n);
  }

  cout << ans << endl;

  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000000 + 7
#define INF 1000000000
typedef long long ll;
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int d[1000][1000];
char a[1000][1000];

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h = 0, w = 0;
queue<pair<int, int>> que;

int bfs() {
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();

    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      if (1 <= nx && nx <= w && 1 <= ny && ny <= h && a[ny][nx] != '#' &&
          d[ny][nx] == INF) {
        que.push(pair<int, int>(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }

  int ans = 0;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      ans = max(ans, d[i][j]);
    }
  }

  return ans;
}

int main() {

  cin >> h >> w;
  string s;

  for (int i = 0; i <= h; i++) {
    for (int j = 0; j <= w; j++) {
      d[i][j] = INF;
    }
  }
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++) {
      a[i][j] = s[j - 1];
      if (a[i][j] == '#') {
        que.push(pair<int, int>(i, j));
        d[i][j] = 0;
      }
    }
  }

  int res = bfs();
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD 1000000000 + 7
#define INF 1000000000
typedef long long ll;
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int d[1010][1010];
char a[1010][1010];

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h = 0, w = 0;
queue<pair<int, int>> que;

int bfs() {
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();

    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      if (1 <= nx && nx <= w && 1 <= ny && ny <= h && a[ny][nx] != '#' &&
          d[ny][nx] == INF) {
        que.push(pair<int, int>(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }

  int ans = 0;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      ans = max(ans, d[i][j]);
    }
  }

  return ans;
}

int main() {

  cin >> h >> w;
  string s;

  for (int i = 0; i <= h; i++) {
    for (int j = 0; j <= w; j++) {
      d[i][j] = INF;
    }
  }
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++) {
      a[i][j] = s[j - 1];
      if (a[i][j] == '#') {
        que.push(pair<int, int>(i, j));
        d[i][j] = 0;
      }
    }
  }

  int res = bfs();
  cout << res << endl;
}
Problem p03053 (C++)

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

int n, m, x, a, b, c;
bool arr[10005][10005];
char y;
queue<pair<pair<int, int>, int>> q;

int main() {
  memset(arr, -1, sizeof(arr));
  cin >> n >> m;
  x = n * m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> y;
      if (y == '#') {
        arr[i][j] = 1;
        q.push(make_pair(make_pair(i, j), 0));
      }
    }
  }
  while (x > 1) {
    a = q.front().first.first;
    b = q.front().first.second;
    c = q.front().second;
    q.pop();
    x--;
    if (a > 1 && arr[a - 1][b] == 0) {
      q.push(make_pair(make_pair(a - 1, b), c + 1));
      arr[a - 1][b] = 1;
    }
    if (a < n && arr[a + 1][b] == 0) {
      q.push(make_pair(make_pair(a + 1, b), c + 1));
      arr[a + 1][b] = 1;
    }
    if (b > 1 && arr[a][b - 1] == 0) {
      q.push(make_pair(make_pair(a, b - 1), c + 1));
      arr[a][b - 1] = 1;
    }
    if (b < m && arr[a][b + 1] == 0) {
      q.push(make_pair(make_pair(a, b + 1), c + 1));
      arr[a][b + 1] = 1;
    }
  }
  c = q.front().second;
  q.pop();
  cout << c << endl;
}

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

int n, m, x, a, b, c;
bool arr[10005][10005];
char y;
queue<pair<pair<int, int>, int>> q;

int main() {
  cin >> n >> m;
  x = n * m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> y;
      if (y == '#') {
        arr[i][j] = 1;
        q.push(make_pair(make_pair(i, j), 0));
      }
    }
  }
  while (x > 1) {
    a = q.front().first.first;
    b = q.front().first.second;
    c = q.front().second;
    q.pop();
    x--;
    if (a > 1 && arr[a - 1][b] == 0) {
      q.push(make_pair(make_pair(a - 1, b), c + 1));
      arr[a - 1][b] = 1;
    }
    if (a < n && arr[a + 1][b] == 0) {
      q.push(make_pair(make_pair(a + 1, b), c + 1));
      arr[a + 1][b] = 1;
    }
    if (b > 1 && arr[a][b - 1] == 0) {
      q.push(make_pair(make_pair(a, b - 1), c + 1));
      arr[a][b - 1] = 1;
    }
    if (b < m && arr[a][b + 1] == 0) {
      q.push(make_pair(make_pair(a, b + 1), c + 1));
      arr[a][b + 1] = 1;
    }
  }
  c = q.front().second;
  q.pop();
  cout << c << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <queue>
#include <stdio.h>
// #define large 1005
#define large 50

using namespace std;

int h, w;
char a[large][large];
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};
int visited[large][large] = {};

queue<int> que;

void bfs(int x, int y) {
  // if(visited[x][y]>0) return;
  for (int i = 0; i < 4; i++) {
    if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
        y + vecy[i] >= w)
      continue;
    else {
      if (a[x + vecx[i]][y + vecy[i]] == '.' &&
          visited[x + vecx[i]][y + vecy[i]] == 0)
      // if(a[x+vecx[i]][y+vecy[i]]=='.')
      {
        visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
        a[x + vecx[i]][y + vecy[i]] = '#';
        que.push(x + vecx[i]);
        que.push(y + vecy[i]);
      }
    }
  }
  return;
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++)
    scanf("%s", a[i]);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] == '#') {
        que.push(i);
        que.push(j);
      }

  while (!que.empty()) {
    // for(int j=0;j<h;j++)
    //     printf("%s\n",a[j]);
    // printf("\n");

    int tmpx = que.front();
    que.pop();
    int tmpy = que.front();
    que.pop();
    // printf("bfs(%d,%d)\n",tmpx,tmpy);
    bfs(tmpx, tmpy);
  }
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (visited[i][j] > ans)
        ans = visited[i][j];
    }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <queue>
#include <stdio.h>
#define large 1005
// #define large 50

using namespace std;

int h, w;
char a[large][large];
int vecx[] = {1, -1, 0, 0};
int vecy[] = {0, 0, 1, -1};
int visited[large][large] = {};

queue<int> que;

void bfs(int x, int y) {
  // if(visited[x][y]>0) return;
  for (int i = 0; i < 4; i++) {
    if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
        y + vecy[i] >= w)
      continue;
    else {
      if (a[x + vecx[i]][y + vecy[i]] == '.' &&
          visited[x + vecx[i]][y + vecy[i]] == 0)
      // if(a[x+vecx[i]][y+vecy[i]]=='.')
      {
        visited[x + vecx[i]][y + vecy[i]] = visited[x][y] + 1;
        a[x + vecx[i]][y + vecy[i]] = '#';
        que.push(x + vecx[i]);
        que.push(y + vecy[i]);
      }
    }
  }
  return;
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < h; i++)
    scanf("%s", a[i]);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (a[i][j] == '#') {
        que.push(i);
        que.push(j);
      }

  while (!que.empty()) {
    // for(int j=0;j<h;j++)
    //     printf("%s\n",a[j]);
    // printf("\n");

    int tmpx = que.front();
    que.pop();
    int tmpy = que.front();
    que.pop();
    // printf("bfs(%d,%d)\n",tmpx,tmpy);
    bfs(tmpx, tmpy);
  }
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (visited[i][j] > ans)
        ans = visited[i][j];
    }
  printf("%d\n", ans);
  return 0;
}
Problem p03053 (C++)

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

const int INF = 100000000;
typedef pair<int, int> P;
const int MAX_H = 50, MAX_W = 50;
int H, W;
int sy, sx;
int gy, gx;
char maze[MAX_H][MAX_W];

int d[MAX_H][MAX_W];

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

void bfs(int y, int x);

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  queue<P> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze[i][j] == '#') {
        d[i][j] = 0;
        que.push(P(i, j));
      }
    }
  }

  // BFS
  while (que.size()) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] != '#' &&
          d[ny][nx] == INF) {
        que.push(P(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (d[i][j] > ans)
        ans = d[i][j];
    }
  }
  cout << ans << endl;
}

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

const int INF = 100000000;
typedef pair<int, int> P;
const int MAX_H = 1000, MAX_W = 1000;
int H, W;
int sy, sx;
int gy, gx;
char maze[MAX_H][MAX_W];

int d[MAX_H][MAX_W];

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

void bfs(int y, int x);

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }
  queue<P> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze[i][j] == '#') {
        d[i][j] = 0;
        que.push(P(i, j));
      }
    }
  }

  // BFS
  while (que.size()) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int ny = p.first + dy[i], nx = p.second + dx[i];
      if (0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] != '#' &&
          d[ny][nx] == INF) {
        que.push(P(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (d[i][j] > ans)
        ans = d[i][j];
    }
  }
  cout << ans << endl;
}
Problem p03053 (C++)

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("./a.in", "r", stdin);
  freopen("./a.out", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  int h, w;
  cin >> h >> w;

  vector<string> grid(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }

  vector<vector<int>> dist(h, vector<int>(w, -1));
  vector<pair<int, int>> que;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '#') {
        que.emplace_back(i, j);
        dist[i][j] = 0;
      }
    }
  }

  for (int b = 0; b < que.size(); b++) {
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx * dx + dy * dy == 1) {
          int xk = que[b].first + dx;
          int yk = que[b].second + dy;
          if (xk >= 0 && yk >= 0 && xk < h && yk < w && dist[xk][yk] == -1) {
            que.emplace_back(xk, yk);
            dist[xk][yk] = dist[que[b].first][que[b].second] + 1;
          }
        }
      }
    }
  }
  cout << dist[que.back().first][que.back().second] << endl;
  // for(int i=0; i<h; i++){
  // 	for(int j=0; j<w; j++){
  // 		cout << dist[i][j];
  // 	}
  // 	cout << endl;
  // }
}

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

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("./a.in","r",stdin);
  // 	freopen("./a.out","w",stdout);
  // #endif

  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  int h, w;
  cin >> h >> w;

  vector<string> grid(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }

  vector<vector<int>> dist(h, vector<int>(w, -1));
  vector<pair<int, int>> que;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '#') {
        que.emplace_back(i, j);
        dist[i][j] = 0;
      }
    }
  }

  for (int b = 0; b < que.size(); b++) {
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if (dx * dx + dy * dy == 1) {
          int xk = que[b].first + dx;
          int yk = que[b].second + dy;
          if (xk >= 0 && yk >= 0 && xk < h && yk < w && dist[xk][yk] == -1) {
            que.emplace_back(xk, yk);
            dist[xk][yk] = dist[que[b].first][que[b].second] + 1;
          }
        }
      }
    }
  }
  cout << dist[que.back().first][que.back().second] << endl;
  // for(int i=0; i<h; i++){
  // 	for(int j=0; j<w; j++){
  // 		cout << dist[i][j];
  // 	}
  // 	cout << endl;
  // }
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <queue>
#include <tuple>

using namespace std;

int main(int argc, char const *argv[]) {
  int h, w;
  scanf("%d %d\n", h, w);
  char maze[h][w + 1];
  for (int i = 0; i < h; i++) {
    scanf("%s\n", maze[i]);
  }
  int ans = 0;
  queue<tuple<int, int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (maze[i][j] == '#')
        que.push(make_tuple(i, j, 0));
    }
  }
  int dxy[] = {0, 1, 0, -1, 0};
  while (!que.empty()) {
    int x, y, d;
    tie(x, y, d) = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dxy[i];
      int ny = y + dxy[i + 1];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny)
        continue;
      else if (maze[nx][ny] == '.') {
        maze[nx][ny] = '#';
        que.push(make_tuple(nx, ny, d + 1));
        ans = d + 1;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <queue>
#include <tuple>

using namespace std;

int main(int argc, char const *argv[]) {
  int h, w;
  scanf("%d %d\n", &h, &w);
  char maze[h][w + 1];
  for (int i = 0; i < h; i++) {
    scanf("%s\n", maze[i]);
  }
  int ans = 0;
  queue<tuple<int, int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (maze[i][j] == '#')
        que.push(make_tuple(i, j, 0));
    }
  }
  int dxy[] = {0, 1, 0, -1, 0};
  while (!que.empty()) {
    int x, y, d;
    tie(x, y, d) = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dxy[i];
      int ny = y + dxy[i + 1];
      if (nx < 0 || h <= nx || ny < 0 || w <= ny)
        continue;
      else if (maze[nx][ny] == '.') {
        maze[nx][ny] = '#';
        que.push(make_tuple(nx, ny, d + 1));
        ans = d + 1;
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}