Problem p01416 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define LINE() cerr << " (L" << __LINE__ << ")"

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  int H, W;
  cin >> H >> W;

  vector<string> board(H);
  for (int y = 0; y < H; y++) {
    cin >> board[y];
  }

  map<int, int> xmap, ymap;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (board[y][x] != '.') {
        xmap.insert(make_pair(x, 1));
        if (0 < x - 1 && x - 1 < W)
          xmap.insert(make_pair(x - 1, 1));
        if (0 < x + 1 && x + 1 < W)
          xmap.insert(make_pair(x + 1, 1));

        ymap.insert(make_pair(y, 1));
        if (0 < y - 1 && y - 1 < H)
          ymap.insert(make_pair(y - 1, 1));
        if (0 < y + 1 && y + 1 < H)
          ymap.insert(make_pair(y + 1, 1));
      }
    }
  }
  vector<int> xs, ys;
  for (auto it = xmap.begin(); it != xmap.end(); it++) {
    xs.push_back(it->first);
  }
  for (auto it = ymap.begin(); it != ymap.end(); it++) {
    ys.push_back(it->first);
  }
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());

  int h = ys.size(), w = xs.size();
  int res = 0;
  for (int c = 0; c < 30; c++) {
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++)
        if (board[ys[y]][xs[x]] == '.') {
          map<char, vector<pair<int, int>>> ps;
          for (int d = 0; d < 4; d++) {
            int px = x, py = y;
            while (0 <= px && px < w && 0 <= py && py < h &&
                   board[ys[py]][xs[px]] == '.') {
              px += dx[d];
              py += dy[d];
            }
            if (0 <= px && px < w && 0 <= py && py < h &&
                board[ys[py]][xs[px]] != '.') {
              ps[board[ys[py]][xs[px]]].push_back(make_pair(py, px));
            }
          }

          for (auto it = ps.begin(); it != ps.end(); it++) {
            vector<pair<int, int>> vs = it->second;
            if (it->second.size() == 2) {
              res += 2;
              board[ys[vs[0].first]][xs[vs[0].second]] = '.';
              board[ys[vs[1].first]][xs[vs[1].second]] = '.';
            }
          }
        }
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define LINE() cerr << " (L" << __LINE__ << ")"

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  int H, W;
  cin >> H >> W;

  vector<string> board(H);
  for (int y = 0; y < H; y++) {
    cin >> board[y];
  }

  map<int, int> xmap, ymap;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (board[y][x] != '.') {
        xmap.insert(make_pair(x, 1));
        if (0 < x - 1 && x - 1 < W)
          xmap.insert(make_pair(x - 1, 1));
        if (0 < x + 1 && x + 1 < W)
          xmap.insert(make_pair(x + 1, 1));

        ymap.insert(make_pair(y, 1));
        if (0 < y - 1 && y - 1 < H)
          xmap.insert(make_pair(y - 1, 1));
        if (0 < y + 1 && y + 1 < H)
          xmap.insert(make_pair(y + 1, 1));
      }
    }
  }
  vector<int> xs, ys;
  for (auto it = xmap.begin(); it != xmap.end(); it++) {
    xs.push_back(it->first);
  }
  for (auto it = ymap.begin(); it != ymap.end(); it++) {
    ys.push_back(it->first);
  }
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());

  int h = ys.size(), w = xs.size();
  int res = 0;
  for (int c = 0; c < 30; c++) {
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++)
        if (board[ys[y]][xs[x]] == '.') {
          map<char, vector<pair<int, int>>> ps;
          for (int d = 0; d < 4; d++) {
            int px = x, py = y;
            while (0 <= px && px < w && 0 <= py && py < h &&
                   board[ys[py]][xs[px]] == '.') {
              px += dx[d];
              py += dy[d];
            }
            if (0 <= px && px < w && 0 <= py && py < h &&
                board[ys[py]][xs[px]] != '.') {
              ps[board[ys[py]][xs[px]]].push_back(make_pair(py, px));
            }
          }

          for (auto it = ps.begin(); it != ps.end(); it++) {
            vector<pair<int, int>> vs = it->second;
            if (it->second.size() == 2) {
              res += 2;
              board[ys[vs[0].first]][xs[vs[0].second]] = '.';
              board[ys[vs[1].first]][xs[vs[1].second]] = '.';
            }
          }
        }
  }
  cout << res << endl;
}