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

#include <algorithm>
#include <cstdio>
#define MAX_WH 100
using namespace std;

int w, h; // h:i,w:j

char board[MAX_WH][MAX_WH];
bool check[MAX_WH][MAX_WH];

int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};

void back(char c, int i, int j) {
  check[i][j] = true;
  for (int k = 0; k < 4; k++) {
    int pi, pj;
    pi = i + vx[k];
    pj = j + vy[k];
    if (-1 < pi && pi < h && -1 < pj && pj < w) {
      if (board[pi][pj] == c && !check[pi][pj]) {
        back(c, pi, pj);
      }
    }
  }
}

int solve() {
  int sum = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      check[i][j] = false;
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!check[i][j]) {
        check[i][j] = true;
        back(board[i][j], i, j);
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  while (1) {
    scanf(" %d %d", &h, &w);
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf(" %c", &board[i][j]);
      }
    }
    printf("%d\\n", solve());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#define MAX_WH 20
#define MAX_VALUE 10000
using namespace std;

int w, h; // h:i,w:j

char board[MAX_WH][MAX_WH];
bool check[MAX_WH][MAX_WH];

int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};

void back(char c, int i, int j) {
  check[i][j] = true;
  for (int k = 0; k < 4; k++) {
    int pi, pj;
    pi = i + vx[k];
    pj = j + vy[k];
    if (-1 < pi && pi < h && -1 < pj && pj < w) {
      if (board[pi][pj] == c && !check[pi][pj]) {
        back(c, pi, pj);
      }
    }
  }
}

int solve() {
  int sum = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      check[i][j] = false;
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!check[i][j]) {
        check[i][j] = true;
        back(board[i][j], i, j);
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf(" %c", &board[i][j]);
      }
    }
    printf("%d\\n", solve());
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
#define fi first
#define se second

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int m[101][101] = {0};
int main() {
  int h, w;
  char c;
  while (cin >> h >> w, h || w) {
    memset(m, 0, sizeof(m));
    cin.ignore();
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> c;
        if (c == '#')
          m[i][j] = 0;
        if (c == '*')
          m[i][j] = 1;
        if (c == '@')
          m[i][j] = 2;
      }
      cin.ignore();
    }

    int ans = 0, now;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (m[i][j] != -1) {
          queue<pii> qu;
          now = m[i][j];
          ans++;
          for (qu.push(pii(j, i)); !qu.empty();) {
            pii pos = qu.front();
            qu.pop();
            if (m[pos.se][pos.fi] == -1)
              continue;
            m[pos.se][pos.fi] = -1;
            for (int k = 0; k < 4; ++k) {
              if (pos.fi + dx[k] >= 0 && pos.fi + dx[k] < w &&
                  pos.se + dy[k] >= 0 && pos.se + dy[k] < h &&
                  m[pos.se + dy[k]][pos.fi + dx[k]] == now) {
                qu.push(pii(pos.fi + dx[k], pos.se + dy[k]));
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;

typedef pair<int, int> pii;
#define fi first
#define se second

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int m[100][100] = {0};
int main() {
  int h, w;
  char c;
  while (cin >> h >> w, h || w) {
    memset(m, 0, sizeof(m));
    cin.ignore();
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> c;
        if (c == '#')
          m[i][j] = 0;
        if (c == '*')
          m[i][j] = 1;
        if (c == '@')
          m[i][j] = 2;
      }
      cin.ignore();
    }

    int ans = 0, now;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (m[i][j] != -1) {
          queue<pii> qu;
          now = m[i][j];
          ans++;
          for (qu.push(pii(j, i)); !qu.empty();) {
            pii pos = qu.front();
            qu.pop();
            m[pos.se][pos.fi] = -1;
            for (int k = 0; k < 4; ++k)
              if (pos.fi + dx[k] >= 0 && pos.fi + dx[k] < w &&
                  pos.se + dy[k] >= 0 && pos.se + dy[k] < h &&
                  m[pos.se + dy[k]][pos.fi + dx[k]] == now)
                qu.push(pii(pos.fi + dx[k], pos.se + dy[k]));
          }
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX_N 2000

int x[MAX_N][MAX_N];
int col[MAX_N][MAX_N], cnt;
int h, w;

queue<int> Q;

void bfs(int c) {
  while (!Q.empty()) {
    int a = Q.front() / 10000;
    int b = Q.front() % 10000;
    Q.pop();
    if (x[a][b] == x[a - 1][b] && col[a - 1][b] == 0) {
      col[a - 1][b] = cnt;
      Q.push((a - 1) * 10000 + b);
    }
    if (x[a][b] == x[a + 1][b] && col[a + 1][b] == 0) {
      col[a + 1][b] = cnt;
      Q.push((a + 1) * 10000 + b);
    }
    if (x[a][b] == x[a][b - 1] && col[a][b - 1] == 0) {
      col[a][b - 1] = cnt;
      Q.push(a * 10000 + (b - 1));
    }
    if (x[a][b] == x[a][b + 1] && col[a][b + 1] == 0) {
      col[a][b + 1] = cnt;
      Q.push(a * 10000 + (b + 1));
    }
  }
}

int main() {
  while (true) {
    memset(col, 0, sizeof(col));
    memset(x, 0, sizeof(x));
    cin >> h >> w;
    if (h == 0 && w == 0) {
      break;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        char c;
        cin >> c;
        if (c == '@') {
          x[i][j] = 1;
        }
        if (c == '#') {
          x[i][j] = 2;
        }
        if (c == '*') {
          x[i][j] = 3;
        }
      }
    }
    cnt = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (col[i][j] == 0) {
          Q.push(i * 10000 + j);
          cnt++;
          col[i][j] = cnt;
          bfs(x[i][j]);
        }
      }
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

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

#define MAX_N 2000

int x[MAX_N][MAX_N];
int col[MAX_N][MAX_N], cnt;
int h, w;

queue<int> Q;

void bfs(int c) {
  while (!Q.empty()) {
    int a = Q.front() / 10000;
    int b = Q.front() % 10000;
    Q.pop();
    if (x[a][b] == x[a - 1][b] && col[a - 1][b] == 0) {
      col[a - 1][b] = cnt;
      Q.push((a - 1) * 10000 + b);
    }
    if (x[a][b] == x[a + 1][b] && col[a + 1][b] == 0) {
      col[a + 1][b] = cnt;
      Q.push((a + 1) * 10000 + b);
    }
    if (x[a][b] == x[a][b - 1] && col[a][b - 1] == 0) {
      col[a][b - 1] = cnt;
      Q.push(a * 10000 + (b - 1));
    }
    if (x[a][b] == x[a][b + 1] && col[a][b + 1] == 0) {
      col[a][b + 1] = cnt;
      Q.push(a * 10000 + (b + 1));
    }
  }
}

int main() {
  while (true) {
    memset(col, 0, sizeof(col));
    memset(x, 0, sizeof(x));
    cin >> h >> w;
    if (h == 0 && w == 0) {
      break;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 0; j <= w; j++) {
        char c;
        cin >> c;
        if (c == '@') {
          x[i][j] = 1;
        }
        if (c == '#') {
          x[i][j] = 2;
        }
        if (c == '*') {
          x[i][j] = 3;
        }
      }
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (col[i][j] == 0) {
          Q.push(i * MAX_N + j);
          cnt++;
          col[i][j] = cnt;
          bfs(x[i][j]);
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
#define ms(a) memset(a, '\\0', sizeof(a))

int x, y, ans, l, p, m;
char map[105][105];

void dfs(int xx, int yy, char c) {
  if (xx >= 0 && xx < x && yy >= 0 && yy < y && map[xx][yy] == c) {
    map[xx][yy] = '.';
  } else
    return;
  dfs(xx - 1, yy, c);
  dfs(xx + 1, yy, c);
  dfs(xx, yy + 1, c);
  dfs(xx, yy - 1, c);
}

int main() {
  // freopen("Aizu0118.txt","r",stdin);
  scanf("%d %d", &x, &y);
  while (!(x == 0 && y == 0)) {
    l = p = m = 0;
    ans = 0;
    getchar();
    for (int i = 0; i < x; i++) {
      gets(map[i]);
    }
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        if (map[i][j] != '.') {
          dfs(i, j, map[i][j]);
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
    scanf("%d %d", &x, &y);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define H_size 100
#define W_size 100

using namespace std;

size_t num = 0;
char list[H_size][W_size];

void getNum(char (*a)[W_size], char c, size_t i, size_t j, size_t &W,
            size_t &H) {
  if (i < 0 || j < 0 || i >= H || j >= W || a[i][j] != c) {
    return;
  }
  if (a[i][j] == c) {
    a[i][j] = '.';
    getNum(a, c, i - 1, j, W, H);
    getNum(a, c, i, j + 1, W, H);
    getNum(a, c, i + 1, j, W, H);
    getNum(a, c, i, j - 1, W, H);
    return;
  }
}

int main() {

  vector<int> result;
  size_t W, H;

  while (true) {
    num = 0;
    cin >> W >> H;
    getchar();
    if (W == 0 && H == 0) {
      break;
    } else {
      for (size_t i = 0; i < H; ++i) {
        cin.getline(list[i], W + 1);
      }
    }
    for (size_t i = 0; i < H; ++i) {
      for (size_t j = 0; j < W; ++j) {
        if (list[i][j] != '.') {
          getNum(list, list[i][j], i, j, W, H);
          num++;
        }
      }
    }
    cout << num << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int count;
void bfs(char (*sz)[101], int r, int c, int n, int m, char k) {
  if (r < 0 || r >= n || c < 0 || c >= m)
    return;
  if (sz[r][c] == '.' || sz[r][c] != k)
    return;
  sz[r][c] = '.';
  for (int dr = -1; dr <= 1; dr++)
    for (int dc = -1; dc <= 1; dc++) {
      if ((dr != 0 || dc != 0) && dr * dc == 0)
        bfs(sz, dr + r, dc + c, n, m, k);
    }
  return;
}
int main() {
  char sz[101][101];
  int a, b;
  while (scanf("%d%d", &a, &b) == 2 && a * b) {
    count = 0;
    getchar();
    for (int i = 0; i < a; i++)
      scanf("%s", sz[i]);

    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (sz[i][j] == '@' || sz[i][j] == '#' || sz[i][j] == '*') {
          bfs(sz, i, j, a, b, sz[i][j]);
          count++;
        }
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int count;
void bfs(char (*sz)[21], int r, int c, int n, int m, char k) {
  if (r < 0 || r >= n || c < 0 || c >= m)
    return;
  if (sz[r][c] == '.' || sz[r][c] != k)
    return;
  sz[r][c] = '.';
  for (int dr = -1; dr <= 1; dr++)
    for (int dc = -1; dc <= 1; dc++) {
      if ((dr != 0 || dc != 0) && dr * dc == 0)
        bfs(sz, dr + r, dc + c, n, m, k);
    }
  return;
}
int main() {
  char sz[21][21];
  int a, b;
  while (scanf("%d%d", &a, &b) == 2 && a * b) {
    count = 0;
    getchar();
    for (int i = 0; i < a; i++)
      scanf("%s", sz[i]);

    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (sz[i][j] == '@' || sz[i][j] == '#' || sz[i][j] == '*') {
          bfs(sz, i, j, a, b, sz[i][j]);
          count++;
        }
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

int Row, Height;
int total_type;
bool flag_pos[101][101];
char dataSet[101][101];

void count_type(int start_x, int start_y, char bz_type) {
  if (start_x > 0 && start_x <= Row && start_y > 0 && start_y <= Height &&
      dataSet[start_x][start_y] == bz_type) {
    if (flag_pos[start_x][start_y] == false) {
      flag_pos[start_x][start_y] = true;
      count_type(start_x - 1, start_y, bz_type);
      count_type(start_x + 1, start_y, bz_type);
      count_type(start_x, start_y - 1, bz_type);
      count_type(start_x, start_y + 1, bz_type);
    }
  }
}

int main() {
  while (cin >> Row >> Height && Row != 0 && Height != 0) {
    total_type = 0;
    memset(flag_pos, true, sizeof(flag_pos[0][0]));
    for (int i = 1; i <= Row; i++) {
      for (int j = 1; j <= Height; j++) {
        cin >> dataSet[i][j];
        flag_pos[i][j] = false;
      }
    }
    for (int i = 1; i <= Row; i++) {
      for (int j = 1; j <= Height; j++) {
        if (flag_pos[i][j] == false) {
          count_type(i, j, dataSet[i][j]);
          total_type++;
        }
      }
    }
    cout << total_type << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 50;
int N, M;
int mymap[MAX_N][MAX_N];
int ans;
int sum;
int fx[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void dfs(int x, int y, char zf) {
  mymap[x][y] = '.';

  for (int i = 0; i < 4; i++) {
    int nx = x + fx[i][0];
    int ny = y + fx[i][1];
    if (0 <= nx && nx < N && 0 <= ny && ny < M && mymap[nx][ny] == zf) {
      dfs(nx, ny, zf);
    }
  }

  return;
}
void solve(char zf) {
  ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (mymap[i][j] == zf) {
        ans++;
        dfs(i, j, zf);
      }
    }
  }
  sum += ans;
}
int main() {
  while (scanf("%d %d", &N, &M) != EOF, N && M) {
    sum = 0;
    for (int i = 0; i < N; i++) {
      getchar();
      for (int j = 0; j < M; j++)
        scanf("%c", &mymap[i][j]);
    }
    solve('#');
    solve('@');
    solve('*');
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <stdio.h>
#include <string.h>
char map[101][101];
int ii, jj;
int h, w;
char temp[2];
void func(int, int);
using namespace std;
int main(void) {
  while (1) {
    scanf("%d %d", &h, &w);
    if (h == 0 && w == 0) {
      break;
    }
    int i, j, cnt = 0;
    for (i = 0; i < h; i++) {
      scanf("%s", map[i]);
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (map[i][j] == '#' || map[i][j] == '*' || map[i][j] == '@') {
          temp[0] = map[i][j];
          map[i][j] = 'o';
          func(i, j);
          cnt++;
        }
      } /*
       for(j=0;j<h;j++){
               printf("%s\\n",map[j]);
       }
       printf("\\n");*/
    }
    printf("%d\\n", cnt);
  }
}
void func(int y, int x) {
  int dy[4] = {0, 1, -1, 0}, dx[4] = {1, 0, 0, -1};
  int yy, xx;
  int i;
  for (i = 0; i < 4; i++) {
    yy = y + dy[i];
    xx = x + dx[i];
    if (map[yy][xx] == temp[0] && yy >= 0 && yy < h && xx < w && xx >= 0) {
      map[yy][xx] = 'o';
      func(yy, xx);
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <stdio.h>
#include <string.h>
char map[101][101];
int ii, jj;
char temp[2];
void func(int, int);
using namespace std;
int main(void) {
  while (1) {
    int h, w;
    scanf("%d %d", &h, &w);
    if (h == 0 && w == 0) {
      break;
    }
    int i, j, cnt = 0;
    for (i = 0; i < h; i++) {
      scanf("%s", map[i]);
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (map[i][j] == '#' || map[i][j] == '*' || map[i][j] == '@') {
          temp[0] = map[i][j];
          map[i][j] = 'o';
          func(i, j);
          cnt++;
        }
      }
    } /*
     for(i=0;i<h;i++){
             printf("%s\\n",map[i]);
     }*/
    printf("%d\\n", cnt);
  }
}
void func(int y, int x) {
  int dy[4] = {0, 1, -1, 0}, dx[4] = {1, 0, 0, -1};
  int yy, xx;
  int i;
  for (i = 0; i < 4; i++) {
    yy = y + dy[i];
    xx = x + dx[i];
    if (map[yy][xx] == temp[0]) {
      map[yy][xx] = 'o';
      func(yy, xx);
    }
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

char map[101][101];
pair<int, int> s;
int w, h;
int cnt;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
class Node {
public:
  int x, y;
  Node(int _x, int _y) : x(_x), y(_y) {}
};
int sx, sy;
void show() {
  for (int i = 0; i < h; i++) {
    cout << map[i] << endl;
  }
  cout << endl;
}
void bfs(int x, int y, char c) {
  //  cout<<x<<","<<y<<endl;
  queue<Node> q;
  q.push(Node(x, y));
  map[y][x] = '.';
  while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    //    show();
    for (int i = 0; i < 4; i++) {
      int nx = cur.x + dx[i];
      int ny = cur.y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h || map[ny][nx] == '.' ||
          map[ny][nx] != c)
        continue;
      map[ny][nx] = '.';
      q.push(Node(nx, ny));
    }
  }
}
void solve() {
  cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] != '.') {
        cnt++;
        bfs(j, i, map[i][j]);
      }
    }
  }
  cout << cnt << endl;
}
int main() {
  while (cin >> h >> w && (w || h)) {
    //    for(int i=0;i<h;i++)for(int j=0;j<w;j++)map[i][j]='.';
    for (int i = 0; i < h; i++) {
      cin >> map[i];
    }
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

char map[30][30];
pair<int, int> s;
int w, h;
int cnt;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
class Node {
public:
  int x, y;
  Node(int _x, int _y) : x(_x), y(_y) {}
};
int sx, sy;
void show() {
  for (int i = 0; i < h; i++) {
    cout << map[i] << endl;
  }
  cout << endl;
}
void bfs(int x, int y, char c) {
  //  cout<<x<<","<<y<<endl;
  queue<Node> q;
  q.push(Node(x, y));
  map[y][x] = '.';
  while (!q.empty()) {
    Node cur = q.front();
    q.pop();
    //    show();
    for (int i = 0; i < 4; i++) {
      int nx = cur.x + dx[i];
      int ny = cur.y + dy[i];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h || map[ny][nx] == '.' ||
          map[ny][nx] != c)
        continue;
      map[ny][nx] = '.';
      q.push(Node(nx, ny));
    }
  }
}
void solve() {
  cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] != '.') {
        cnt++;
        bfs(j, i, map[i][j]);
      }
    }
  }
  cout << cnt << endl;
}
int main() {
  while (cin >> h >> w && (w || h)) {
    //    for(int i=0;i<h;i++)for(int j=0;j<w;j++)map[i][j]='.';
    for (int i = 0; i < h; i++) {
      cin >> map[i];
    }
    solve();
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
typedef long long ll;
#define INF 2147483647

int w, h;
char a[102][102];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
int ans = 0;

void dfs(int x, int y, char s) {
  if (x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s)
    return;
  char t = a[x][y];
  a[x][y] = 'o';
  for (int i = 0; i < 4; i++) {
    dfs(x + dir[i][0], y + dir[i][1], t);
  }
}

int main() {
  while (cin >> h >> w) {
    if (w == 0 && h == 0)
      break;
    ans = 0;
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != 'o') {
          ans++;
          dfs(i, j, a[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;
#define INF 2147483647

int w, h;
char a[22][22];
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
int ans = 0;

void dfs(int x, int y, char s) {
  if (x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s)
    return;
  char t = a[x][y];
  a[x][y] = 'o';
  for (int i = 0; i < 4; i++) {
    dfs(x + dir[i][0], y + dir[i][1], t);
  }
}

int main() {
  while (cin >> h >> w) {
    if (w == 0 && h == 0)
      break;
    ans = 0;
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != 'o') {
          ans++;
          dfs(i, j, a[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

using namespace std;

const int MAX_H = 101;
const int MAX_W = 101;
int H, W;
char blocks[MAX_H][MAX_W];

int init() {
  scanf("%d%d", &H, &W);

  if (H == 0 && W == 0)
    return -1;

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

  return 0;
}

void fill(int row, int col) {
  char type = blocks[row][col];
  blocks[row][col] = ' ';

  int di[] = {1, -1, 0, 0};
  int dj[] = {0, 0, 1, -1};

  for (int k = 0; k < 4; k++) {
    int ti = row + di[k];
    int tj = col + dj[k];

    if (0 <= ti && ti < H && 0 <= tj && tj < W) {
      if (blocks[ti][tj] == type) {
        fill(ti, tj);
      }
    }
  }
}

void solve() {
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (blocks[i][j] != ' ') {
        fill(i, j);
        ans++;
      }
    }
  }
  printf("%d\\n", ans);
}

int main() {
  while (!init()) {
    solve();
  }
}

Error: -11

Fixed submission:

#include <cstdio>

using namespace std;

const int MAX_H = 101;
const int MAX_W = 101;
int H, W;
char blocks[MAX_H][MAX_W];

int init() {
  scanf("%d%d", &H, &W);

  if (H == 0 && W == 0)
    return -1;

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

  return 0;
}

void fill(int row, int col) {
  char type = blocks[row][col];
  blocks[row][col] = ' ';

  int di[] = {1, -1, 0, 0};
  int dj[] = {0, 0, 1, -1};

  for (int k = 0; k < 4; k++) {
    int ti = row + di[k];
    int tj = col + dj[k];

    if (0 <= ti && ti < H && 0 <= tj && tj < W) {
      if (blocks[ti][tj] == type) {
        fill(row, col);
      }
    }
  }
}

void solve() {
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (blocks[i][j] != ' ') {
        fill(i, j);
        ans++;
      }
    }
  }
  printf("%d\\n", ans);
}

int main() {
  while (!init()) {
    solve();
  }
}