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

#include <cstdio>
#include <iostream>
#include <stack>

using namespace std;

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

void dfs(int y, int x, char type) {

  char cur_type = field[y][x];

  field[y][x] = '.';

  for (int dy = -1; dy <= 1; dy++) {
    for (int dx = -1; dx <= 1; dx++) {
      int nx = x + dx;
      int ny = y + dy;

      if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] == cur_type &&
          dx * dy == 0) {
        dfs(ny, nx, cur_type);
      }
    }
  }
  return;
}

void solve() {
  int res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] != '.') {

        dfs(i, j, field[i][j]);

        res++;
      }
    }
  }
  cout << res << endl;
}

int main() {

  while (1) {

    cin >> H >> W;
    cin.ignore();

    if (H == 0 && W == 0) {
      break;
    }
    string line;

    for (int i = 0; i < H; i++) {
      getline(cin, line);
      for (int j = 0; j < W; j++) {
        field[i][j] = line[j];
      }
    }
    solve();
  }

  return 0;
}

/*
10 10
####*****@
@#@@@@@*#*
@##***@#@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@*#
2 1
#
@
0 0
 */

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
#include <stack>

using namespace std;

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

void dfs(int y, int x, char type) {

  char cur_type = field[x][y];

  field[x][y] = '.';

  for (int dy = -1; dy <= 1; dy++) {
    for (int dx = -1; dx <= 1; dx++) {
      int nx = x + dx;
      int ny = y + dy;

      if (0 <= nx && nx < W && 0 <= ny && ny < H && field[nx][ny] == cur_type &&
          dx * dy == 0) {
        dfs(nx, ny, cur_type);
      }
    }
  }
  return;
}

void solve() {
  int res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] != '.') {

        dfs(i, j, field[i][j]);

        res++;
      }
    }
  }
  cout << res << endl;
}

int main() {

  while (1) {

    cin >> H >> W;
    cin.ignore();

    if (H == 0 && W == 0) {
      break;
    }
    string line;

    for (int i = 0; i < H; i++) {
      getline(cin, line);
      for (int j = 0; j < W; j++) {
        field[i][j] = line[j];
      }
    }
    solve();
  }

  return 0;
}

/*
10 10
####*****@
@#@@@@@*#*
@##***@#@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@*#
2 1
#
@
0 0
 */
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

// aoj 0118 Property Distribution
// 多次dfs求?通?个数，有3?果子
// 注意H和W，不是row和col
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

char tile[123][123];

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

bool out_of_range(int x, int y) { return x < 0 || x >= H || y < 0 || y >= W; }

void dfs(int x, int y, char now) {
  if (out_of_range(x, y) || tile[x][y] != now)
    return; // 超界或者不是本区域
  int nx, ny;
  tile[x][y] = '%'; //????不要忘?
  for (int i = 0; i < 4; ++i) {
    nx = x + dx[i], ny = y + dy[i];
    dfs(nx, ny, now);
  }
}

void debug() {
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      printf("%c", tile[i][j]);
    }
    putchar(10);
  }
  putchar(10);
}

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

int main() {
  while (scanf("%d %d", &H, &W) == 2 && H && W) {
    for (int i = 0; i < H; ++i)
      scanf("%s", tile[i]);
    solve();
  }
  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 <iostream>
#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];
size_t W, H;

void getNum(char (*a)[W_size], const char c, size_t i, size_t j) {
  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);
    getNum(a, c, i, j + 1);
    getNum(a, c, i + 1, j);
    getNum(a, c, i, j - 1);
    return;
  }
}

int main() {

  while (true) {
    num = 0;
    cin >> H >> W;
    if (W == 0 && H == 0) {
      break;
    } else {
      for (size_t i = 0; i < H; ++i) {
        // cin.getline(list[i], W + 1);
        for (size_t j = 0; j < W; ++j) {
          cin >> list[i][j];
        }
      }
    }
    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);
          num++;
        }
      }
    }
    cout << num << endl;
  }
  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: Runtime Error

#include <stdio.h>

#define N 100
char a[N][N];
int rows, cols;

int dfs(int curX, int curY, char curChar) {
  if (curX < 0 || curX >= rows)
    return 0;
  if (curY < 0 || curY >= cols)
    return 0;
  if (a[curX][curY] != curChar)
    return 0;

  a[curX][curY] = '0';
  dfs(curX - 1, curY, curChar);
  dfs(curX + 1, curY, curChar);
  dfs(curX, curY - 1, curChar);
  dfs(curX, curY + 1, curChar);
  return 0;
}

int main() {
  while (scanf("%d%d", &rows, &cols), rows || cols) {
    int count = 0;
    int i, j;
    getchar();
    for (i = 0; i < rows; i++) {
      for (j = 0; j < cols; j++)
        scanf("%c", &a[i][j]);
      getchar();
    }
    for (i = 0; i < rows; i++)
      for (j = 0; j < cols; j++)
        if (a[i][j] != '0') {
          dfs(i, j, a[i][j]);
          count++;
        }

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

Error: 0

Fixed submission:

#include <stdio.h>
#define N 20
int W, H;
int count = 0;
char a[N][N];

int DFS(int curX, int curY, int curChar) {
  if (curX < 0 || curX >= W)
    return 0;
  if (curY < 0 || curY >= H)
    return 0;
  if (a[curX][curY] == curChar) {
    a[curX][curY] = '0';
    DFS(curX, curY + 1, curChar);
    DFS(curX, curY - 1, curChar);
    DFS(curX - 1, curY, curChar);
    DFS(curX + 1, curY, curChar);
  } else
    return 0;
}

int main() {
  int i, j;
  while (scanf("%d%d", &H, &W),
         H || W) // 判断?入先列数，后行数！！！！ 若?0 0 ?停止
  {
    count = 0; //?次?入列数行数后，count要置0
    for (i = 0; i < W; i++) {
      getchar(); // 由于?行?入后面都有一个回?，那?要用getchar()把回?去掉，
      for (j = 0; j < H; j++) {
        scanf("%c", &a[i][j]);
      }
    }

    for (i = 0; i < W; i++) {

      for (j = 0; j < H; j++) {
        if (a[i][j] != '0') {
          DFS(i, j, a[i][j]);
          count++;
        }
      }
    }

    printf("%d\\n", count);
  }

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


#include <algorithm>
#include <iostream>
#include <limits.h>
#include <queue>
#include <string>
// typedef pair<int,int> P;

using namespace std;

const int INF = INT_MAX / 3;
const int V_MAX = 10000, E_MAX = 10000;

int H, W;
const int MAX = 200;

char maps[MAX][MAX];

void solve(int i, int j) {

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

  char c = maps[i][j];
  maps[i][j] = '.';

  for (int k = 0; k < 4; k++) {
    int ni = i + dx[k];
    int nj = j + dy[k];
    if ((0 <= ni && ni < H) && (0 <= nj && nj < W))
      if (maps[ni][nj] != '.' && c == maps[ni][nj]) {
        // cout<<"pohe"<<endl;
        solve(ni, nj);
      }
  }
}

int main() {

  int res = 0;

  while (1) {
    res = 0;
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;

    for (int i = 0; i < H; i++) {
      cin >> maps[i];
    }
    // cout<<"hoge\\n";

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (maps[i][j] != '.') {
          solve(i, j);
          res++;
          // cout<<"piyo"<<endl;
        }
        /*
        for(int i=0;i<H;i++){
          for(int j=0;j<W;j++){
            cout<<maps[i][j]<<" ";
          }
          cout<<endl;
        }
        cout<<endl;
        */
      }
    cout << res << endl;

    // cout<<solve()<<endl;
  }

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <limits.h>
#include <queue>
#include <string>
// typedef pair<int,int> P;

using namespace std;

const int INF = INT_MAX / 3;
const int V_MAX = 10000, E_MAX = 10000;

int H, W;
const int MAX = 200;

char maps[MAX][MAX];

void solve(int i, int j) {

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

  char c = maps[i][j];
  maps[i][j] = '.';

  for (int k = 0; k < 4; k++) {
    int ni = i + dx[k];
    int nj = j + dy[k];
    if ((0 <= ni && ni < H) && (0 <= nj && nj < W))
      if (maps[ni][nj] != '.' && c == maps[ni][nj]) {
        // cout<<"pohe"<<endl;
        solve(ni, nj);
      }
  }
}

int main() {

  int res = 0;

  while (1) {
    res = 0;
    cin >> H >> W;
    if (H == 0 && W == 0)
      break;

    for (int i = 0; i < H; i++) {
      cin >> maps[i];
    }
    // cout<<"hoge\\n";

    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (maps[i][j] != '.') {
          solve(i, j);
          res++;
          // cout<<"piyo"<<endl;
        }

        for (int i = 0; i < H; i++) {
          for (int j = 0; j < W; j++) {
            cout << maps[i][j] << " ";
          }
          cout << endl;
        }
        cout << endl;
      }
    cout << res << endl;

    // cout<<solve()<<endl;
  }

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

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

char field[101][101], a;
int H, W;

int dfs(int n, int m) {
  if (m + 1 <= W) {
    if (field[n][m + 1] == a) {
      field[n][m + 1] = 'v';
      dfs(n, m + 1);
    }
  }
  if (n + 1 <= H) {
    if (field[n + 1][m] == a) {
      field[n + 1][m] = 'v';
      dfs(n + 1, m);
    }
  }
  if (m - 1 >= 0) {
    if (field[n][m - 1] == a) {
      field[n][m - 1] = 'v';
      dfs(n, m - 1);
    }
  }
  if (n - 1 >= 0) {
    if (field[n - 1][m] == a) {
      field[n - 1][m] = 'v';
      dfs(n - 1, m);
    }
  }

  return 1;
}

int main(void) {
  int sum;
  while (cin >> H >> W) {
    sum = 0;
    if (H == 0 && W == 0)
      break;
    string str;
    for (int i = 0; i < H; i++) {
      cin >> str;
      for (int j = 0; j < W; j++) {
        field[i][j] = str[j];
      }
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (field[i][j] == '#') {
          a = '#';
          sum += dfs(i, j);
        } else if (field[i][j] == '@') {
          a = '@';
          sum += dfs(i, j);
        } else if (field[i][j] == '*') {
          a = '*';
          sum += dfs(i, j);
        } else
          continue;
      }
    }
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

char field[101][101], a;
int H, W;

int dfs(int n, int m) {
  if (m > W || m < 0 || n > H || n < 0)
    return 0;
  if (field[n][m + 1] == a) {
    field[n][m + 1] = 'v';
    dfs(n, m + 1);
  }
  if (field[n + 1][m] == a) {
    field[n + 1][m] = 'v';
    dfs(n + 1, m);
  }
  if (field[n][m - 1] == a) {
    field[n][m - 1] = 'v';
    dfs(n, m - 1);
  }
  if (field[n - 1][m] == a) {
    field[n - 1][m] = 'v';
    dfs(n - 1, m);
  }

  return 1;
}

int main(void) {
  int sum = 0;
  while (cin >> H >> W) {
    if (H == 0 && W == 0)
      break;
    string str;
    for (int i = 0; i < H; i++) {
      cin >> str;
      for (int j = 0; j < W; j++) {
        field[i][j] = str[j];
      }
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (field[i][j] == '#') {
          a = '#';
          sum += dfs(i, j);
        } else if (field[i][j] == '@') {
          a = '@';
          sum += dfs(i, j);
        } else if (field[i][j] == '*') {
          a = '*';
          sum += dfs(i, j);
        } else
          continue;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int h, w;
string s[1000];
char k;

void solve(int x, int y) {

  if (s[x][y] != k)
    return;
  s[x][y] = '.';

  if (x + 1 < h)
    solve(x + 1, y);
  if (y + 1 < w)
    solve(x, y + 1);
  if (x - 1 >= 0)
    solve(x - 1, y);
  if (y - 1 >= 0)
    solve(x, y - 1);
}

int main() {

  while (true) {
    cin >> h >> w;
    if (!(h + w))
      break;

    for (int i = 0; i < h; i++)
      cin >> s[i];

    int ans = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] != '.') {
          k = s[i][j];
          solve(i, j);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

string s[1000];
int n, m, memo[1000][1000], x, y;

int solve(int i, int j) {

  if (s[i][j] != s[x][y])
    return 0;
  if (memo[i][j] > 0)
    return memo[i][j];
  memo[i][j] = 1;

  if (i + 1 < n && s[i + 1][j] == s[x][y])
    solve(i + 1, j);
  if (j + 1 < m && s[i][j + 1] == s[x][y])
    solve(i, j + 1);
  if (i - 1 > -1 && s[i - 1][j] == s[x][y])
    solve(i - 1, j);
  if (j - 1 > -1 && s[i][j - 1] == s[x][y])
    solve(i, j - 1);

  return 0;
}

int main() {

  while (true) {
    cin >> n >> m;
    if (!n && !m)
      break;
    memset(memo, 0, sizeof(memo));
    int count = 0;

    for (int i = 0; i < m; i++)
      cin >> s[i];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (memo[i][j] != 1) {
          x = i;
          y = j;
          solve(i, j);
          count++;
        }
      }
    }

    cout << count << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int map[101][101];
int h, w;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

void REC(int y, int x, int type) {
  map[y][x] = 0;
  for (int i = 0; i < 4; i++) {
    if (y + dy[i] >= 0 && y + dy[i] < h && x + dx[i] >= 0 && x + dx[i] < w) {
      if (map[y + dy[i]][x + dx[i]] == type)
        REC(y + dy[i], x + dx[i], type);
    }
  }
}
int main() {
  while (true) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    char z;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> z;
        if (z == '#')
          map[i][j] = 1;
        if (z == '@')
          map[i][j] = 2;
        if (z == '*')
          map[i][j] = 3;
      }
    }
    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (map[i][j] != 0) {
          count++;
          REC(i, j, map[i][j]);
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int map[100][100];
int h, w;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

void REC(int y, int x, int type) {
  map[y][x] = 0;
  for (int i = 0; i < 4; i++) {
    if (map[y + dy[i]][x + dx[i]] == type)
      REC(y + dy[i], x + dx[i], type);
  }
}
int main() {
  while (true) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    char z;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> z;
        if (z == '#')
          map[i][j] = 1;
        if (z == '@')
          map[i][j] = 2;
        if (z == '*')
          map[i][j] = 3;
      }
    }
    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (map[i][j] != 0) {
          count++;
          REC(i, j, map[i][j]);
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int W, H;

string field[100];

bool visit[100][100];

void dfs(int y, int x) {
  visit[y][x] = true;
  static int dy[4] = {0, 1, 0, -1};
  static int dx[4] = {1, 0, -1, 0};
  char c = field[y][x];

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (0 <= ny && ny < H && 0 <= nx && nx < W && visit[ny][nx] == false &&
        c == field[ny][nx]) {
      dfs(ny, nx);
    }
  }
}

void pv() {
  cerr << endl;
  cerr << "-----" << endl;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cerr << visit[y][x];
    }
    cerr << endl;
  }

  cerr << "-----" << endl;
}

int main() {
  for (; cin >> H >> W, W || H;) {
    for (int y = 0; y < H; y++)
      cin >> field[y];

    memset(visit, 0, sizeof visit);

    int ans = 0;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if (visit[y][x] == false) {
          ans++;
          dfs(y, x);
        }
      }
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int W, H;

string field[100];

bool visit[100][100];

void dfs(int y, int x) {
  visit[y][x] = true;
  static int dy[4] = {0, 1, 0, -1};
  static int dx[4] = {1, 0, -1, 0};
  char c = field[y][x];

  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (0 <= ny && ny < H && 0 <= nx && nx < W && visit[ny][nx] == false &&
        c == field[ny][nx]) {
      dfs(ny, nx);
    }
  }
}

void pv() {
  cerr << endl;
  cerr << "-----" << endl;
  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      cerr << visit[y][x];
    }
    cerr << endl;
  }

  cerr << "-----" << endl;
}

int main() {
  for (; cin >> W >> H, W || H;) {
    for (int y = 0; y < H; y++)
      cin >> field[y];

    memset(visit, 0, sizeof visit);

    int ans = 0;
    for (int y = 0; y < H; y++) {
      for (int x = 0; x < W; x++) {
        if (visit[y][x] == false) {
          ans++;
          dfs(y, x);
        }
      }
    }

    cout << ans << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

void replace(string *field, char c, int sx, int sy, int w, int h) {
  static const int dx[] = {-1, 0, 0, 1};
  static const int dy[] = {0, -1, 1, 0};

  field[sy][sx] = '.';
  for (int i = 0; i < 4; i++) {
    if (((sx + dx[i] >= 0) && (sx + dx[i] < w)) &&
        ((sy + dy[i] >= 0) && (sy + dy[i] < h))) {
      if (field[sy + dy[i]][sx + dx[i]] == c) {
        replace(field, c, sx + dx[i], sy + dy[i], w, h);
      }
    }
  }
}

int main(int argc, char **argv) {
  int w, h;
  string field[100];

  cin >> h >> w;
  while ((w != 0) && (h != 0)) {
    int num = 0;

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

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != '.') {
          num++;
          replace(field, field[i][j], j, i, w, h);
        }
      }
    }
    cout << num << endl;
    cin >> h >> w;
  }

  return 0;
}

Error: 33

Fixed submission:

#include <iostream>

using namespace std;

void replace(string *field, char c, int sx, int sy, int w, int h) {
  static const int dx[] = {-1, 0, 0, 1};
  static const int dy[] = {0, -1, 1, 0};

  field[sy][sx] = '.';
  for (int i = 0; i < 4; i++) {
    if (((sx + dx[i] >= 0) && (sx + dx[i] < w)) &&
        ((sy + dy[i] >= 0) && (sy + dy[i] < h))) {
      if (field[sy + dy[i]][sx + dx[i]] == c) {
        replace(field, c, sx + dx[i], sy + dy[i], w, h);
      }
    }
  }
}

int main(int argc, char **argv) {
  int w, h;
  string field[100];
  int num = 0;

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

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != '.') {
          num++;
          replace(field, field[i][j], j, i, w, h);
        }
      }
    }
    cout << num << endl;
    cin >> w >> h;
  }

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


#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;

#define int_ int64_t
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define lb long double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const ll N = 105;
ll n, m;
char grid[N][N];
ll ans = 0;
bool vis[N][N];
ll dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void dfs(ll x, ll y) {
  vis[x][y] = 1;
  for (ll i = 0; i < 4; i++) {
    ll xx = x + dir[i][0];
    ll yy = y + dir[i][1];
    if (xx > 0 && xx <= n && yy > 0 && yy <= m) {
      if (!vis[xx][yy] && grid[xx][yy] == grid[x][y])
        dfs(xx, yy);
    }
  }
}

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        cin >> grid[i][j];
      }
    }
    memset(vis, 0, sizeof(vis));
    ans = 0;
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        if (!vis[i][j]) {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << std::endl;
  }
}


Error: 0

Fixed submission:


#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;

#define int_ int64_t
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define lb long double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const ll N = 25;
ll n, m;
char grid[N][N];
ll ans = 0;
bool vis[N][N];
ll dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void dfs(ll x, ll y) {
  vis[x][y] = 1;
  for (ll i = 0; i < 4; i++) {
    ll xx = x + dir[i][0];
    ll yy = y + dir[i][1];
    if (xx > 0 && xx <= n && yy > 0 && yy <= n) {
      if (!vis[xx][yy] && grid[xx][yy] == grid[x][y])
        dfs(xx, yy);
    }
  }
}

int main() {
  while (true) {
    scanf("%lld%lld", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (ll i = 1; i <= n; i++) {
      scanf("%s", &grid[i][1]);
    }
    memset(vis, 0, sizeof(vis));
    ans = 0;
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= m; j++) {
        if (!vis[i][j]) {
          ans++;
          dfs(i, j);
        }
      }
    }
    printf("%lld\\n", ans);
  }
}