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


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2014

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

#define MAX 52

char area[MAX][MAX];
int ans[MAX][MAX];
int visit[MAX][MAX];
int W, H;

void init() {
  fill_n((char *)area, sizeof(area) / sizeof(char), '.');
  fill_n((int *)ans, sizeof(ans) / sizeof(int), 0);
  fill_n((int *)visit, sizeof(visit) / sizeof(int), 0);
}

void clear() { fill_n((int *)visit, sizeof(visit) / sizeof(int), 0); }

void dfs(int x, int y, char color) {
  int mark = (color == 'W') ? 1 : 2;
  char rival = (color == 'W') ? 'B' : 'W';

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

  if (x < 0 || x >= W || y < 0 || y >= H) {
    return;
  }
  if (area[y][x] == rival) {
    return;
  }

  if (!visit[y][x]) {
    visit[y][x] = 1;
    if (area[y][x] != 'W' && area[y][x] != 'B') {
      ans[y][x] |= mark;
    }

    rep(i, 4) { dfs(x + dx[i], y + dy[i], color); }
  }

  return;
}

int main() {

  while (cin >> W >> H && W) {
    int black = 0, white = 0;
    init();

    rep(h, H) {
      rep(w, W) { cin >> area[h][w]; }
    }

    rep(h, H) {
      rep(w, W) {
        if (area[h][w] != '.') {
          clear();
          dfs(w, h, area[h][w]);
        }
      }
    }

    rep(h, H) {
      rep(w, W) {
        int num = ans[h][w];
        if (num == 1) {
          white++;
        }
        if (num == 2) {
          black++;
        }
      }
    }

    cout << black << " " << white << endl;
  }
  return 0;
}

Error: 0

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2014

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

#define MAX 10

char area[MAX][MAX];
int ans[MAX][MAX];
int visit[MAX][MAX];
int W, H;

void init() {
  fill_n((char *)area, sizeof(area) / sizeof(char), '.');
  fill_n((int *)ans, sizeof(ans) / sizeof(int), 0);
  fill_n((int *)visit, sizeof(visit) / sizeof(int), 0);
}

void clear() { fill_n((int *)visit, sizeof(visit) / sizeof(int), 0); }

void dfs(int x, int y, char color) {
  int mark = (color == 'W') ? 1 : 2;
  char rival = (color == 'W') ? 'B' : 'W';

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

  if (x < 0 || x >= W || y < 0 || y >= H) {
    return;
  }
  if (area[y][x] == rival) {
    return;
  }

  if (!visit[y][x]) {
    visit[y][x] = 1;
    if (area[y][x] != 'W' && area[y][x] != 'B') {
      ans[y][x] |= mark;
    }

    rep(i, 4) { dfs(x + dx[i], y + dy[i], color); }
  }

  return;
}

int main() {

  while (cin >> W >> H && W) {
    int black = 0, white = 0;
    init();

    rep(h, H) {
      rep(w, W) { cin >> area[h][w]; }
    }

    rep(h, H) {
      rep(w, W) {
        if (area[h][w] != '.') {
          clear();
          dfs(w, h, area[h][w]);
        }
      }
    }

    rep(h, H) {
      rep(w, W) {
        int num = ans[h][w];
        if (num == 1) {
          white++;
        }
        if (num == 2) {
          black++;
        }
      }
    }

    cout << black << " " << white << endl;
  }
  return 0;
}