Problem p03053 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
using p_int = pair<int, int>;
int H, W;
// char field[1000][1000];
char field[6][6];
p_int direction[4] = {make_pair(1, 0), make_pair(0, 1), make_pair(-1, 0),
                      make_pair(0, -1)};
vector<p_int> blacks;

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

  cin >> H >> W;
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      cin >> field[y][x];
      if (field[y][x] == '#') {
        blacks.emplace_back(y, x);
      }
    }
  }

  int total = 0;
  while (true) {
    bool reverse = false;
    vector<p_int> next_blacks;
    for (auto b : blacks) {
      int y = b.first;
      int x = b.second;
      for (auto d : direction) {
        int ny = y + d.first;
        int nx = x + d.second;
        if (0 <= ny && ny < H && 0 <= nx && nx < W && field[ny][nx] == '.') {
          next_blacks.emplace_back(ny, nx);
          field[ny][nx] = '#';
          reverse = true;
        }
      }
    }
    // for (int y = 0; y < H; ++y) {
    //     for (int x = 0; x < W; ++x) {
    //         if (field[y][x] == '#') {
    //             for (auto d: direction) {
    //                 int ny = y + d.first;
    //                 int nx = x + d.second;
    //                 if (0 <= ny && ny < H && 0 <= nx && nx < W &&
    //                 field[ny][nx] == '.') {
    //                     field[ny][nx] = 'x';
    //                     reverse = true;
    //                 }
    //             }
    //         }
    //     }
    // }
    if (!reverse) {
      break;
    }
    blacks.clear();
    for (auto b : next_blacks) {
      blacks.push_back(b);
    }

    // for (int i = 0; i < H; ++i) {
    //     for (int j = 0; j < W; ++j) {
    //         if (field[i][j] == 'x') {
    //             field[i][j] = '#';
    //         }
    //     }
    // }
    total++;
  }

  cout << total << '\n';

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using p_int = pair<int, int>;
int H, W;
char field[1000][1000];
p_int direction[4] = {make_pair(1, 0), make_pair(0, 1), make_pair(-1, 0),
                      make_pair(0, -1)};
vector<p_int> blacks;

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

  cin >> H >> W;
  for (int y = 0; y < H; ++y) {
    for (int x = 0; x < W; ++x) {
      cin >> field[y][x];
      if (field[y][x] == '#') {
        blacks.emplace_back(y, x);
      }
    }
  }

  int total = 0;
  while (true) {
    bool reverse = false;
    vector<p_int> next_blacks;
    for (auto b : blacks) {
      int y = b.first;
      int x = b.second;
      for (auto d : direction) {
        int ny = y + d.first;
        int nx = x + d.second;
        if (0 <= ny && ny < H && 0 <= nx && nx < W && field[ny][nx] == '.') {
          next_blacks.emplace_back(ny, nx);
          field[ny][nx] = '#';
          reverse = true;
        }
      }
    }
    // for (int y = 0; y < H; ++y) {
    //     for (int x = 0; x < W; ++x) {
    //         if (field[y][x] == '#') {
    //             for (auto d: direction) {
    //                 int ny = y + d.first;
    //                 int nx = x + d.second;
    //                 if (0 <= ny && ny < H && 0 <= nx && nx < W &&
    //                 field[ny][nx] == '.') {
    //                     field[ny][nx] = 'x';
    //                     reverse = true;
    //                 }
    //             }
    //         }
    //     }
    // }
    if (!reverse) {
      break;
    }
    blacks.clear();
    for (auto b : next_blacks) {
      blacks.push_back(b);
    }

    // for (int i = 0; i < H; ++i) {
    //     for (int j = 0; j < W; ++j) {
    //         if (field[i][j] == 'x') {
    //             field[i][j] = '#';
    //         }
    //     }
    // }
    total++;
  }

  cout << total << '\n';

  return 0;
}