alexjercan/bugnet huggingface dataset:
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#pragma warning(disable : 4996)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long Int;

using namespace std;

struct point {
  int x;
  int y;
  point(int _y, int _x) : y(_y), x(_x) {}
};

char tile[101][101];
stack<point> st;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // fstream fs("input.txt");
  ////////////ここから開始///////////

  int x(-1), y(-1);
  while (cin >> y >> x, x && y) {
    REP(i, 101) REP(j, 101) tile[i][j] = 'C';
    REP(i, y) {
      REP(j, x) {
        cin >> tile[i][j];
        //	fs >> tile[i][j];
      }
    }

    int ans(0);
    int dx[]{0, 1, 0, -1};
    int dy[]{1, 0, -1, 0};

    REP(i, y) {
      REP(j, x) {
        if (tile[i][j] != 'C') {
          ++ans;
          char now_c(tile[i][j]);
          tile[i][j] = 'C';
          st.emplace(i, j);
          while (!st.empty()) {
            point now = st.top();
            st.pop();
            REP(i, 4) {
              int x1 = now.x + dx[i];
              int y1 = now.y + dy[i];
              if (x1 >= x || x1 < 0 || y1 >= y || y1 < 0)
                continue;

              if (tile[y1][x1] == now_c) {
                st.emplace(y1, x1);
                tile[y1][x1] = 'C';
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <functional>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#pragma warning(disable : 4996)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long Int;

using namespace std;

struct point {
  int x;
  int y;
  point(int _y, int _x) : y(_y), x(_x) {}
};

char tile[101][101];
stack<point> st;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  fstream fs("input.txt");
  ////////////ここから開始///////////

  int x(-1), y(-1);
  while (fs >> y >> x, x && y) {
    REP(i, 101) REP(j, 101) tile[i][j] = 'C';
    REP(i, y) {
      REP(j, x) {
        // cin >> tile[i][j];
        fs >> tile[i][j];
      }
    }

    int ans(0);
    int dx[]{0, 1, 0, -1};
    int dy[]{1, 0, -1, 0};

    REP(i, y) {
      REP(j, x) {
        if (tile[i][j] != 'C') {
          ++ans;
          char now_c(tile[i][j]);
          tile[i][j] = 'C';
          st.emplace(i, j);
          while (!st.empty()) {
            point now = st.top();
            st.pop();
            REP(i, 4) {
              int x1 = now.x + dx[i];
              int y1 = now.y + dy[i];
              if (x1 > x || x1 < 0 || y1 > y || y1 < 0)
                continue;

              if (tile[y1][x1] == now_c) {
                st.emplace(y1, x1);
                tile[y1][x1] = 'C';
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const int N = 111;
int w, h, ans;
char ch, maze[N][N];
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
void dfs(int x, int y) {
  maze[x][y] = '-';
  for (int i = 0; i < 4; ++i) {
    int nx = x + cx[i], ny = y + cy[i];
    if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == ch)
      dfs(nx, ny);
  }
}
int main() {
  while (cin >> h >> w, (h && w)) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        cin >> maze[i][j];
    ans = 0;
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j) {
        if (maze[i][j] != '-') {
          ch = maze[i][j];
          ++ans;
          dfs(i, j);
        }
      }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
const int N = 22;
int w, h, ans;
char ch, maze[N][N];
const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1};
void dfs(int x, int y) {
  maze[x][y] = '-';
  for (int i = 0; i < 4; ++i) {
    int nx = x + cx[i], ny = y + cy[i];
    if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == ch)
      dfs(nx, ny);
  }
}
int main() {
  while (cin >> h >> w, (h && w)) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        cin >> maze[i][j];
    ans = 0;
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j) {
        if (maze[i][j] != '-') {
          ch = maze[i][j];
          ++ans;
          dfs(i, j);
        }
      }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

char Array[2000][2000];
int count;

void DFS(int h, int w, char c) {

  if (Array[h][w] != c || Array[h][w] == '.') {
    return;
  }

  char tmp = Array[h][w];

  Array[h][w] = '.';

  DFS(h - 1, w, tmp);
  DFS(h, w + 1, tmp);
  DFS(h + 1, w, tmp);
  DFS(h, w - 1, tmp);
}

int main() {
  int H, W;

  while (1) {
    cin >> H >> W;

    if (H == 0 && W == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> Array[i][j];
      }
    }

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        if (Array[i][j] != '.') {
          count++;
          DFS(i, j, Array[i][j]);
        }
      }
    }
    cout << count << endl;

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        Array[i][j] = '.';
      }
    }
    count = 0;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

char Array[200][200];
int count;

void DFS(int h, int w, char c) {

  if (Array[h][w] != c && Array[h][w] == '.') {
    return;
  }

  char tmp = Array[h][w];

  Array[h][w] = '.';

  DFS(h - 1, w, tmp);
  DFS(h, w + 1, tmp);
  DFS(h + 1, w, tmp);
  DFS(h, w - 1, tmp);
}

int main() {
  int H, W;

  while (1) {
    cin >> H >> W;
    if (H == 0 && W == 0) {
      break;
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> Array[i][j];
      }
    }

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        if (Array[i][j] != '.') {
          count++;
          DFS(i, j, Array[i][j]);
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 100000000
#define _INF -100000000
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;

char field[100][101];
int h, w;
queue<P> que;
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
int ret;

void solve() {
  Loop(i, h) {
    Loop(j, w) {
      if (field[i][j] != '.') {
        que.push(P(i, j));
        char c = field[i][j];
        field[i][j] = '.';
        while (que.size() > 0) {
          P p = que.front();
          que.pop();
          Loop(k, 4) {
            int a = p.first + dx[k], b = p.second + dy[k];
            if (a >= 0 && a <= h - 1 && b >= 0 && b <= w - 1 &&
                field[a][b] == c) {
              field[a][b] = '.';
              que.push(P(a, b));
            }
          }
        }
        ret++;
      }
    }
  }
}

int main() {
  while (cin >> h >> w, h || w) {
    Loop(i, h) {
      Loop(j, w) { cin >> field[i][j]; }
    }
    ret = 0;
    solve();
    cout << ret << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 100000000
#define _INF -100000000
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;

char field[100][101];
int h, w;
queue<P> que;
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
int ret;

void solve() {
  Loop(i, h) {
    Loop(j, w) {
      if (field[i][j] != '.') {
        que.push(P(i, j));
        char c = field[i][j];
        field[i][j] = '.';
        while (que.size() > 0) {
          P p = que.front();
          que.pop();
          Loop(k, 4) {
            int a = p.first + dx[k], b = p.second + dy[k];
            if (field[a][b] == c) {
              field[a][b] = '.';
              que.push(P(a, b));
            }
          }
        }
        ret++;
      }
    }
  }
}

int main() {
  while (cin >> h >> w, h || w) {
    Loop(i, h) {
      Loop(j, w) { cin >> field[i][j]; }
    }
    ret = 0;
    solve();
    cout << ret << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

char F[102][102];

void DFS1(int Y, int X) {
  if (F[Y][X] != '@')
    return;

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

  DFS1(Y - 1, X);
  DFS1(Y, X - 1);
  DFS1(Y, X + 1);
  DFS1(Y + 1, X);
}

void DFS2(int Y, int X) {
  if (F[Y][X] != '#')
    return;

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

  DFS2(Y - 1, X);
  DFS2(Y, X - 1);
  DFS2(Y, X + 1);
  DFS2(Y + 1, X);
}

void DFS3(int Y, int X) {
  if (F[Y][X] != '*')
    return;

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

  DFS3(Y - 1, X);
  DFS3(Y, X - 1);
  DFS3(Y, X + 1);
  DFS3(Y + 1, X);
}

int main() {
  int N, M;
  cin >> N >> M;
  while (N != 0 & M != 0) {
    for (int y = 1; y <= N; y++) {
      for (int x = 1; x <= M; x++) {
        cin >> F[y][x];
      }
    }

    int count = 0;
    for (int y = 1; y <= N; y++) {
      for (int x = 1; x <= M; x++) {
        if (F[y][x] == '@') {
          count++;
          DFS1(y, x);
        }
      }
    }
    for (int y = 1; y <= N; y++) {
      for (int x = 1; x <= M; x++) {
        if (F[y][x] == '#') {
          count++;
          DFS2(y, x);
        }
      }
    }
    for (int y = 1; y <= N; y++) {
      for (int x = 1; x <= M; x++) {
        if (F[y][x] == '*') {
          count++;
          DFS3(y, x);
        }
      }
    }
    cout << count << endl;
    cin >> N >> M;
  }
  return 0;
}

Error: -11

Fixed submission:


#include <iostream>
using namespace std;

char F[102][102];

void DFS1(int Y, int X) {
  if (F[Y][X] == '@')

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

  DFS1(Y - 1, X - 1);
  DFS1(Y - 1, X);
  DFS1(Y - 1, X + 1);
  DFS1(Y, X - 1);
  DFS1(Y, X + 1);
  DFS1(Y + 1, X - 1);
  DFS1(Y + 1, X);
  DFS1(Y + 1, X + 1);
}

void DFS2(int Y, int X) {
  if (F[Y][X] == '#')

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

  DFS2(Y - 1, X - 1);
  DFS2(Y - 1, X);
  DFS2(Y - 1, X + 1);
  DFS2(Y, X - 1);
  DFS2(Y, X + 1);
  DFS2(Y + 1, X - 1);
  DFS2(Y + 1, X);
  DFS2(Y + 1, X + 1);
}

void DFS3(int Y, int X) {
  if (F[Y][X] == '*')

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

  DFS3(Y - 1, X - 1);
  DFS3(Y - 1, X);
  DFS3(Y - 1, X + 1);
  DFS3(Y, X - 1);
  DFS3(Y, X + 1);
  DFS3(Y + 1, X - 1);
  DFS3(Y + 1, X);
  DFS3(Y + 1, X + 1);
}

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 count = 0;
  for (int y = 1; y <= N; y++) {
    for (int x = 1; x <= M; x++) {
      if (F[y][x] == '@') {
        count++;
        DFS1(y, x);
      }
    }
  }
  for (int y = 1; y <= N; y++) {
    for (int x = 1; x <= M; x++) {
      if (F[y][x] == '#') {
        count++;
        DFS2(y, x);
      }
    }
  }
  for (int y = 1; y <= N; y++) {
    for (int x = 1; x <= M; x++) {
      if (F[y][x] == '*') {
        count++;
        DFS3(y, x);
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
using namespace std;
char S[105][105];
int xx[] = {0, -1, 0, 1};
int yy[] = {-1, 0, 1, 0};
int N, M;
clock_t start, finish;
void dfs(int x, int y,
         char temp_c) { // 同种水果再dfs，因此加上一个参数代表水果种类
  int i, j, temp_x, temp_y;
  S[x][y] = '.';
  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++) {
      temp_x = x + xx[i];
      temp_y = y + yy[i];
      if (S[temp_x][temp_y] != temp_c || temp_x < 0 || temp_x >= N ||
          temp_y < 0 || temp_y >= M)
        continue;
      dfs(temp_x, temp_y, temp_c);
    }
}
int main() {
  start = clock();
  int i, j, sum;
  while (cin >> N >> M && (N || M)) {
    sum = 0;
    for (i = 0; i < N; i++)
      for (j = 0; j < M; j++)
        cin >> S[i][j];
    for (i = 0; i < N; i++)
      for (j = 0; j < M; j++)
        if (S[i][j] != '.') {
          dfs(i, j, S[i][j]);
          sum++;
        }
    printf("%d\\n", sum);
  }
  finish = clock();
  // cout << finish-start   << "/" << CLOCKS_PER_SEC  << " (s) "<< endl;
  return 0;
}


Error: TLE

Fixed submission:

/*AOJ 0118 区域个数 DFS*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <time.h>
using namespace std;

const int maxn = 25;
int n, m;
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
char map[maxn][maxn];
int visit[maxn][maxn];
int total;
clock_t start, finish;

bool judge(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m)
    return true;
  return false;
}

void dfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int nextx = x + dx[i];
    int nexty = y + dy[i];
    if (!visit[nextx][nexty] && judge(nextx, nexty) &&
        map[nextx][nexty] == map[x][y]) {
      visit[nextx][nexty] = 1;
      dfs(nextx, nexty);
    }
  }
}

int main(void) {
  start = clock();
  while (~scanf("%d %d", &n, &m)) {
    if (n + m == 0)
      break;
    total = 1;
    for (int i = 1; i <= n; i++) {
      getchar();
      for (int j = 1; j <= m; j++) {
        visit[i][j] = 0;
        scanf("%c", &map[i][j]);
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (!visit[i][j]) {
          visit[i][j] = 1;
          total++;
          dfs(i, j);
        }
      }
    }
    /*for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                    printf("%d ",visit[i][j]);
            }
            printf("\\n");
    }*/
    cout << total - 1 << endl;
  }
  finish = clock();
  // cout << finish-start   << "/" << CLOCKS_PER_SEC  << " (s) "<< endl;
  return 0;
}
/*
10 10
####*****@
@#@@@@#*#*
@##***@@@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@#@
0 0
*/
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <queue>
using namespace std;

int main() {
  const int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
  while (true) {
    int h, w;
    char fld[128][128];
    scanf("%d%d", &h, &w);
    if (h == 0)
      break;
    for (int i = 0; i < h; ++i)
      scanf("%s", fld[i]);
    int ans = 0;
    for (int sx = 0; sx < h; ++sx) {
      for (int sy = 0; sy < w; ++sy) {
        if (fld[sx][sy] == 'x')
          continue;
        char c = fld[sx][sy];
        queue<pair<int, int>> q;
        q.push(make_pair(sx, sy));
        while (!q.empty()) {
          int x = q.front().first, y = q.front().second;
          q.pop();
          for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx >= h || ny >= w)
              continue;
            if (fld[nx][ny] != c)
              continue;
            fld[nx][ny] = 'x';
            q.push(make_pair(nx, ny));
          }
        }
        ans += 1;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <queue>
using namespace std;

int main() {
  const int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
  while (true) {
    int h, w;
    char fld[32][32];
    scanf("%d%d", &h, &w);
    if (h == 0)
      break;
    for (int i = 0; i < h; ++i)
      scanf("%s", fld[i]);
    int ans = 0;
    for (int sx = 0; sx < h; ++sx) {
      for (int sy = 0; sy < w; ++sy) {
        if (fld[sx][sy] == 'x')
          continue;
        char c = fld[sx][sy];
        queue<pair<int, int>> q;
        q.push(make_pair(sx, sy));
        while (!q.empty()) {
          int x = q.front().first, y = q.front().second;
          q.pop();
          for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx >= h || ny >= w)
              continue;
            if (fld[nx][ny] != c)
              continue;
            fld[nx][ny] = 'x';
            q.push(make_pair(nx, ny));
          }
        }
        ans += 1;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define MAX_M 2000
#define MAX_N 2000
using namespace std;

string field[200];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int INF = 0x3f3f3f3f;
int N, M, cnt;

void dfs(int x, int y, char c) {
  field[x][y] = '.';
  for (int i = 0; i <= 3; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == c) {
      dfs(nx, ny, field[nx][ny]);
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  while (cin >> N >> M && N && M) {
    cnt = 0;
    for (int i = 0; i < N; i++)
      cin >> field[i];

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (field[i][j] != '.') {
          dfs(i, j, field[i][j]);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

//
//  0118 Property Distribution
//
//  Created by TaoSama on 2015-02-19
//  Copyright (c) 2014 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define CLR(x, y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, m;
char a[15][15];
int d[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void dfs(int x, int y, char cur) {
  a[x][y] = 'X';
  for (int i = 0; i < 4; ++i) {
    int nx = x + d[i][0], ny = y + d[i][1];
    if (a[nx][ny] != 'X' && a[nx][ny] == cur && nx >= 1 && nx <= n && ny >= 1 &&
        ny <= m)
      dfs(nx, ny, a[nx][ny]);
  }
}
int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
  ios_base::sync_with_stdio(0);

  while (cin >> n >> m && n != 0 && m != 0) {
    for (int i = 1; i <= n; ++i)
      cin >> a[i] + 1;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        if (a[i][j] != 'X') {
          ++ans;
          dfs(i, j, a[i][j]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
const int MAX_N = 105;
const int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
char map[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int W, H, num;

void dfs(int x, int y) {
  visited[x][y] = true;
  for (int i = 0; i < 4; i++) {
    int to_x = x + dir[i][0], to_y = y + dir[i][1];
    if (0 <= to_x && to_x < H && 0 <= to_y && to_y < W &&
        !visited[to_x][to_y] && map[x][y] == map[to_x][to_y])
      dfs(to_x, to_y);
  }
}

int main() {
  while (scanf("%d%d", &H, &W) == 2 && H) {
    for (int i = 0; i < H; i++)
      scanf("%s", map[i]);
    num = 0;
    memset(visited, false, sizeof(visited));
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        if (!visited[i][j]) {
          num++;
          dfs(i, j);
        }
    printf("%d\\n", num);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdlib.h>
using namespace std;
int n, m;
char map[101][101];
int dfs(int x, int y) {
  map[x][y] = '.';
  return 1 + dfs(x + 1, y) + dfs(x - 1, y) + dfs(x, y + 1) + dfs(x, y - 1);
}
int main(int argc, char *argv[]) {
  while (cin >> m >> n) {
    int ans = 0;
    if (n == 0 || m == 0)
      break;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> map[i][j];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (map[i][j] != '.')
          ans = dfs(i, j);
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

//
// Created by yijiezhu on 12/20/16.
//

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

#define MAX_N 101

int m, n;
char matrix[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int cnt;

void dfs(int i, int j, char ch) {
  visited[i][j] = true;
  matrix[i][j] = (char)('a' + cnt - 1);
  int delta[] = {0, 1, 0, -1, 0};
  for (int q = 0; q < 4; q++) {
    int ni = i + delta[q], nj = j + delta[q + 1];
    if (ni < 0 || ni >= m || nj < 0 || nj >= n)
      continue;
    if (visited[ni][nj] || matrix[ni][nj] != ch)
      continue;
    dfs(ni, nj, ch);
  }
}

void solve() {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (!visited[i][j]) {
        cnt++;
        dfs(i, j, matrix[i][j]);
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (true) {
    cin >> m >> n;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cin >> matrix[i][j];
      }
    }
    cnt = 0;
    memset(visited, false, sizeof(visited));
    solve();
  }
}

Error: 0

Fixed submission:

//
// Created by yijiezhu on 12/20/16.
//

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

const int MAX_N = 21;

int m, n;
char matrix[MAX_N][MAX_N];
bool visited[MAX_N][MAX_N];
int cnt;

void dfs(int i, int j, char ch) {
  visited[i][j] = true;
  matrix[i][j] = (char)('a' + cnt - 1);
  int delta[] = {0, 1, 0, -1, 0};
  for (int q = 0; q < 4; q++) {
    int ni = i + delta[q], nj = j + delta[q + 1];
    if (ni < 0 || ni >= m || nj < 0 || nj >= n)
      continue;
    if (visited[ni][nj] || matrix[ni][nj] != ch)
      continue;
    dfs(ni, nj, ch);
  }
}

void solve() {
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (!visited[i][j]) {
        cnt++;
        dfs(i, j, matrix[i][j]);
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (true) {
    cin >> m >> n;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        cin >> matrix[i][j];
      }
    }
    cnt = 0;
    memset(visited, false, sizeof(visited));
    solve();
  }
}