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

#include <cstdio>
#include <deque>
#include <iostream>
#include <memory.h>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef struct _POINT {
  int x, y;
  _POINT(){};
  _POINT(int _x, int _y) : x(_x), y(_y){};
} POINT;

#define rep(idx, max) for (int idx = 0, idx##Max = (max); idx < idx##Max; idx++)
#define rrep(idx, min, max)                                                    \\
  for (int idx = (min), idx##Max = max; idx < idx##Max; idx++)
#define erep(idx, min, max)                                                    \\
  for (int idx = (min), idx##Max = max; idx <= idx##Max; idx++)

#define rev(idx, max) for (int idx = (max)-1; idx >= 0; idx--)
#define rrev(idx, min, max)                                                    \\
  for (int idx = (max)-1, idx##Min = (min); idx >= idx##Min; idx--)
#define erev(idx, min, max)                                                    \\
  for (int idx = (max), idx##Min = (min); idx >= idx##Min; idx--)

#define foreach(it, obj)                                                       \\
  for (auto it = obj.begin(), it##End = obj.end(); it >= it##End; it++)

#define MAX_N 128

int g_width, g_height;           //! ??????????????????
char g_map[MAX_N][MAX_N];        //! ??????????????±
char g_mapVisited[MAX_N][MAX_N]; //! ?????????????¨?????????????

///
/// ???????????¨??? (for debugging)
///
void PrintMap() {
  erep(y, 0, g_height + 1) {
    erep(x, 0, g_width + 1) { ::printf("%c", g_map[y][x]); }
    ::puts("");
  }
  ::puts("");
}

///
/// ??????????????±??\\???
///
void InitMap() {
  ::memset(g_map, 'x', sizeof(g_map));
  ::memset(g_mapVisited, 0, sizeof(g_mapVisited));

  // erep(y, 1, g_height)	erep(x, 1, g_width)	{	std::cin >> g_map[y][x];
  // }
  erep(y, 1, g_height) {
    ::scanf("%s", g_map[y] + 1);
    g_map[y][g_width + 1] = 'x';
  }
  // PrintMap();
}

///
/// BFS??§?????????????????£?¶??????????????¨?????????°?????????????????°??§???????????¶???
///
void BfsFill(POINT start) {
  static int dx[] = {0, +1, 0, -1};
  static int dy[] = {+1, 0, -1, 0};
  //	static POINT dr[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  deque<POINT> que;

  int field = g_map[start.y][start.x];
  que.push_back(start);

  while (que.size()) {
    rep(i, que.size()) {
      POINT p = que.front();
      g_mapVisited[p.y][p.x] = true;
      rep(n, 4) {
        POINT pn(p.x + dx[n], p.y + dy[n]);
        if (g_map[pn.y][pn.x] == field && !g_mapVisited[pn.y][pn.x]) {
          que.push_back(pn);
        }
      }
      que.pop_front();
    }
  }
}

///
/// DFS??§?????????????????£?¶??????????????¨?????????°?????????????????°??§???????????¶???
///
void DfsFill(POINT start) {
  static int dx[] = {0, +1, 0, -1};
  static int dy[] = {+1, 0, -1, 0};
  //	static POINT dr[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  stack<POINT> stk;

  int field = g_map[start.y][start.x];
  stk.push(start);

  while (stk.size()) {
    POINT p = stk.top();
    stk.pop();
    g_mapVisited[p.y][p.x] = true;
    rep(n, 4) {
      POINT pn(p.x + dx[n], p.y + dy[n]);
      if (g_map[pn.y][pn.x] == field && !g_mapVisited[pn.y][pn.x]) {
        stk.push(pn);
      }
    }
  }
}

///
/// ?§£???
///
int Solve() {
  int count = 0;
  erep(y, 1, g_height) erep(x, 1, g_width) {
    // ???????????¶?????????????????????
    if (g_mapVisited[y][x]) {
      continue;
    }
    // ??°?????????????????¶?????¨???????????????
    count++;
    ::DfsFill(POINT(x, y));
  }
  return (count);
}

int main() {
  while (std::cin >> g_height >> g_width, g_width) {
    ::InitMap();
    cout << ::Solve() << endl;
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <deque>
#include <iostream>
#include <memory.h>
#include <string>
#include <vector>
using namespace std;

typedef struct _POINT {
  int x, y;
  _POINT(){};
  _POINT(int _x, int _y) : x(_x), y(_y){};
} POINT;

#define rep(idx, max) for (int idx = 0, idx##Max = (max); idx < idx##Max; idx++)
#define rrep(idx, min, max)                                                    \\
  for (int idx = (min), idx##Max = max; idx < idx##Max; idx++)
#define erep(idx, min, max)                                                    \\
  for (int idx = (min), idx##Max = max; idx <= idx##Max; idx++)

#define rev(idx, max) for (int idx = (max)-1; idx >= 0; idx--)
#define rrev(idx, min, max)                                                    \\
  for (int idx = (max)-1, idx##Min = (min); idx >= idx##Min; idx--)
#define erev(idx, min, max)                                                    \\
  for (int idx = (max), idx##Min = (min); idx >= idx##Min; idx--)

#define foreach(it, obj)                                                       \\
  for (auto it = obj.begin(), it##End = obj.end(); it >= it##End; it++)

#define MAX_N 128

int g_width, g_height;           //! ??????????????????
char g_map[MAX_N][MAX_N];        //! ??????????????±
char g_mapVisited[MAX_N][MAX_N]; //! ?????????????¨?????????????

///
/// ???????????¨??? (for debugging)
///
void PrintMap() {
  erep(y, 1, g_height) {
    erep(x, 1, g_width) { ::printf("%02d", g_map[y][x]); }
    ::puts("");
  }
  ::puts("");
}

///
/// ??????????????±??\\???
///
void InitMap() {
  ::memset(g_map, -1, sizeof(g_map));
  ::memset(g_mapVisited, 0, sizeof(g_mapVisited));

  // erep(y, 1, g_height)	erep(x, 1, g_width)	{	std::cin >> g_map[y][x];
  // }
  erep(y, 1, g_height) {
    char szLine[MAX_N];
    ::scanf(
        "%s",
        szLine +
            1); // '+1':
                // ?¬???????????????§???1???????§????????????????szLine??????1st????´???????????´????0th????´????????????±???
    erep(x, 1, g_width) { g_map[y][x] = szLine[x]; }
  }
}

///
/// ?????????????????£?¶??????????????¨?????????°?????????????????°??§???????????¶???
///
void BfsFill(POINT start) {
  static int dx[] = {0, +1, 0, -1};
  static int dy[] = {+1, 0, -1, 0};
  //	static POINT dr[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  deque<POINT> que;

  int field = g_map[start.y][start.x];
  que.push_back(start);

  while (que.size()) {
    rep(i, que.size()) {
      POINT p = que.front();
      g_mapVisited[p.y][p.x] = true;
      rep(n, 4) {
        POINT pn(p.x + dx[n], p.y + dy[n]);
        if (g_map[pn.y][pn.x] == field && !g_mapVisited[pn.y][pn.x]) {
          que.push_back(pn);
        }
      }
      que.pop_front();
    }
  }
}

///
/// ?§£???
///
int Solve() {
  int count = 0;
  erep(y, 1, g_height) erep(x, 1, g_width) {
    // ???????????¶?????????????????????
    if (g_mapVisited[y][x]) {
      continue;
    }
    // ??°?????????????????¶?????¨???????????????
    count++;
    ::BfsFill(POINT(x, y));
  }
  return (count);
}

int main() {
  while (std::cin >> g_height >> g_width, g_width) {
    ::InitMap();
    cout << ::Solve() << endl;
  }
}