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

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

using namespace std;

int H, W;

void saiki(vector<vector<char>> &map, int i, int j, char c) {

  map[i][j] = ' ';
  int dx = j + 1;
  int dy = i + 1;
  int mdx = j - 1;
  int mdy = i - 1;
  if (dy >= 0 && dy < H && map[dy][j] == c)
    saiki(map, dy, j, c);
  if (mdy >= 0 && mdy < H && map[mdy][j] == c)
    saiki(map, mdy, j, c);
  if (dx >= 0 && dx < W && map[i][dx] == c)
    saiki(map, i, dx, c);
  if (mdx >= 0 && mdx < W && map[i][mdx] == c)
    saiki(map, i, mdx, c);
}

int main() {

  bool flag = true;
  char temp;
  vector<vector<char>> map;
  while (flag) {
    int count = 0;
    cin >> H >> W;
    if (H == 0 && W == 0)
      flag = false;
    else {
      for (int i = 0; i < H; i++) {
        map.push_back(vector<char>());
        for (int j = 0; j < W; j++) {
          cin >> temp;
          map[i].push_back(temp);
        }
      }

      char c;
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          c = map[i][j];
          if (c != ' ') {
            count++;
            saiki(map, i, j, c);
          }
        }
      }
      cout << count << endl;
      map.clear();
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

int H, W;

void saiki(vector<vector<char>> &map, int i, int j, char c) {

  map[i][j] = ' ';
  int dx = j + 1;
  int dy = i + 1;
  int mdx = j - 1;
  int mdy = i - 1;
  if (dx >= 0 && dx < H && map[dy][j] == c)
    saiki(map, dy, j, c);
  if (mdx >= 0 && mdx < H && map[mdy][j] == c)
    saiki(map, mdy, j, c);
  if (dx >= 0 && dx < H && map[i][dx] == c)
    saiki(map, i, dx, c);
  if (mdx >= 0 && mdx < H && map[i][mdx] == c)
    saiki(map, i, mdx, c);
}

int main() {

  bool flag = true;
  char temp;
  vector<vector<char>> map;
  while (flag) {
    int count = 0;
    cin >> H >> W;
    if (H == 0 && W == 0)
      flag = false;
    for (int i = 0; i < H; i++) {
      map.push_back(vector<char>());
      for (int j = 0; j < W; j++) {
        cin >> temp;
        map[i].push_back(temp);
      }
    }

    char c;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        c = map[i][j];
        if (c != ' ') {
          count++;
          saiki(map, i, j, c);
        }
      }
    }
    cout << count << endl;
    map.clear();
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

const int MAX_H = 100, MAX_W = 100,
          v[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::string map[MAX_H];
int W, H;

// x,y »ÝÌÊu c Á·¶
void dfs(int x, int y, char c) {
  map[y][x] = ' ';

  for (int i = 0; i < 4; i++) {
    int nx = x + v[i][0], ny = y + v[i][1];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && map[ny][nx] == c)
      dfs(nx, ny, c);
  }
  return;
}

int main() {
  // Input
  while (std::cin >> H >> W, W || H) {
    for (int i = 0; i < H; i++) {
      std::cin >> map[i];
    }

    // Processing
    int res = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (map[i][j] != ' ')
          res++, dfs(j, i, map[i][j]);
      }
    }

    std::cout << res << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

const int MAX_H = 100, MAX_W = 100,
          v[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::string map[MAX_H];
int W, H;

// x,y »ÝÌÊu c Á·¶
void dfs(int x, int y, char c) {
  map[y][x] = ' ';

  for (int i = 0; i < 4; i++) {
    int nx = x + v[i][0], ny = y + v[i][1];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && map[ny][nx] == c)
      dfs(nx, ny, c);
  }
  return;
}

int main() {
  // Input
  while (std::cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      std::cin >> map[i];
    }

    // Processing
    int res = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (map[i][j] != ' ')
          res++, dfs(j, i, map[i][j]);
      }
    }

    std::cout << res << std::endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

char F[102][102];

void DFS(int y, int x, char a) {
  if (F[y][x] != a) {
    return;
  }
  F[y][x] = '.';

  DFS(y - 1, x, a);
  DFS(y, x + 1, a);
  DFS(y + 1, x, a);
  DFS(y, x - 1, a);
}

int main() {
  int n, m;
  int cnt = 0;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      return 0;
    }
    for (int y = 1; y <= n; y++) {
      for (int x = 1; x <= m; x++) {
        cin >> F[y][x];
      }
    }
    cnt = 0;
    for (int y = 1; y <= n; y++) {
      for (int x = 1; x <= m; x++) {
        if (F[y][x] == '#') {
          cnt++;
          DFS(y, x, '#');
        }
        if (F[y][x] == '*') {
          cnt++;
          DFS(y, x, '*');
        }
        if (F[y][x] == '@') {
          cnt++;
          DFS(y, x, '@');
        }
      }
    }
    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

char F[102][102];

void DFS(int y, int x, char a) {
  if (F[y][x] != a) {
    return;
  }
  F[y][x] = '.';

  DFS(y - 1, x, a);
  DFS(y, x + 1, a);
  DFS(y + 1, x, a);
  DFS(y, x - 1, a);
}

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      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, '#');
          }
          if (F[y][x] == '*') {
            cnt++;
            DFS(y, x, '*');
          }
          if (F[y][x] == '@') {
            cnt++;
            DFS(y, x, '@');
          }
        }
      }
      cout << cnt << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const char kind[] = {'#', '@', '*'};

int INF = 1 << 10;

vector<string> field(101);

void display(int h, int w) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << field[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}

void dfs(int h, int w, char c, P s) {
  field[s.first][s.second] = '.';
  for (int i = 0; i < 4; i++) {
    int nx = s.second + dx[i], ny = s.first + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] == c) {
      dfs(h, w, c, P(ny, nx));
    }
  }
}

P getStart(int h, int w, char c) {
  bool flag = false;
  P res(INF, INF);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == c) {
        res.first = i;
        res.second = j;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  return res;
}

int main() {
  int h, w;

  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;

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

    int ans = 0;
    for (int i = 0; i < 3; i++) {
      char c = kind[i];
      while (1) {
        P s = getStart(h, w, c);
        if (s == P(INF, INF))
          break;
        dfs(h, w, c, s);
        ans++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const char kind[] = {'#', '@', '*'};

int INF = 1 << 10;

vector<string> field;

void display(int h, int w) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << field[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}

void dfs(int h, int w, char c, P s) {
  field[s.first][s.second] = '.';
  for (int i = 0; i < 4; i++) {
    int nx = s.second + dx[i], ny = s.first + dy[i];
    if (0 <= nx && nx < w && 0 <= ny && ny < h && field[ny][nx] == c) {
      dfs(h, w, c, P(ny, nx));
    }
  }
}

P getStart(int h, int w, char c) {
  bool flag = false;
  P res(INF, INF);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == c) {
        res.first = i;
        res.second = j;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  return res;
}

int main() {
  int h, w;

  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;

    for (int i = 0; i < h; i++) {
      string tmp;
      cin >> tmp;
      field.push_back(tmp);
    }

    int ans = 0;
    for (int i = 0; i < 3; i++) {
      char c = kind[i];
      while (1) {
        P s = getStart(h, w, c);
        if (s == P(INF, INF))
          break;
        dfs(h, w, c, s);
        ans++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

#define MAX_W 100
#define MAX_H 100

char room[MAX_W][MAX_H];
int W, H;

const int direc[4][2] = {
    {0, -1},
    {1, 0},
    {0, 1},
    {-1, 0},
};

void dfs(const int &row, const int &col, const char c);

int main() {
  while (cin >> H >> W, W > 0) {
    int step = 0;
    int col, row;
    for (row = 0; row < H; ++row) {
      for (col = 0; col < W; ++col) {
        cin >> room[row][col];
      }
    }
    for (row = 0; row < H; ++row) {
      for (col = 0; col < W; ++col) {
        if (room[row][col] != '!') {
          dfs(row, col, room[row][col]);
          ++step;
        }
      }
    }
    cout << step << endl;
  }
  return 0;
}

void dfs(const int &row, const int &col, const char c) {
  room[row][col] = '!';
  for (int d = 0; d < 4; ++d) {
    int curRow = row + direc[d][1];
    int curCol = col + direc[d][0];
    if (curRow >= 0 && curRow < H && curCol >= 0 && curCol < W &&
        room[curRow][curCol] == c) {
      dfs(curRow, curCol, c);
    }
  }
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> p;

int w, h;
int movement[3] = {-1, 0, 1};

int main() {
  while (cin >> w >> h) {
    if (!w && !h)
      break;

    char map[w][h], dump, cur;
    for (int i = 0; i < w; ++i) {
      for (int j = 0; j < h; ++j) {
        cin >> map[i][j];
      }
    }
    /*for(int i=0; i<w; ++i){
        for(int j=0; j<h; ++j){
            cout << map[i][j];
        }
        cout << endl;
    }*/

    queue<p> myqueue;
    int counter = 0;

    for (int i = 0; i < w; ++i) {
      for (int j = 0; j < h; ++j) {
        if (map[i][j] == 'E')
          continue;
        cur = map[i][j];
        p t = make_pair(i, j);
        myqueue.push(t);
        while (!myqueue.empty()) {
          t = myqueue.front();
          myqueue.pop();
          map[t.first][t.second] = 'E';
          int curX = t.first, curY = t.second;
          for (int x = 0; x < 3; ++x) {
            for (int y = 0; y < 3; ++y) {
              if (movement[x] != 0 && movement[y] != 0)
                continue;
              if (curX + movement[x] >= 0 && curY + movement[y] >= 0 &&
                  curX + movement[x] < w && curY + movement[y] < h &&
                  map[curX + movement[x]][curY + movement[y]] == cur) {
                myqueue.push(make_pair(curX + movement[x], curY + movement[y]));
              }
            }
          }
        }
        /*cout << endl;
        for(int q=0; q<w; ++q){
            for(int r=0; r<h; ++r){
                cout << map[q][r];
            }
            cout << endl;
        }
        cout << endl;*/
        counter++;
      }
    }
    cout << counter << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

int main() {
  int w, h, cnt;
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  char m[100][101];
  queue<pii> q;
  for (;;) {
    scanf("%d%d", &h, &w);
    if (h == 0 && w == 0)
      break;
    cnt = 0;
    rep(i, h) scanf("%s", m[i]);
    rep(i, h) rep(j, w) {
      if (m[i][j] != 0) {
        char c = m[i][j];
        cnt++;
        m[i][j] = 0;
        q.push(pii(i, j));
        while (!q.empty()) {
          int x = q.front().second, y = q.front().first;
          q.pop();
          rep(k, 4) {
            if (in(x + dx[k], 0, w - 1) && in(y + dy[k], 0, h - 1) &&
                m[y + dy[k]][x + dx[k]] == c) {
              m[y + dy[k]][x + dx[k]] = 0;
              q.push(pii(y + dy[k], x + dx[k]));
            }
          }
        }
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

int ans;
char f[101][101];
bool b[101][101];
int wi, he;

void saiki(int w, int h, char c) {
  if (b[h][w])
    return;
  if (c != f[h][w]) {
    c = f[h][w];
    ans++;
  }
  b[h][w] = true;
  if (w == wi - 1 && h == he - 1)
    return;
  if (w != 0)
    if (c == f[h][w - 1])
      saiki(w - 1, h, c);
  if (h != 0)
    if (c == f[h - 1][w])
      saiki(w, h - 1, c);
  if (w != wi - 1)
    if (c == f[h][w + 1])
      saiki(w + 1, h, c);
  if (h != he) {
    if (c == f[h + 1][w])
      saiki(w, h + 1, c);
  }
}

int main(void) {
  int i, j;
  for (;;) {
    scanf("%d%d", &wi, &he);
    if (wi == 0 || he == 0)
      break;

    for (i = 0; i < 101; i++) {
      for (j = 0; j < 101; j++) {
        b[i][j] = false;
      }
    }
    ans = 0;

    for (i = 0; i < he; i++) {
      scanf("%s", &f[i]);
    }

    for (i = 0; i < he; i++)
      for (j = 0; j < wi; j++)
        saiki(i, j, 0);
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

//
// Created by jchen on 18-7-4.
// 　　aoj　0118 深度优先搜索

// 给一个三种字母组成的区域,判断有多少个区域
#include <iostream>

#define MAX_H 100
#define MAX_W 100

using namespace std;

int h, w;

char field[MAX_H][MAX_W];

int a[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};

void dfs(int x, int y, char t) {
  field[x][y] = 'a';
  for (int i = 0; i < 4; i++) {
    int nx = x + a[i][0];
    int ny = y + a[i][1];

    if (nx >= 0 && ny >= 0 && nx < h && ny < w && field[nx][ny] == t) {
      dfs(nx, ny, t);
    }
  }
}
// 遍历过的部分全都换成'a'
int main() {

  while (cin >> h >> w) {
    int res = 0;
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != 'a') {
          res++;
          dfs(i, j, field[i][j]);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

//
// Created by jchen on 18-7-4.
// 　Read and Black 杭电oj　深度优先搜索

#include <iostream>

#define MAX_W 20
#define MAX_H 20

using namespace std;

int w, h;

char field[MAX_W][MAX_H];

int res = 0;
int a[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};

void dfs(int x, int y) {
  res++;
  field[x][y] = '#';
  for (int i = 0; i < 4; i++) {
    int nx = x + a[i][0];
    int ny = y + a[i][1];

    if (nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] == '.') {
      dfs(nx, ny);
    }
  }
}

int main() {
  while (cin >> w >> h) {
    res = 0;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[i][j];
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] == '@') {
          dfs(i, j);
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int H, W, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char c[100][100], sc;

int main() {
  while (cin >> H >> W) {
    if (!H)
      return 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    queue<P> q;
    int ans = 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (c[i][j] != '.') {
          q.push(P(i, j));
          sc = c[i][j];
          ans++;
        }
        while (!q.empty()) {
          int sy = q.front().first, sx = q.front().second;
          q.pop();
          c[sy][sx] = '.';
          // cout<<"("<<sy<<","<<sx<<")"<<sc<<endl;
          for (int i = 0; i < 4; i++) {
            int ty = sy + dy[i], tx = sx + dx[i];
            if (ty < 0 || ty >= H || tx < 0 || tx >= W || c[ty][tx] != sc)
              continue;
            q.push(P(ty, tx));
            c[ty][tx] = '.';
          }
          /*for(int i=0;i<H;i++){
                  for(int j=0;j<W;j++)cout<<c[i][j];cout<<endl;
          }*/
        }
      }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int H, W, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char c[100][100], sc;

int main() {
  while (cin >> H >> W) {
    if (!H)
      return 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    queue<P> q;
    int ans = 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (c[i][j] != '.') {
          q.push(P(i, j));
          sc = c[i][j];
          ans++;
        }
        while (!q.empty()) {
          int sy = q.front().first, sx = q.front().second;
          q.pop();
          c[sy][sx] = '.';
          // cout<<"("<<sy<<","<<sx<<")"<<sc<<endl;
          for (int i = 0; i < 4; i++) {
            int ty = sy + dy[i], tx = sx + dx[i];
            if (ty < 0 || ty >= H || tx < 0 || tx >= W || c[ty][tx] != sc)
              continue;
            q.push(P(ty, tx));
          }
          /*for(int i=0;i<H;i++){
                  for(int j=0;j<W;j++)cout<<c[i][j];cout<<endl;
          }*/
        }
      }
    cout << ans << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

/*
 * Filename:    aoj0118.cpp
 * Desciption:  DFS
 * Created:     2016-03-02
 * Author:      JIngwei Xu [mail:xu_jingwei@outlook.com]
 *
 */
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
#define INT_MAX 2 << 30
using namespace std;
// typedef long long ll;
int n, m, ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
char maze[105][105];
void dfs(int x, int y, char s) {

  int tx, ty;
  maze[x][y] = 0;
  for (int i = 0; i < 4; i += 1) {
    tx = x + dx[i];
    ty = y + dy[i];
    if (tx < 0 || ty < 0 || tx >= n || ty >= m)
      continue;
    if (maze[tx][ty] == s)
      dfs(tx, ty, s);
  }
}
int main() {
  while (scanf("%d%d", &n, &m) && n && m) {
    for (int i = 0; i < n; i += 1) {
      scanf("%s", maze[i]);
    }
    ans = 0;
    for (int i = 0; i < n; i += 1) {
      for (int j = 0; j < m; j += 1) {
        if (maze[i][j] != 0) {
          dfs(i, j, maze[i][j]);
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

/*
 * Filename:    aoj0118.cpp
 * Desciption:  DFS
 * Created:     2016-03-02
 * Author:      JIngwei Xu [mail:xu_jingwei@outlook.com]
 *
 */
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
#define INT_MAX 2 << 30
using namespace std;
// typedef long long ll;
int n, m, ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
char maze[30][30];
void dfs(int x, int y, char s) {

  int tx, ty;
  maze[x][y] = 0;
  for (int i = 0; i < 4; i += 1) {
    tx = x + dx[i];
    ty = y + dy[i];
    if (tx < 0 || ty < 0 || tx >= n || ty >= m)
      continue;
    if (maze[tx][ty] == s)
      dfs(tx, ty, s);
  }
}
int main() {
  while (scanf("%d%d", &n, &m) && n && m) {
    for (int i = 0; i < n; i += 1) {
      scanf("%s", maze[i]);
    }
    ans = 0;
    for (int i = 0; i < n; i += 1) {
      for (int j = 0; j < m; j += 1) {
        if (maze[i][j] != 0) {
          dfs(i, j, maze[i][j]);
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int tate = 0, yoko = 0;
vector<vector<char>> kukaku;
char kudamono;
void kazyuen(int a, int b) {
  kukaku[a][b] = 0;
  if (a - 1 > -1 && kukaku[a - 1][b] == kudamono)
    kazyuen(a - 1, b);
  if (a + 1 < tate && kukaku[a + 1][b] == kudamono)
    kazyuen(a + 1, b);
  if (b - 1 > -1 && kukaku[a][b - 1] == kudamono)
    kazyuen(a, b - 1);
  if (b + 1 < yoko && kukaku[a][b + 1] == kudamono)
    kazyuen(a, b + 1);
}
int main() {
  int a, b;
  while (cin >> a >> b, a | b) {
    kukaku.clear();
    tate = a;
    yoko = b;
    for (int c = 0; c < a; c++) {
      vector<char> h(b);
      kukaku.push_back(h);
      string e;
      cin >> e;
      for (int d = 0; d < b; d++) {
        kukaku[c][d] = e[d];
      }
    }
    int s = 0;
    for (int l = 0; l < a; l++) {
      for (int r = 0; r < b; r++) {
        if (kukaku[l][r]) {
          s++;
          kudamono = kukaku[l][r];
          kazyuen(l, r);
        }
      }
    }
    cout << s << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int tate = 0, yoko = 0;
vector<vector<char>> kukaku;
char kudamono;
void kazyuen(int, int);
int main() {
  kukaku.clear();
  int a, b;
  while (cin >> a >> b, a | b) {
    tate = a;
    yoko = b;
    for (int c = 0; c < a; c++) {
      vector<char> h(b);
      kukaku.push_back(h);
      string e;
      cin >> e;
      for (int d = 0; d < b; d++) {
        kukaku[c][d] = e[d];
      }
    }
    int s = 0;
    for (int l = 0; l < a; l++) {
      for (int r = 0; r < b; r++) {
        if (kukaku[l][r]) {
          s++;
          kudamono = kukaku[l][r];
          kazyuen(l, r);
        }
      }
    }
    cout << s << endl;
  }
}
void kazyuen(int a, int b) {
  kukaku[a][b] = 0;
  if (a - 1 > -1 && kukaku[a - 1][b] == kudamono)
    kazyuen(a - 1, b);
  if (a + 1 < tate && kukaku[a + 1][b] == kudamono)
    kazyuen(a + 1, b);
  if (b - 1 > -1 && kukaku[a][b - 1] == kudamono)
    kazyuen(a, b - 1);
  if (b + 1 < yoko && kukaku[a][b + 1] == kudamono)
    kazyuen(a, b + 1);
}