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

#include <iostream>
using namespace std;
const int MAX_H = 100, MAX_W = 100;
char field[120][120];
int h, w, cnt;
const int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};
void dfs(int y, int x, char kind) {
  if (y < 0 || y >= h || x < 0 || x >= w || field[y][x] != kind)
    return;
  field[y][x] = '.';
  for (int i = 0; i < 4; i++)
    dfs(y + dy[i], x + dx[i], kind);
  return;
}
int main() {
  while (cin >> h >> w) {
    if (!w && !h)
      break;
    for (int i = 0; i < h; i++)
      cin >> field[i];
    cnt = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != '.') {
          cnt++;
          dfs(i, j, field[i][j]);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int h, w;
char field[100][101];
void dfs(int y, int x, char kind);
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

int main() {
  while (cin >> w >> h, w || h) {
    if (!w || !h) {
      cout << "0" << endl;
      continue;
    }
    for (int i = 0; i < h; i++) {
      cin >> field[i];
    }
    int cnt = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != '.') {
          cnt++;
          dfs(i, j, field[i][j]);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

void dfs(int y, int x, char kind) {
  if (y < 0 || y >= h)
    return;
  if (x < 0 || x >= w)
    return;
  if (field[y][x] == kind) {
    field[y][x] = '.';
    for (int i = 0; i < 4; i++) {
      dfs(y + dy[i], x + dx[i], kind);
    }
  }
  return;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <ostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000000
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rev(i, a, b) for (int i = (a)-1; i >= b; i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef queue<int> qi;
typedef queue<pair<int, int>> qpii;
typedef vector<int> vi;
typedef vector<int, int> vii;
typedef vector<string> vs;
typedef vector<pair<int, int>> vpii;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

char f[150][150];
int h, w;

void dfs(int y, int x) {
  char z = f[y][x];
  f[y][x] = 'C';
  rep(i, 0, 4) {
    int my = y + dy[i];
    int mx = x + dx[i];
    if (0 <= my && my < h && 0 <= mx && mx < w && f[my][mx] == z)
      dfs(my, mx);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    rep(i, 0, h) {
      rep(j, 0, w) { cin >> f[i][j]; }
    }

    int ans = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (f[i][j] != 'C') {
          dfs(i, j);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <ostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000000
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rev(i, a, b) for (int i = (a)-1; i >= b; i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef queue<int> qi;
typedef queue<pair<int, int>> qpii;
typedef vector<int> vi;
typedef vector<int, int> vii;
typedef vector<string> vs;
typedef vector<pair<int, int>> vpii;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

char m[101][101];
int h, w;

void dfs(int y, int x) {
  char z = m[y][x];
  m[y][z] = '.';
  rep(i, 0, 4) {
    int my = y + dy[i];
    int mx = x + dx[i];
    if (0 <= my && my < h && 0 <= mx && mx < w && m[my][mx] == z)
      dfs(my, mx);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    rep(i, 0, h) rep(j, 0, w) cin >> m[i][j];
    int ans = 0;
    rep(i, 0, h) rep(j, 0, w) if (m[i][j] != '.') {
      dfs(i, j);
      ans++;
    }

    cout << ans << endl;
  }

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

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

#define rep(i, n) for (int i = 0; i < n; i++)
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int cnt = 0;
int w, h;
char tab[101][101];

void dfs(int x, int y, char c) {
  tab[y][x] = '.';
  rep(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[ny][nx] == c) {
      dfs(nx, ny, c);
    }
  }
}

void solve() {
  rep(i, h) {
    rep(j, w) { cin >> tab[i][j]; }
  }
  cnt = 0;
  rep(i, h) {
    rep(j, w) {
      if (tab[i][j] != '.') {
        cnt++;
        dfs(j, i, tab[i][j]);
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    solve();
  }
}

Error: WA

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < n; i++)
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int cnt = 0;
int w, h;
char tab[101][101];

void dfs(int x, int y, char c) {
  tab[x][y] = '.';
  rep(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && tab[nx][ny] == c) {
      dfs(nx, ny, c);
    }
  }
}

int main() {
  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    rep(i, h) rep(j, w) cin >> tab[i][j];
    rep(i, h) {
      rep(j, w) {
        if (tab[i][j] != '.') {
          cnt++;
          dfs(tab[i][j], i, j);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
char maps[150][150];
int xiang[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, m;
void dfs(int x, int y, char c) {
  int i, tx, ty;
  for (i = 0; i < 4; i++) {
    tx = x + xiang[i][0];
    ty = y + xiang[i][1];
    if (tx < 0 || ty < 0 || tx >= n || ty >= m || maps[tx][ty] != c)
      continue;
    maps[tx][ty] = '.';
    dfs(tx, ty, c);
  }
}
int main() {
  while (scanf("%d%d", &n, &m) && n && m) {
    int i, j;
    for (i = 0; i < n; i++) {
      scanf("%s", maps[i]);
    }
    int ans = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (maps[i][j] != '.') {
          ans++;
          dfs(i, j, maps[i][j]);
          maps[i][j] = '.';
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int N, M;
char room[20][20];
int next_step[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int number;
void dfs(int x, int y, char c) {
  room[x][y] = 'X';
  for (int i = 0; i < 4; i++) {
    int nx = x + next_step[i][0], ny = y + next_step[i][1];
    if (nx >= 0 && nx < N && ny >= 0 && ny < M && room[nx][ny] == c) {
      dfs(nx, ny, c);
    }
  }
}
void solve() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++) {
      if (room[i][j] != 'X') {
        dfs(i, j, room[i][j]);
        number++;
      }
    }
  cout << number << endl;
}
int main(int argc, char const *argv[]) {
  while (cin >> M >> N, N != 0) {
    number = 0;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++) {
        cin >> room[i][j];
      }
    solve();
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

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

char a[105][105];
int used[105][105];
int n, m;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void dfs(int x, int y) {
  used[x][y] = 1;
  for (int i = 0; i < 4; i++) {
    int xx = x + dir[i][0];
    int yy = y + dir[i][1];
    if (xx >= 0 && xx < n && yy >= 0 && yy < m && (a[xx][yy] == a[x][y]) &&
        used[xx][yy] == 0) {
      dfs(xx, yy);
    }
  }
  return;
}
int main() {
  int i, j, res;
  while (~scanf("%d %d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    res = 0;
    memset(used, 0, sizeof(used));
    for (i = 0; i < n; i++) {
      scanf("%s", a[i]);
    }
    for (i = 0; i < n; i++)
      for (j = 0; j < m; j++) {
        if ((a[i][j] == '#' || a[i][j] == '*' || a[i][j] == '@') &&
            used[i][j] == 0) {
          dfs(i, j);
          res++;
        }
      }
    printf("%d\\n", res);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int N, M, ans;
char map[50][50];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

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

int main() {
  while (scanf("%d %d", &N, &M) != EOF) {
    if (N == 0 && M == 0)
      break;
    ans = 0;
    for (int i = 0; i < N; i++) {
      scanf("%s", map[i]);
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        if (map[i][j] == '#') {
          dfs(i, j, '#');
          ans++;
        } else if (map[i][j] == '@') {
          dfs(i, j, '@');
          ans++;
        } else if (map[i][j] == '*') {
          dfs(i, j, '*');
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }

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

#include "cstdio"
#include "cstring"
using namespace std;

const int maxn = 120;
int n, m, ans;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int vis[maxn][maxn];
char mp[maxn][maxn];

void dfs(int x, int y, char t) {
  vis[x][y] = 1;
  for (int i = 0; i < 4; i++) {
    int px = x + dx[i];
    int py = y + dy[i];
    if (px >= 0 && py >= 0 && px < n && py < m && !vis[px][py] &&
        mp[px][py] == t)
      dfs(px, py, t);
  }
}

int main() {
  // freopen("c.txt","r",stdin);
  while (scanf("%d%d", &n, &m) && n && m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        mp[i][j] = getchar();
        while (mp[i][j] == '\\0' || mp[i][j] == '\\n')
          mp[i][j] = getchar();
      }
    ans = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (!vis[i][j]) {
          ans++;
          dfs(i, j, mp[i][j]);
        }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: -11

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 1002
#define maxc 10
using namespace std;
struct point {
  int r, c;
};
char graph[maxn][maxn];
int len[maxn][maxn];
bool mark[maxn][maxn];
point node[maxc];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int main(void) {
  int h, w, cheese, tr, tc, answer = 0;
  scanf("%d%d%d", &h, &w, &cheese);
  memset(graph, 0, sizeof(graph));
  memset(node, 0, sizeof(node));
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      do
        scanf("%c", &graph[i][j]);
      while (graph[i][j] == ' ' || graph[i][j] == '\\n');
      if (graph[i][j] >= '0' && graph[i][j] <= '9') {
        node[graph[i][j] - '0'].r = i;
        node[graph[i][j] - '0'].c = j;
      } else if (graph[i][j] == 'S') {
        node[0].r = i;
        node[0].c = j;
      }
    }
  for (int cnt = 0; cnt < cheese; cnt++) {
    memset(len, 0, sizeof(len));
    memset(mark, false, sizeof(mark));
    for (int i = 0; i <= h + 1; i++) {
      mark[i][0] = true;
      mark[i][w + 1] = true;
    }
    for (int j = 0; j <= w + 1; j++) {
      mark[0][j] = true;
      mark[h + 1][j] = true;
    }
    queue<point> q;
    q.push(node[cnt]);
    len[node[cnt].r][node[cnt].c] = 0;
    point ongo, togo;
    while (!q.empty()) {
      ongo = q.front();
      q.pop();
      if (mark[ongo.r][ongo.c])
        continue;
      mark[ongo.r][ongo.c] = true;
      if (ongo.r == node[cnt + 1].r && ongo.c == node[cnt + 1].c) {
        answer += len[ongo.r][ongo.c];
        break;
      }
      for (int i = 0; i < 4; i++) {
        togo.r = ongo.r + dir[i][0];
        togo.c = ongo.c + dir[i][1];
        if (mark[togo.r][togo.c] == false && graph[togo.r][togo.c] != 'X') {
          len[togo.r][togo.c] = len[ongo.r][ongo.c] + 1;
          q.push(togo);
        }
      }
    }
  }
  printf("%d\\n", answer);
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int sum, n, m;
int k;
char map[105][105];
int dx[10] = {-1, 1, 0, 0};
int dy[10] = {0, 0, -1, 1};
void dfs1(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m)
    return;
  if (map[x][y] == '#') {
    map[x][y] = ' ';
    for (int i = 0; i < 4; i++) {
      dfs1(x + dx[i], y + dy[i]);
    }
  }
}
void dfs2(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m)
    return;
  if (map[x][y] == '@') {
    map[x][y] = ' ';
    for (int i = 0; i < 4; i++) {
      dfs2(x + dx[i], y + dy[i]);
    }
  }
}
void dfs3(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m)
    return;
  if (map[x][y] == '*') {
    map[x][y] = ' ';
    for (int i = 0; i < 4; i++) {
      dfs3(x + dx[i], y + dy[i]);
    }
  }
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    getchar();
    if (m == 0 || n == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%c", &map[i][j]);
      }
      getchar();
    }
    sum = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (map[i][j] == '#') {
          sum++;
          dfs1(i, j);
        }
        if (map[i][j] == '@') {
          sum++;
          dfs2(i, j);
        }
        if (map[i][j] == '*') {
          sum++;
          dfs3(i, j);
        }
      }
    }
    printf("%d\\n", sum);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define N 10
int h, w;
char map[N][N + 1];
int cnt;

void dfs(int x, int y, char c) {
  map[x][y] = 'X';
  int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  for (int i = 0; i < 4; i++) {
    int nx = x + d[i][0], ny = y + d[i][1];
    if (0 <= nx && nx < h && 0 <= ny && ny < w && map[nx][ny] == c)
      dfs(nx, ny, c);
  }
}

void solve() {
  cnt = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] != 'X') {
        dfs(i, j, map[i][j]);
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (cin >> h >> w, h || w) {
    for (int i = 0; i < h; i++)
      cin >> map[i];
    solve();
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
char map[105][105];
int n, m, dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0}, ans;
void dfs(int x, int y, char ch) {
  map[x][y] = 0;
  for (int i = 0; i < 4; ++i) {
    int tx = x + dx[i], ty = y + dy[i];
    if (tx < 0 || tx >= n || ty < 0 || ty >= m || map[tx][ty] != ch) {
      continue;
    }
    dfs(tx, ty, ch);
  }
}
int main() {
  while (scanf("%d%d", &n, &m), n | m) {
    for (int i = 0; i < n; ++i) {
      scanf("%s", &map[i]);
    }
    ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (map[i][j]) {
          ++ans;
          dfs(i, j, map[i][j]);
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int w, h;
char adj[25][25];
bool vis[25][25];
int ans;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
bool is_in(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w)
    return true;
  return false;
}
void dfs(int x, int y) {
  // printf("66");
  for (int i = 0; i < 4; i++) {
    int xx = dx[i] + x, yy = dy[i] + y;
    if (is_in(xx, yy) && adj[xx][yy] == adj[x][y] && !vis[xx][yy]) {
      vis[xx][yy] = true;
      dfs(xx, yy);
    }
  }
}
inline void solve() {
  ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!vis[i][j]) {
        ans++;
        dfs(i, j);
      }
    }
  }
  printf("%d\\n", ans);
}
int main() {
  // freopen("1.txt","r",stdin);
  while (scanf("%d%d", &w, &h) != EOF) {
    if (w == 0 && h == 0)
      break;
    memset(vis, false, sizeof(vis));
    for (int i = 0; i < h; i++) {
      scanf("%s", adj[i]);
    }
    solve();
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1000, inf = 0x3f3f3f3f;
char maze[maxn][maxn];
int h, w, ans = 0;
void dfs(int x, int y, char sig) {
  maze[x][y] = '.';
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      if (i * j == 0) {
        int nx = x + i, ny = y + j;
        if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == sig)
          dfs(nx, ny, sig);
      }
    }
  }
}
int main() {
  // freopen("read.txt", "r", stdin);
  while (cin >> h >> w && h && w) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> maze[i][j];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (maze[i][j] != '.') {
          char c = maze[i][j];
          dfs(i, j, c);
          ans++;
        }
      }
    cout << ans << endl;
    ans = 0;
  }
  // fclose(stdin);
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int n, m, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, ans;
char c[30][30], k;
void DFS(int x, int y) {
  if (c[x][y] == k && x >= 1 && x <= n && y >= 1 && y <= m) {
    c[x][y] = '?';
  } else
    return;
  for (int i = 0; i < 4; i++)
    DFS(x + dx[i], y + dy[i]);
  return;
}
int main() {
  while (1) {
    cin >> n >> m;
    if (m == 0 && n == 0)
      return 0;
    int fx, fy;
    ans = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        cin >> c[i][j];
      }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (c[i][j] != '?') {
          ans++;
          k = c[i][j];
          DFS(i, j);
        }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

int x[4] = {1, -1, 0, 0};
int y[4] = {0, 0, 1, -1};
int a[200][200], n, m, ans;
int cn, nn, mm, v[200][200], q[1000000][2];
char c;

void doit(int p, int qq) {
  int t = 0, w = 0;
  q[0][0] = p;
  q[0][1] = qq;
  for (t = w = 0; t <= w; t++) {
    for (int r = 0; r <= 3; r++) {
      int xx = q[t][0] + x[r];
      int yy = q[t][1] + y[r];
      if (a[xx][yy] == a[q[t][0]][q[t][1]] && v[xx][yy] == 0)
        if (xx > 0 && xx <= n && yy > 0 && yy <= m) {
          w++;
          q[w][0] = xx;
          q[w][1] = yy;
          v[xx][yy] = 1;
        }
    }
  }
}

int main(void) {
  while (1) {
    memset(a, 0, sizeof(a));
    memset(v, 0, sizeof(v));
    ans = 0;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%c", &c);
      for (int j = 1; j <= m; j++) {
        scanf("%c", &c);
        if (c == '#')
          a[i][j] = 1;
        else if (c == '@')
          a[i][j] = 2;
        else
          a[i][j] = 3;
      }
    }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        if (v[i][j] == 0) {
          ans++;
          doit(i, j);
        }
      }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int M = 500;
int res, ans;
char a[M][M];
bool vis[M][M];
void func(int x, int y) {
  vis[x][y] = true;
  if (a[x][y + 1] == a[x][y] && (!vis[x][y + 1]))
    func(x, y + 1);
  if (a[x + 1][y] == a[x][y] && (!vis[x + 1][y]))
    func(x + 1, y);
  if (a[x][y - 1] == a[x][y] && (!vis[x][y - 1]))
    func(x, y - 1);
  if (a[x - 1][y] == a[x][y] && (!vis[x - 1][y]))
    func(x - 1, y);
  res = 1;
  return;
}
int main() {
  int h, l;
  while (~scanf("%d%d", &l, &h)) {
    if (l * h == 0)
      return 0;
    for (int j = 1; j <= l; j++) {
      scanf("\\n");
      for (int i = 1; i <= h; i++)
        scanf("%c", &a[j][i]);
    }
    for (int j = 1; j <= l; j++) {
      for (int i = 1; i <= h; i++) {
        if (!vis[i][j])
          func(i, j);
        ans += res, res = 0;
      }
    }
    printf("%d\\n", ans);
    memset(a, 0, sizeof(a));
    memset(vis, 0, sizeof(vis));
    res = ans = 0;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>

const int MAX_H = 100;
const int MAX_W = 100;

//?入
int H, W;
char g[MAX_H][MAX_W + 1];

int d[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}}; // 方向

// 深度?先遍?g[i][j]同?局域
void dfs(int x, int y, int c) {
  for (int i = 0; i < 4; i++) {
    int nx = x + d[i][0], ny = y + d[i][1];
    if (0 <= nx && nx < H && 0 <= ny && ny < W && g[nx][ny] == c) {
      g[nx][ny] = '1';
      dfs(nx, ny, c);
    }
  }
}

void solve() {
  // 遍?果?，?到一个新位置，遍??位置能到?的地方
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (g[i][j] != '1') {
        ans++;
        char c = g[i][j];
        g[i][j] = '1';
        dfs(i, j, c);
      }
    }
  }
  printf("%d\\n", ans);
}

int main(int argc, char const *argv[]) {

  while (scanf("%d %d", &H, &W)) {
    if (H == 0 && W == 0)
      break;
    for (int i = 0; i < H; i++) {
      scanf("%s", g[i]);
    }
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
char s[30][30];
int b[30][30], m, n, ans;
void dfs(int x, int y) {
  int next[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
  int k, tx, ty;
  b[x][y] = 1;
  for (k = 0; k < 4; k++) {
    tx = next[k][0] + x;
    ty = next[k][1] + y;
    if (tx < 0 || ty < 0 || tx > m - 1 || ty > n - 1)
      continue;
    if (b[tx][ty] == 0 && s[tx][ty] == s[x][y])
      dfs(tx, ty);
  }
}
int main() {
  while (cin >> m >> n, m != 0 || n != 0) {
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++)
        scanf(" %c", &s[i][j]);
    memset(b, 0, sizeof(b));
    ans = 0;
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++) {
        if (b[i][j] == 0) {
          ans++;
          dfs(i, j);
        }
      }
    cout << ans << endl;
  }
  return 0;
}