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

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

int main() {

  int a, b;

  while (1) {
    cin >> a >> b;
    if (a == 0 && b == 0)
      break;
    char P[150][150] = {};
    for (int i = 0; i < a; i++) {
      string q;
      cin >> q;
      for (int I = 0; I < b; I++)
        P[i][I + 1] = q[I];
    }
    int X = 1;
    int M[120][120] = {};
    for (int i = 0; i < a; i++)
      for (int j = 1; j <= b; j++) {
        if (P[i][j] != P[i][j - 1])
          X++;
        M[i][j] = X;
        // if(P[i][j]==P[i][j-1]&&j>0)M[i][j]==M[i][j-1];
      }
    /*
    for(int i=0;i<a;i++){cout<<endl;
    for(int j=1;j<=b;j++)
    { cout<<M[i][j];}
    }
*/

    for (int i = 0; i < a; i++)
      for (int j = 1; j <= b; j++)
        if (P[i + 1][j] == P[i][j]) {
          int Y = M[i + 1][j];
          // M[i][j]=M[i-1][j];
          for (int I = 0; I < a; I++)
            for (int J = 0; J <= b; J++)
              if (M[I][J] == Y)
                M[I][J] = M[i][j];
        }

    bool F[10004] = {};
    int ans = 0;
    for (int i = 0; i < a; i++)
      for (int j = 1; j <= b; j++) {
        F[M[i][j]] = true;
      }
    for (int i = 1; i < 10002; i++)
      if (F[i] == true)
        ans++; /*
for(int i=0;i<a;i++){cout<<endl;
for(int j=1;j<=b;j++)
{ cout<<M[i][j];}
}


*/

    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct TA {
  string name;
  int po;
  int rr;
};
int main() {
  int n;
  struct TA N[11];
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> N[i].name;
      int a, b, c;
      cin >> a >> b >> c;
      N[i].po = a * 3 + c * 1;
      N[i].rr = i;
    }
    for (int i = 0; i < n; i++)
      for (int j = n - 1; j > i; j--)
        if (N[j].po > N[j - 1].po) {
          struct TA T = N[j];
          N[j] = N[j - 1];
          N[j - 1] = T;
        }
    for (int i = 0; i < n; i++)
      cout << N[i].name << "," << N[i].po << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int H, W;
char farms[101][101];
const int X[4] = {-1, 0, 0, 1};
const int Y[4] = {0, -1, 1, 0};

void dfs(int x, int y) {
  char f = farms[x][y];
  farms[x][y] = '.';
  for (int i = 0; i < 4; i++) {
    int nx = x + X[i], ny = y + Y[i];
    if (0 <= nx && nx < H && 0 <= ny && ny < W && farms[nx][ny] == f) {
      dfs(nx, ny);
    }
  }
  return;
}

int solve() {
  int res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (farms[i][j] != '.') {
        dfs(i, j);
        res++;
      }
    }
  }
  return res;
}

int main() {
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 101; j++) {
      farms[i][j] = '.';
    }
  }
  cin >> H >> W;
  while (!(H == 0 && W == 0)) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> farms[i][j];
      }
    }
    cout << solve() << endl;
    cin >> H >> W;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

using namespace std;

int H, W;
char farms[101][101];

void dfs(int x, int y) {
  char f = farms[x][y];
  farms[x][y] = '.';
  for (int dx = -1; dx <= 1; dx++) {
    if (dx == -1 || dx == 1) {
      int dy = 0;
      int nx = x + dx, ny = y + dy;
      if (0 <= nx && nx < W && 0 <= ny && ny < H && farms[nx][ny] == f) {
        dfs(nx, ny);
      }
    }
    if (dx == 0) {
      for (int dy = -1; dy <= 1; dx = dy + 2) {
        int nx = x + dx, ny = y + dy;
        if (0 <= nx && nx < W && 0 <= ny && ny < H && farms[nx][ny] == f) {
          dfs(nx, ny);
        }
      }
    }
  }
  return;
}

int solve() {
  int res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (farms[i][j] != '.') {
        dfs(i, j);
        res++;
      }
    }
  }
  return res;
}

int main() {
  cin >> H >> W;
  while (!(H == 0 && W == 0)) {
    for (int i = 0; i < 101; i++) {
      for (int j = 0; j < 101; j++) {
        farms[i][j] = '.';
      }
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> farms[i][j];
      }
    }
    cout << solve() << endl;
    cin >> H >> W;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void destroy(vector<string> &field, int i, int j, int symbol) {
  if (field[i][j] == symbol) {
    int symbol = field[i][j];
    field[i][j] = '-';
    destroy(field, i - 1, j, symbol);
    destroy(field, i + 1, j, symbol);
    destroy(field, i, j - 1, symbol);
    destroy(field, i, j + 1, symbol);
  }
}

int search(vector<string> &field, int i, int j) {
  static int counter = 0;
  if (i == 1 && j == 1) {
    counter = 0;
  }
  if (field[i][j] == '-') {
    return counter;
  }
  counter++;
  int symbol = field[i][j];
  field[i][j] = '-';
  destroy(field, i - 1, j, symbol);
  destroy(field, i + 1, j, symbol);
  destroy(field, i, j - 1, symbol);
  destroy(field, i, j + 1, symbol);
  return counter;
}

int main() {
  while (1) {
    int h, w;
    cin >> h >> w;
    if (!(h | w)) {
      return EXIT_SUCCESS;
    }
    vector<string> field(h + 2);

    field[0] = "";
    field[h + 1] = "";
    for (int j = 0; j <= w + 1; j++) {
      field[0] += "-";
      field[h + 1] += "-";
    }
    for (int i = 1; i <= h; i++) {
      cin >> field[i];
      field[i] = "-" + field[i] + "-";
    }

    int n;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        n = search(field, i, j);
      }
    }

    cout << n << endl;
  }
}

Error: -11

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void destroy(vector<string> &field, int i, int j, int symbol) {
  if (field[i][j] == symbol) {
    int symbol = field[i][j];
    field[i][j] = '-';
    destroy(field, i - 1, j, symbol);
    destroy(field, i + 1, j, symbol);
    destroy(field, i, j - 1, symbol);
    destroy(field, i, j + 1, symbol);
  }
}

int search(vector<string> &field, int i, int j) {
  static int counter = 0;
  if (i == 1 && j == 1) {
    counter = 0;
  }
  if (field[i][j] == '-') {
    return counter;
  }
  counter++;
  int symbol = field[i][j];
  field[i][j] = '-';
  destroy(field, i - 1, j, symbol);
  destroy(field, i + 1, j, symbol);
  destroy(field, i, j - 1, symbol);
  destroy(field, i, j + 1, symbol);
  return counter;
}

int main() {
  while (1) {
    int h, w;
    cin >> h >> w;
    if (!(h | w)) {
      return EXIT_SUCCESS;
    }

    vector<string> field;
    field.reserve(h + 2);

    field[0] = "";
    field[h + 1] = "";
    for (int j = 0; j <= w + 1; j++) {
      field[0] += "-";
      field[h + 1] += "-";
    }

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

    int n;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        n = search(field, i, j);
      }
    }
    cout << n << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

char F[102][102];

void DFS(int Y, int X, char f) {
  if (F[Y][X] != f)
    return;

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

  DFS(Y - 1, X, f);
  DFS(Y, X + 1, f);
  DFS(Y + 1, X, f);
  DFS(Y, X - 1, f);
}

int main() {
  while (true) {
    int H, W;
    cin >> H >> W;

    if (H == 0 && W == 0)
      break;

    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        cin >> F[y][x];
      }
    }

    int cnt = 0;
    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        if (F[y][x] != '.') {
          cnt++;
          DFS(y, x, F[y][x]);
        }
      }
    }

    cout << cnt << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

char F[102][102];

void DFS(int Y, int X, int e) {
  if (F[Y][X] != e) {
    return;
  }
  F[Y][X] = '0';

  DFS(Y - 1, X, e);
  DFS(Y, X + 1, e);
  DFS(Y + 1, X, e);
  DFS(Y, X - 1, e);
}

int main() {
  int H, W;
  cin >> H >> W;
  while (true) {
    for (int y = 1; y <= H; y++) {
      for (int x = 1; x <= W; x++) {
        cin >> F[y][x];
      }
    }
    int a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      break;
    }
  }
  int cnt = 0;
  for (int y = 1; y <= H; y++) {
    for (int x = 1; x <= W; x++) {
      if (F[y][x] != '0') {
        cnt++;
        DFS(y, x, F[y][x]);
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void dfs(int x, int y, char c);

int main() {
  char buf[101];
  int a;
  char c;

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

    for (int i = 0; i < h; i++) {
      cin >> buf;
      memcpy(hatake[i], buf, sizeof(char) * w);
    }

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

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

Error: TLE

Fixed submission:

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

void dfs(int x, int y, char c);

int main() {
  char buf[101];
  int a;
  char c;

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

    for (int i = 0; i < h; i++) {
      cin >> buf;
      memcpy(hatake[i], buf, sizeof(char) * w);
    }

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

void dfs(int x, int y, char c) {
  int nx, ny;
  for (int i = 0; i < 4; i++) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < w && hatake[nx][ny] == c) {
      hatake[nx][ny] = '.';
      dfs(nx, ny, c);
    }
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e5 + 10;

#define MAX_W 100
#define MAX_H 100
char farm[MAX_W][MAX_H];
int W, H;
const int direction[4][2] = {
    {-1, 0},
    {1, 0},
    {0, -1},
    {0, 1},
};

void dfs(int x, int y, char tree) {
  farm[x][y] = 'x';
  for (int i = 0; i < 4; ++i) {
    int nx = x + direction[i][0];
    int ny = y + direction[i][1];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && farm[nx][ny] == tree) {
      dfs(nx, ny, tree);
    }
  }
}

int main() {

  while (cin >> H >> W, W > 0) {
    int res = 0;
    int x, y;
    for (y = 0; y < H; ++y) {
      for (x = 0; x < W; ++x) {
        cin >> farm[x][y];
      }
    }

    for (y = 0; y < H; ++y) {
      for (x = 0; x < W; ++x) {
        if (farm[x][y] != 'x') {
          dfs(x, y, farm[x][y]);
          ++res;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

void Dfs(vector<vector<char>> &table, int h, int w, int x, int y) {
  char cur = table[x][y];
  table[x][y] = '.';
  for (int i = 0; i < 4; ++i) {
    int next_x = x + dir[i][0];
    int next_y = y + dir[i][1];
    if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h &&
        table[next_x][next_y] == cur) {
      Dfs(table, h, w, next_x, next_y);
    }
  }
}

int main() {
  int h, w;
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    vector<vector<char>> table(h, vector<char>(w));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> table[i][j];
      }
    }
    int ret = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (table[i][j] != '.') {
          Dfs(table, h, w, i, j);
          ++ret;
        }
      }
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e5 + 10;

#define MAX_W 100
#define MAX_H 100
char farm[MAX_W][MAX_H];
int W, H;
const int direction[4][2] = {
    {-1, 0},
    {1, 0},
    {0, -1},
    {0, 1},
};

void dfs(int x, int y, char tree) {
  farm[x][y] = 'x';
  for (int i = 0; i < 4; ++i) {
    int nx = x + direction[i][0];
    int ny = y + direction[i][1];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && farm[nx][ny] == tree) {
      dfs(nx, ny, tree);
    }
  }
}

int main() {

  while (cin >> H >> W, W > 0) {
    int res = 0;
    int x, y;
    for (y = 0; y < H; ++y) {
      for (x = 0; x < W; ++x) {
        cin >> farm[x][y];
      }
    }

    for (y = 0; y < H; ++y) {
      for (x = 0; x < W; ++x) {
        if (farm[x][y] != 'x') {
          dfs(x, y, farm[x][y]);
          ++res;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

void Dfs(vector<vector<char>> &table, int h, int w, int x, int y) {
  char cur = table[x][y];
  table[x][y] = '.';
  for (int i = 0; i < 4; ++i) {
    int next_x = x + dir[i][0];
    int next_y = y + dir[i][1];
    if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h &&
        table[next_x][next_y] == cur) {
      Dfs(table, h, w, next_x, next_y);
    }
  }
}

int main() {
  int h, w;
  while (cin >> h >> w, w > 0) {
    if (h == 0 && w == 0)
      break;
    vector<vector<char>> table(h, vector<char>(w));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> table[i][j];
      }
    }
    int ret = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (table[i][j] != '.') {
          Dfs(table, h, w, i, j);
          ++ret;
        }
      }
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

char table[100][100];
int w, h;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void Dfs(int x, int y, char cur) {
  table[x][y] = 'X';
  for (int i = 0; i < 4; ++i) {
    int next_x = x + dir[i][0];
    int next_y = y + dir[i][1];
    if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h &&
        table[next_x][next_y] == cur) {
      Dfs(next_x, next_y, cur);
    }
  }
}

int main() {

  while (cin >> h >> w, w > 0) {
    int res = 0;
    int x, y;
    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        cin >> table[x][y];
      }
    }

    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        if (table[x][y] != 'X') {
          Dfs(x, y, table[x][y]);
          ++res;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

char table[100][100];
int w, h;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void Dfs(int x, int y, char cur) {
  table[x][y] = 'X';
  for (int i = 0; i < 4; ++i) {
    int next_x = x + dir[i][0];
    int next_y = y + dir[i][1];
    if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h &&
        table[next_x][next_y] == cur) {
      Dfs(next_x, next_y, cur);
    }
  }
}

int main() {

  while (cin >> h >> w, w > 0) {
    int res = 0;
    int x, y;
    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        cin >> table[x][y];
      }
    }

    for (y = 0; y < h; ++y) {
      for (x = 0; x < w; ++x) {
        if (table[x][y] != 'x') {
          Dfs(x, y, table[x][y]);
          ++res;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct P {
  int y;
  int x;
  char c; // 持ってるやつ
  P(int y, int x, char c) : y(y), x(x), c(c) {}
};
char g[111][111];
int d[111][111];
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
void dfs(int y, int x, char c);
int h, w;
int main() {
  while (1) {
    cin >> h >> w;
    if (!h && !w)
      break;
    for (int i = 0; i < h; i++) {
      cin >> g[i];
    }
    memset(d, 0, sizeof(d));
    int ret = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (g[i][j] == '@' || g[i][j] == '#' || g[i][j] == '*') {
          dfs(i, j, g[i][j]);
          ret++;
        }
      }
    }
    cout << ret << endl;
  }
}
void dfs(int y, int x, char c) {
  queue<P> que;
  que.push(P(y, x, c));
  d[y][x] = 1;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    g[p.y][p.x] = '\\0';
    for (int i = 0; i < 4; i++) {
      int ty = p.y + dy[i];
      int tx = p.x + dx[i];
      if (g[ty][tx] == c && d[ty][tx] == 0) {
        d[ty][tx] = 1;
        g[ty][tx] = '\\0';
        que.push(P(ty, tx, c));
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
struct P {
  int y;
  int x;
  char c; // 持ってるやつ
  P(int y, int x, char c) : y(y), x(x), c(c) {}
};
char g[111][111];
int d[111][111];
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
void dfs(int y, int x, char c);
int h, w;
int main() {
  while (1) {
    cin >> h >> w;
    if (!h && !w)
      break;
    for (int i = 0; i < w; i++) {
      cin >> g[i];
    }
    memset(d, 0, sizeof(d));
    int ret = 0;
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        if (g[i][j] != '\\0') {
          dfs(i, j, g[i][j]);
          ret++;
        }
      }
    }
    cout << ret << endl;
  }
}
void dfs(int y, int x, char c) {
  queue<P> que;
  que.push(P(y, x, c));
  d[y][x] = 1;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    g[p.y][p.x] = '\\0';
    for (int i = 0; i < 4; i++) {
      int ty = p.y + dy[i];
      int tx = p.x + dx[i];
      if (g[ty][tx] == c && d[ty][tx] == 0) {
        d[ty][tx] = 1;
        g[ty][tx] = '\\0';
        que.push(P(ty, tx, c));
      }
    }
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int h, w, c;
char map[101][101];
char fru;
void dfs(int i, int j) {
  int dx, dy, nx, ny;
  map[i][j] = '.';
  for (dy = -1; dy <= 1; dy++) {
    for (dx = -1; dx <= 1; dx++) {
      if (dx * dy == 0) { // 斜めはダメ
        ny = i + dy;
        nx = j + dx;
        if (map[ny][nx] == fru && 0 <= ny && ny < h && 0 <= nx && nx < w) {
          dfs(ny, nx);
        }
      }
    }
  }
  return;
}
int main() {
  while (1) {
    c = 0;
    int i, j;
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        switch (map[i][j]) {
        case '@':
          fru = '@';
          dfs(i, j);
          c++;
          break;
        case '#':
          fru = '#';
          dfs(i, j);
          c++;
          break;
        case '*':
          fru = '*';
          dfs(i, j);
          c++;
          break;
        default:
          break;
        }
      }
    }
    cout << c << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int h, w, c;
char map[12][12];
char fru;
void dfs(int i, int j) {
  int dx, dy, nx, ny;
  map[i][j] = '.';
  for (dy = -1; dy <= 1; dy++) {
    for (dx = -1; dx <= 1; dx++) {
      if (dx * dy == 0) {
        ny = i + dy;
        nx = j + dx;
        if (map[ny][nx] == fru && 0 <= ny && ny < h && 0 <= nx && nx < w) {
          dfs(ny, nx);
        }
      }
    }
  }
  return;
}
int main() {
  while (1) {
    c = 0;
    int i, j;
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        switch (map[i][j]) {
        case '@':
          fru = '@';
          dfs(i, j);
          c++;
          break;
        case '#':
          fru = '#';
          dfs(i, j);
          c++;
          break;
        case '*':
          fru = '*';
          dfs(i, j);
          c++;
          break;
        default:
          break;
        }
      }
    }
    cout << c << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;

vector<string> map;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {1, 0, -1, 0};

void rec(int x, int y, char c) {
  map[y][x] = '.';

  for (int i = 0; i < 4; i++) {
    if (0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H) {
      if (map[y + dy[i]][x + dx[i]] == c) {
        rec(x + dx[i], y + dy[i], c);
      }
    }
  }
}

int solve() {
  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (map[i][j] != '.') {
        rec(j, i, map[i][j]);
        ret++;
      }
    }
  }

  return ret;
}

int main() {
  while (true) {
    cin >> H >> W;

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

    map = vector<string>(H);

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

    cout << solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int H, W;

vector<string> map;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {1, 0, -1, 0};

void rec(int x, int y, char c) {
  map[y][x] = '.';

  for (int i = 0; i < 4; i++) {
    if (0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H) {
      if (map[y + dy[i]][x + dx[i]] == c) {
        rec(x + dx[i], y + dy[i], c);
      }
    }
  }
}

int solve() {
  int ret = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (map[i][j] != '.') {
        rec(j, i, map[i][j]);
        ret++;
      }
    }
  }

  return ret;
}

int main() {
  while (true) {
    cin >> H >> W;

    map = vector<string>(H);

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

    cout << solve() << endl;
  }

  return 0;
}