alexjercan/bugnet huggingface dataset:
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000000 + 7
using namespace std;

int H, W;
char fruit[200][200];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void dfs(int h, int w, char c) {
  fruit[h][w] = '.';
  for (int i = 0; i < 4; i++) {
    if (h + dx[i] >= 0 && h + dx[i] <= H && w + dy[i] >= 0 && w + dy[i] <= W &&
        fruit[h + dx[i]][w + dy[i]] == c) {
      dfs(h + dx[i], w + dy[i], c);
    }
  }
}
int main() {
  while (cin >> H >> W && H != 0 && W != 0) {
    string a;
    for (int i = 0; i < H; i++) {
      cin >> a;
      for (int j = 0; j < W; j++) {
        fruit[i][j] = a[j];
      }
    }
    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (fruit[i][j] == '#')
          dfs(i, j, '#'), ans++;
        if (fruit[i][j] == '*')
          dfs(i, j, '*'), ans++;
        if (fruit[i][j] == '@')
          dfs(i, j, '@'), ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000000 + 7
using namespace std;

int H, W;
char fruit[15][15];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

void dfs(int h, int w, char c) {
  fruit[h][w] = '.';
  for (int i = 0; i < 4; i++) {
    if (h + dx[i] >= 0 && h + dx[i] <= H && w + dy[i] >= 0 && w + dy[i] <= W &&
        fruit[h + dx[i]][w + dy[i]] == c) {
      dfs(h + dx[i], w + dy[i], c);
    }
  }
}
int main() {
  while (cin >> H >> W && H != 0 && W != 0) {
    string a;
    for (int i = 0; i < H; i++) {
      cin >> a;
      for (int j = 0; j < W; j++) {
        fruit[i][j] = a[j];
      }
    }
    int ans = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (fruit[i][j] == '#')
          dfs(i, j, '#'), ans++;
        if (fruit[i][j] == '*')
          dfs(i, j, '*'), ans++;
        if (fruit[i][j] == '@')
          dfs(i, j, '@'), ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, m, sum;
char step;
char map[105][105];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void dfs(int x, int y) {
  map[x][y] = '.';
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx >= 0 && xx < n && yy >= 0 && yy < m && map[xx][yy] == step)
      dfs(xx, yy);
  }
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    sum = 0;
    memset(map, '\\0', sizeof(map));
    for (int i = 0; i < n; i++)
      cin >> map[i];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (map[i][j] != '.') {
          step = map[i][j];
          dfs(i, j);
          sum++;
        }
      }
    cout << sum << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
// #include <>
#include <iostream>
using namespace std;
int n, m;
char input[50][50];
bool judge(int x, int y) {
  if (0 <= x && x < n && 0 <= y && y < m && input[x][y] != '.')
    return true;
  return false;
}
void dfs(int x, int y, char o) {
  input[x][y] = '.';
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      int nx = x + dx, ny = y + dy;
      if (judge(nx, ny) && input[nx][ny] == o)
        dfs(nx, ny, o);
    }
  }
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      return 0;
    int ans = 0;
    memset(input, 0, sizeof(input));
    for (int i = 0; i < n; i++)
      scanf("%s", input[i]);
    printf("\\n\\n\\n");
    for (int i = 0; i < n; i++)
      printf("%s\\n", input[i]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (input[i][j] == '#') {
          dfs(i, j, '#');
          ans++;
        }
        if (input[i][j] == '*') {
          dfs(i, j, '*');
          ans++;
        }
        if (input[i][j] == '@') {
          dfs(i, j, '@');
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

//-*-coding:utf-8-*-
//----------------------------------------------------------
// module: main
//----------------------------------------------------------
#include <algorithm>
#include <deque>
#include <map>
#include <vector>

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX_S = 100 + 100;

int w;
int h;

char m[MAX_S][MAX_S]; // map
bool u[MAX_S][MAX_S]; // used

typedef pair<int, int> pos;

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

pos getnext(int i, pos p) {
  p.first += dx[i];
  p.second += dy[i];
  return p;
}

void walk(int x, int y) {
  vector<pos> s;
  s.push_back(pos(x, y));
  char tile = m[y][x];

  while (!s.empty()) {
    pos p = s.back();
    s.pop_back();

    if (u[p.second][p.first])
      continue;
    u[p.second][p.first] = true;

    for (int i = 0; i < 4; ++i) {
      pos next = getnext(i, p);
      if (next.first < 0 || next.first >= w || next.second < 0 ||
          next.second >= h)
        continue;
      if (m[next.second][next.first] != tile)
        continue;

      s.push_back(next);
    }
  }
}

void solve() {
  memset(u, 0, MAX_S * MAX_S);

  int count = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (u[i][j])
        continue;
      count++;
      walk(j, i);
    }
  }
  printf("%d\\n", count);
};

int main(int argc, char *argv[]) {
  while (true) {
    scanf("%d %d", &h, &w);
    if (w <= 0)
      break;
    for (int i = 0; i < h; ++i) {
      scanf("%s", m[i]);
    }
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

//-*-coding:utf-8-*-
//----------------------------------------------------------
// module: main
//----------------------------------------------------------
#include <algorithm>
#include <deque>
#include <map>
#include <vector>

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX_S = 100 + 100;

int w;
int h;

char m[MAX_S][MAX_S]; // map
bool u[MAX_S][MAX_S]; // used

typedef pair<int, int> pos;

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

pos getnext(int i, pos p) {
  p.first += dx[i];
  p.second += dy[i];
  return p;
}

void walk(int x, int y) {
  vector<pos> s;
  s.push_back(pos(x, y));
  char tile = m[y][x];

  while (!s.empty()) {
    pos p = s.back();
    s.pop_back();

    if (u[p.second][p.first])
      continue;
    u[p.second][p.first] = true;

    for (int i = 0; i < 4; ++i) {
      pos next = getnext(i, p);
      if (m[next.second][next.first] != tile)
        continue;

      s.push_back(next);
    }
  }
}

void solve() {
  memset(u, 0, MAX_S * MAX_S);

  int count = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (u[i][j])
        continue;
      count++;
      walk(j, i);
    }
  }
  printf("%d\\n", count);
};

int main(int argc, char *argv[]) {
  while (true) {
    scanf("%d %d", &w, &h);
    if (w <= 0)
      break;
    for (int i = 0; i < h; ++i) {
      scanf("%s", m[i]);
    }
    solve();
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

vector<string> board;
int h, w;

void dfs(int x, int y, char c) {
  board[y][x] = '.';
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (board[ny][nx] == c) {
      dfs(nx, ny, c);
    }
  }
}

int solve() {
  int ret = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (board[i][j] == '.') {
        continue;
      }
      dfs(j, i, board[i][j]);
      ret++;
    }
  }

  return ret;
}

int main() {
  string str;

  while (1) {
    cin >> h >> w;
    if (w == 0 && h == 0) {
      break;
    }
    board.assign(h, "");
    for (int i = 0; i < h; i++) {
      cin >> str;
      board[i] = "." + str + ".";
    }
    board.insert(board.begin(), string(w + 2, '.'));
    board.push_back(string(w + 2, '.'));

    cout << solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

vector<string> board;
int h, w;

void dfs(int x, int y, char c) {
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (board[ny][nx] == c) {
      board[y][x] = '.';
      dfs(nx, ny, c);
    }
  }
}

int solve() {
  int ret = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (board[i][j] == '.') {
        continue;
      }
      char c = board[i][j];
      dfs(j, i, c);
      ret++;
    }
  }

  return ret;
}

int main() {
  string str;

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    board.assign(h, "");
    for (int i = 0; i < h; i++) {
      cin >> str;
      board[i] = "." + str + ".";
    }
    board.insert(board.begin(), string(w + 2, '.'));
    board.push_back(string(w + 2, '.'));

    cout << solve() << endl;
  }

  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

char F[102][102];

void DFS(int Y, int X, char c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = '0';

  DFS(Y - 1, X, c);
  // DFS(Y-1,X+1,c);
  DFS(Y, X + 1, c);
  // DFS(Y+1,X+1,c);
  DFS(Y + 1, X, c);
  // DFS(Y+1,X-1,c);
  DFS(Y, X - 1, c);
  // DFS(Y-1,X-1,c);
}

int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;

    for (int h = 1; h <= N; h++) {
      for (int w = 1; w <= M; w++) {
        cin >> F[h][w];
      }
    }

    int cnt = 0;
    for (int h = 1; h <= N; h++) {
      for (int w = 1; w <= M; w++) {
        if (F[h][w] == '@') {
          cnt++;
          DFS(h, w, '@');
        } else if (F[h][w] == '#') {
          cnt++;
          DFS(h, w, '#');
        } else if (F[h][w] == '*') {
          cnt++;
          DFS(h, w, '*');
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

char F[102][102];

void DFS(int Y, int X, char c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = c;

  DFS(Y - 1, X, c);
  DFS(Y - 1, X + 1, c);
  DFS(Y, X + 1, c);
  // DFS(Y+1,X+1,c);
  DFS(Y + 1, X, c);
  // DFS(Y+1,X-1,c);
  DFS(Y, X - 1, c);
  DFS(Y - 1, X - 1, c);
}

int main() {
  int N, M;

  cin >> N >> M;

  for (int y = 1; y <= N; y++) {
    for (int x = 1; x <= M; x++) {
      cin >> F[y][x];
    }
  }

  int cnt = 0;
  for (int y = 1; y <= N; y++) {
    for (int x = 1; x <= M; x++) {
      if (F[y][x] == '@') {
        cnt++;
        DFS(y, x, '@');
      }
    }
  }
  for (int y = 1; y <= N; y++) {
    for (int x = 1; x <= M; x++) {
      if (F[y][x] == '#') {
        cnt++;
        DFS(y, x, '#');
      }
    }
  }
  for (int y = 1; y <= N; y++) {
    for (int x = 1; x <= M; x++) {
      if (F[y][x] == '*') {
        cnt++;
        DFS(y, x, '*');
      }
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

using namespace std;

#define N_MAX 100
#define M_MAX 100
#define INF '.'

int N, M;
char Field[N_MAX][M_MAX + 1] = {INF};

bool solve(int x, int y) {
  if (Field[x][y] == INF)
    return false;
  char tmp = Field[x][y];
  Field[x][y] = INF;

  int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (nx < 0 || N <= nx || ny < 0 || M <= ny)
      continue;
    if (Field[nx][ny] == tmp)
      solve(nx, ny);
  }
  return true;
}

int main(void) {
  int i, j;
  while (1) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    for (i = 0; i < N; i++)
      scanf("%s", Field[i]);

    int count = 0;
    for (i = 0; i < N; i++) {
      for (j = 0; j < M; j++) {
        if (solve(i, j))
          count++;
      }
    }
    printf("%d\\n", count);
    count = 0;
  }
  return 0;
}

Error: 1

Fixed submission:

#include <cstdio>

using namespace std;

#define N_MAX 100
#define M_MAX 100
#define INF '.'

int N, M;
char Field[N_MAX][M_MAX + 1] = {INF};

bool solve(int x, int y) {
  if (Field[x][y] == INF)
    return false;
  char tmp = Field[x][y];
  Field[x][y] = INF;

  int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (nx < 0 || N <= nx || ny < 0 || M <= ny)
      continue;
    if (Field[nx][ny] == tmp)
      solve(nx, ny);
  }
  return true;
}

int main(void) {
  int i, j;
  while (1) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    for (i = 0; i < N; i++)
      scanf("%s", Field[i]);
    scanf("%d %d", &i, &j);
    if (i != 0 && j != 0)
      return 1;

    int count = 0;
    for (i = 0; i < N; i++) {
      for (j = 0; j < M; j++) {
        if (solve(i, j))
          count++;
      }
    }
    printf("%d", count);
    count = 0;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

char kukaku[101][101];
int H, W;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
void dfs(int x, int y) {
  char ch = kukaku[x][y];
  kukaku[x][y] = '.';
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
      if (kukaku[nx][ny] == ch)
        dfs(nx, ny);
    }
  }
  return;
}

int main() {
  while (1) {
    std::cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int i = 0; i < 101; ++i) {
      for (int j = 0; j < 101; ++j) {
        kukaku[i][j] = '.';
      }
    }
    for (int i = 0; i < H; ++i) {
      std::string s;
      std::cin >> s;
      for (int j = 0; j < W; ++j)
        kukaku[i][j] = s[j];
    }
    int res = 0;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (kukaku[i][j] != '.') {
          dfs(i, j);
          res++;
        }
      }
    }
    std::cout << res << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

char kukaku[101][101];
int H, W;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
void dfs(int x, int y) {
  char ch = kukaku[x][y];
  kukaku[x][y] = '.';
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H) {
      if (kukaku[nx][ny] == ch)
        dfs(nx, ny);
    }
  }
  return;
}

int main() {
  while (1) {
    std::cin >> H >> W;
    if (H == 0 && W == 0)
      break;
    for (int i = 0; i < 101; ++i) {
      for (int j = 0; j < 101; ++j) {
        kukaku[i][j] = '.';
      }
    }
    for (int i = 0; i < H; ++i) {
      std::string s;
      std::cin >> s;
      for (int j = 0; j < W; ++j)
        kukaku[i][j] = s[j];
    }
    int res = 0;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (kukaku[i][j] != '.') {
          dfs(i, j);
          res++;
          for (int k = 0; k < H; ++k) {
            for (int l = 0; l < W; ++l) {
              std::cout << kukaku[k][l];
            }
            std::cout << std::endl;
          }
        }
      }
    }
    std::cout << res << std::endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int W, H;
char field[100][100];

void dfs(int x, int y, char obj) {
  field[x][y] = 'T';

  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      if (dx != 0 && dy != 0)
        continue;
      int nx = x + dx, ny = y + dy;
      if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == obj) {
        dfs(nx, ny, obj);
      }
    }
  }
}

int main(void) {
  while (1) {
    cin >> H, cin >> W;
    if (!W && !H)
      break;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> field[i][j];
      }
    }

    int count = 0;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (field[i][j] == '#' || field[i][j] == '@' || field[i][j] == '*') {
          dfs(i, j, field[i][j]);
          count++;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

int W, H;
char field[30][30];

void dfs(int x, int y, char obj) {
  field[x][y] = 'T';

  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      if (dx != 0 && dy != 0)
        continue;
      int nx = x + dx, ny = y + dy;
      if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == obj) {
        dfs(nx, ny, obj);
      }
    }
  }
}

int main(void) {
  while (1) {
    cin >> H, cin >> W;
    if (!W && !H)
      break;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> field[i][j];
      }
    }

    int count = 0;

    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (field[i][j] == '#' || field[i][j] == '@' || field[i][j] == '*') {
          dfs(i, j, field[i][j]);
          count++;
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX = 100;
int N, M;
char garden[MAX][MAX + 1];
int num;

void dfs(int x, int y, char ch) {
  garden[x][y] = '.';
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      if (abs(dx) + abs(dy) > 1)
        continue;
      int nx = x + dx, ny = y + dy;
      if (nx >= 0 && nx < N && ny >= 0 && ny < M && garden[nx][ny] == ch)
        dfs(nx, ny, ch);
    }
  }
  return;
}

int main() {
  for (;;) {
    cin >> N >> M;
    if (N == 0)
      break;
    memset(garden, '\\0', sizeof(garden));
    for (int i = 0; i < N; i++)
      cin >> garden[i];
    num = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (garden[i][j] != '.') {
          dfs(i, j, garden[i][j]);
          num++;
        }
      }
    }
    cout << num << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX = 20;
int N, M;
char garden[MAX][MAX + 1];
int num;

void dfs(int x, int y, char ch) {
  garden[x][y] = '.';
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      if (abs(dx) + abs(dy) > 1)
        continue;
      int nx = x + dx, ny = y + dy;
      if (nx >= 0 && nx < N && ny >= 0 && ny < M && garden[nx][ny] == ch)
        dfs(nx, ny, ch);
    }
  }
  return;
}

int main() {
  for (;;) {
    cin >> N >> M;
    if (N == 0)
      break;
    memset(garden, '\\0', sizeof(garden));
    for (int i = 0; i < N; i++)
      cin >> garden[i];
    num = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (garden[i][j] != '.') {
          dfs(i, j, garden[i][j]);
          num++;
        }
      }
    }
    cout << num << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
#define MAX_N 100
void makemap(int h);
void seach();
void setblank(int x, int y, char c);
int h, w, ans;
string str[MAX_N];

int main() {
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    ans = 0;
    makemap(h);
    seach();
    cout << ans << endl;
  }
  return 0;
}

void makemap(int h) {
  for (int i = 0; i < h; i++) {
    cin >> str[i];
  }
}

void seach() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (str[i][j] == '#' || str[i][j] == '@' || str[i][j] == '*') {
        ans++;
        setblank(j, i, str[i][j]);
      }
    }
  }
}

void setblank(int x, int y, char c) {
  int movex[] = {0, 1, 0, -1}, movey[] = {-1, 0, 1, 0};
  for (int i = 0; i < 4; i++) {
    int nowx = x + movex[i], nowy = y + movey[i];
    if (nowx >= w || nowx < 0 || nowy >= h || nowy < 0 || str[nowy][nowx] != c)
      ;
    else {
      str[nowy][nowx] = '!';
      setblank(nowx, nowy, c);
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
#define MAX_N 100
void makemap(int h);
void seach();
void setblank(int x, int y, char c);
int h, w, ans;
string str[MAX_N];

int main() {
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    ans = 0;
    makemap(h);
    seach();
    cout << ans << endl;
  }
  return 0;
}

void makemap(int h) {
  for (int i = 0; i < h; i++) {
    cin >> str[i];
  }
}

void seach() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (str[i][j] == '#' || str[i][j] == '@' || str[i][j] == '*') {
        ans++;
        setblank(j, i, str[i][j]);
      }
    }
  }
}

void setblank(int x, int y, char c) {
  int movex[] = {0, 1, 0, -1}, movey[] = {-1, 0, 1, 0};
  for (int i = 0; i < 4; i++) {
    int nowx = x + movex[i], nowy = y + movey[i];
    if (nowx > w || nowx < 0 || nowy > h || nowy < 0 || str[nowy][nowx] != c)
      ;
    else {
      str[nowy][nowx] = '!';
      setblank(nowx, nowy, c);
    }
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

int W, H;
char Maps[120][120];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void dfs(int x, int y);
int ans;

int main(void) {
  while (scanf("%d %d", &H, &W) != EOF && W || H) {

    for (int i = 0; i < H; i++)
      scanf("%s", Maps[i]);

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        if (Maps[i][j] != ' ') {
          dfs(i, j);
          ans++;
        }

    printf("%d\\n", ans);
    ans = 0;
  }
  return 0;
}

void dfs(int x, int y) {
  char tmp = Maps[x][y];
  Maps[x][y] = ' ';
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < H && ny >= 0 && ny < W && Maps[nx][ny] == tmp)
      dfs(nx, ny);
  }
}

Error: 0

Fixed submission:

#include <stdio.h>

int W, H;
char Maps[22][22];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
void dfs(int x, int y);
int ans;

int main(void) {
  while (scanf("%d %d", &H, &W) != EOF) {
    if (W == 0 && H == 0)
      return 0;
    getchar();

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        scanf("%c", &Maps[i][j]);
      getchar();
    }

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        if (Maps[i][j] != '!') {
          dfs(i, j);
          ans++;
        }

    if (W * H != 0)
      printf("%d\\n", ans);
    else
      printf("\\n");
  }
  return 0;
}

void dfs(int x, int y) {
  char tmp = Maps[x][y];
  Maps[x][y] = '!';
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && Maps[nx][ny] == tmp)
      dfs(nx, ny);
  }
}