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

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

string s;

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

char check(int i, int j, int di, int dj) {
  char last = '?';
  for (int t = 0; t < 3; ++t) {
    if (i < 0 || i >= 3 || j < 0 || j >= 3) {
      return 'd';
    }
    if (last != '?' && last != s[i * 3 + j]) {
      return 'd';
    }
    last = s[i * 3 + j];
    i += di;
    j += dj;
  }
  if (last == 'o' || last == 'x')
    return last;
  return 'd';
}

int main() {
  while (cin >> s) {
    // cout << s<< endl;
    char ans = 'd';
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        for (int k = 0; k < 8; ++k) {
          char tmp = check(i, j, di[k], dj[k]);
          // cout <<  i << " " << j << " " << tmp << endl;
          if (tmp != 'd') {
            ans = tmp;
            break;
          }
        }
        if (ans != 'd')
          break;
      }
      if (ans != 'd')
        break;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

string s;

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

char check(int i, int j, int di, int dj) {
  char last = '?';
  for (int t = 0; t < 3; ++t) {
    if (i < 0 || i >= 3 || j < 0 || j >= 3) {
      return 'd';
    }
    if (last != '?' && last != s[i * 3 + j]) {
      return 'd';
    }
    last = s[i * 3 + j];
    i += di;
    j += dj;
  }
  if (last == 'o' || last == 'x')
    return last;
  return 'd';
}

int main() {
  while (cin >> s) {
    char ans = 'd';
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        for (int k = 0; k < 9; ++k) {
          char tmp = check(i, j, di[k], dj[k]);
          // cout <<  i << " " << j << " " << tmp << endl;
          if (tmp != 'd') {
            ans = tmp;
            break;
          }
        }
        if (ans != 'd')
          break;
      }
      if (ans != 'd')
        break;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

char field[12][12];
const int direction_x[4] = {0, 0, 1, -1};
const int direction_y[4] = {-1, 1, 0, 0};
int new_x, new_y = 0;

void dfs(int x, int y) {
  field[x][y] = '0';

  for (int i = 0; i < 4; i++) {
    new_x = x + direction_x[i];
    new_y = y + direction_y[i];

    if (0 <= new_x && new_x < 12 && 0 <= new_y && new_y < 12) {
      if (field[new_x][new_y] == '1') {
        dfs(new_x, new_y);
      }
    }
  }

  return;
}

int main() {
  int result = 0;

  while (cin >> field[0]) {
    result = 0;

    for (int j = 1; j < 12; j++) {
      for (int k = 0; k < 12; k++) {
        cin >> field[j][k];
        if (field[j][k] == EOF) {
          return 0;
        }
      }
    }

    for (int j = 0; j < 12; j++) {
      for (int k = 0; k < 12; k++) {
        if (field[j][k] == '1') {
          dfs(j, k);
          result++;
        }
      }
    }

    cout << result << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

char field[12][12];
const int direction_x[4] = {0, 0, 1, -1};
const int direction_y[4] = {-1, 1, 0, 0};
int new_x, new_y = 0;

void dfs(int x, int y) {
  field[x][y] = '0';

  for (int i = 0; i < 4; i++) {
    new_x = x + direction_x[i];
    new_y = y + direction_y[i];

    if (0 <= new_x && new_x < 12 && 0 <= new_y && new_y < 12) {
      if (field[new_x][new_y] == '1') {
        dfs(new_x, new_y);
      }
    }
  }

  return;
}

int main() {
  int result = 0;

  for (int i = 0; i < 20 || cin.eof(); i++) {
    result = 0;

    for (int j = 0; j < 12; j++) {
      for (int k = 0; k < 12; k++) {
        cin >> field[j][k];
      }
    }

    for (int j = 0; j < 12; j++) {
      for (int k = 0; k < 12; k++) {
        if (field[j][k] == '1') {
          dfs(j, k);
          result++;
        }
      }
    }

    cout << result << endl;
  }

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

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

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

void dfs(int x, int y) {
  table[x][y] = '0';
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < 12 && ny >= 0 && ny < 12 && table[nx][ny] == '1') {
      dfs(nx, ny);
    }
  }
}

int main() {
  while (cin >> table[0]) {
    for (int i = 1; i < 12; i++)
      cin >> table[i];
    int ans = 0;
    rep(i, 12) {
      rep(j, 12) {
        if (table[i][j] == '1') {
          dfs(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

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

void dfs(int x, int y) {
  table[x][y] = '0';
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < 12 && ny >= 0 && ny < 12 && table[nx][ny] == '1') {
      dfs(nx, ny);
    }
  }
}

int main() {
  while (1) {
    rep(i, 12) {
      rep(j, 12) { cin >> table[i][j]; }
    }
    int ans = 0;
    rep(i, 12) {
      rep(j, 12) {
        if (table[i][j] == '1') {
          dfs(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int cnt;
char field[13][13];

void solve(int x, int y) {
  field[y][x] = '0';
  if (x + 1 < 12)
    if (field[y][x + 1] == '1')
      solve(x + 1, y);
  if (x - 1 >= 0)
    if (field[y][x - 1] == '1')
      solve(x - 1, y);
  if (y + 1 < 12)
    if (field[y + 1][x] == '1')
      solve(x, y + 1);
  if (y - 1 >= 0)
    if (field[y - 1][x] == '1')
      solve(x, y - 1);
}

int main() {
  while (1) {
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        cin >> field[i][j];
        if (cin.eof())
          return 0;
      }
    }
    cnt = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (field[i][j] == '1') {
          cnt++;
          solve(j, i);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int cnt;
char field[13][13];

void solve(int x, int y) {
  field[y][x] = '0';
  if (x + 1 < 12)
    if (field[y][x + 1] == '1')
      solve(x + 1, y);
  if (x - 1 >= 0)
    if (field[y][x - 1] == '1')
      solve(x - 1, y);
  if (y + 1 < 12)
    if (field[y + 1][x] == '1')
      solve(x, y + 1);
  if (y - 1 >= 0)
    if (field[y - 1][x] == '1')
      solve(x, y - 1);
}

int main() {
  while (1) {
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        cin >> field[i][j];
      }
    }
    cnt = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (field[i][j] == '1') {
          cnt++;
          solve(j, i);
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<string> map = vector<string>(12);
int chk[12][12];
int cnt;

void rec(int x, int y) {

  if (x < 0 || x >= 12 || y < 0 || y >= 12 || chk[x][y] == 1 ||
      map[x][y] == '0')
    return;

  chk[x][y] = 1;
  map[x][y] = '0';

  rec(x + 1, y);
  rec(x - 1, y);
  rec(x, y + 1);
  rec(x, y - 1);
}

int main() {

  while (1) {
    cnt = 0;

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

      if (cin.eof())
        return 0;

      for (int j = 0; j < 12; j++) {
        chk[i][j] = 0;
      }
    }

    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++) {
        if (chk[i][j] == 0 && map[i][j] == '1') {
          rec(i, j);
          cnt++;
        }
      }

    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

char map[12][12];
int chk[12][12];
int count;

void rec(int x, int y) {

  if (x < 0 || x >= 12 || y < 0 || y >= 12 || chk[x][y] == 1 || map[x][y] == 0)
    return;

  chk[x][y] = 1;
  map[x][y] = 0;

  rec(x + 1, y);
  rec(x - 1, y);
  rec(x, y + 1);
  rec(x, y - 1);
}

int main() {

  while (1) {
    count = 0;

    for (int i = 0; i < 12; i++) {
      if (!scanf("%s", &map[i]))
        return 0;

      for (int j = 0; j < 12; j++) {
        chk[i][j] = 0;
        if (map[i][j] == '1')
          map[i][j] = 1;
        else if (map[i][j] == '0')
          map[i][j] = 0;
      }
    }

    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++) {
        if (chk[i][j] == 0 && map[i][j] == 1) {
          rec(i, j);
          count++;
        }
      }

    cout << count << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
int dy[] = {1, 0, 0, -1};
int dx[] = {0, 1, -1, 0};
vector<string> s(12);

void dfs(int y, int x) {
  s[y][x] = '0';
  rep(i, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    if (!(0 <= ny && ny < 12 && 0 <= nx && nx < 12))
      continue;
    if (s[ny][nx] == '0')
      continue;
    dfs(ny, nx);
  }
  return; //???????????±?????¢????????¨?????§???????????????
}

int main(void) {
  while (1) {
    int cnt = 0;
    rep(i, 12) {
      if (!(cin >> s[i]))
        return 0; //??????????????±???????????´???
    }

    rep(y, 12) rep(x, 12) {
      if (s[y][x] == '1') {
        dfs(y, x);
        cnt++;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
int dy[] = {1, 0, 0, -1};
int dx[] = {0, 1, -1, 0};
vector<string> s(12);

void dfs(int y, int x) {
  s[y][x] = '0';
  rep(i, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    if (!(0 <= ny && ny < 12 && 0 <= nx && nx < 12))
      continue;
    if (s[ny][nx] == '0')
      continue;
    dfs(ny, nx);
  }
  return; //???????????±?????¢????????¨?????§???????????????
}
int main(void) {
  while (1) {
    int cnt = 0;
    rep(i, 12) cin >> s[i];
    rep(y, 12) rep(x, 12) {
      if (s[y][x] == '1') {
        dfs(y, x);
        cnt++;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

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

void field(std::vector<std::string> &f, int x, int y);

std::istream &input(std::vector<std::string> &vec) {
  for (int i = 0; i < 12; i++) {
    std::cin >> vec[i];
  }
  return std::cin;
}

int main() {
  std::vector<std::string> vec(12);
  while (input(vec)) {

    int n = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (vec[i][j] == '1')
          field(vec, i, j), n++;
      }
    }
    std::cout << n << std::endl;
  }
  return 0;
}

void field(std::vector<std::string> &f, int x, int y) {
  if (f[x][y] == '0')
    return;
  f[x][y] = '0';
  int sx = x - 1, tx = x + 1, sy = y - 1, ty = y + 1;
  if (sx >= 0)
    field(f, sx, y);
  if (tx < 12)
    field(f, tx, y);
  if (sy >= 0)
    field(f, x, sy);
  if (ty < 12)
    field(f, x, ty);
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
void field(std::vector<std::string> f, int x, int y);
int main() {
  std::vector<std::string> vec(12);
  for (int i = 0; i < 12; i++) {
    std::cin >> vec[i];
  }
  int n = 0;
  for (int i = 0; i < 12; i++) {
    for (int j = 0; j < 12; j++) {
      if (vec[i][j] == '1')
        field(vec, i, j);
      n++;
    }
  }
  std::cout << n;
  return 0;
}
void field(std::vector<std::string> f, int x, int y) {
  f[x][y] = '0';
  int sx = x - 1, tx = x + 1, sy = y - 1, ty = y + 1;
  if (sx >= 0)
    field(f, sx, y);
  if (tx <= 12)
    field(f, tx, y);
  if (sy >= 0)
    field(f, x, sy);
  if (ty <= 12)
    field(f, x, ty);
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded


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

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

bool valid(int r, int c) {
  return r >= 0 && r < 12 && c >= 0 && c < 12 && A[r][c];
}

void dfs_rec(int r, int c) {
  if (!A[r][c])
    return;
  A[r][c] = 0;
  for (int i = 0; i < 4; ++i) {
    if (valid(r + dx[i], c + dy[i]))
      dfs_rec(r + dx[i], c + dy[i]);
  }
}

int main() {
  string row;
  while (cin >> row) {
    for (int r = 0; r < 12; ++r) {
      if (r != 0) {
        cin >> row;
      }
      for (int c = 0; c < 12; ++c) {
        A[r][c] = row[c] - '0';
      }
    }

    int sum = 0;
    for (int r = 0; r < 12; ++r) {
      for (int c = 0; c < 12; ++c) {
        if (A[r][c]) {
          sum++;
          dfs_rec(r, c);
        }
      }
    }

    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

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

bool valid(int r, int c) {
  return r >= 0 && r < 12 && c >= 0 && c < 12 && A[r][c];
}

void dfs_rec(int r, int c) {
  if (!A[r][c])
    return;
  A[r][c] = 0;
  for (int i = 0; i < 4; ++i) {
    if (valid(r + dx[i], c + dy[i]))
      dfs_rec(r + dx[i], c + dy[i]);
  }
}

int main() {
  while (1) {
    for (int r = 0; r < 12; ++r) {
      string row;
      cin >> row;
      for (int c = 0; c < 12; ++c) {
        A[r][c] = row[c] - '0';
      }
    }

    int sum = 0;
    for (int r = 0; r < 12; ++r) {
      for (int c = 0; c < 12; ++c) {
        if (A[r][c]) {
          sum++;
          dfs_rec(r, c);
        }
      }
    }

    cout << sum << endl;
  }

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

#include <cstdio>

char field[12][12];

void rec(int i, int j) {
  field[i][j] = '0';

  if (field[i + 1][j] == '1' && i + 1 < 12)
    rec(i + 1, j);
  if (field[i - 1][j] == '1' && i - 1 > -1)
    rec(i - 1, j);
  if (field[i][j + 1] == '1' && j + 1 < 12)
    rec(i, j + 1);
  if (field[i][j - 1] == '1' && j - 1 > -1)
    rec(i, j - 1);
}
int main() {
  int count = 0;

  while (1) {
    bool end = false;
    for (int i = 0; i < 12; i++) {
      if (scanf("%s", &field[i]) == EOF)
        end = true;
    }
    if (end)
      break;
    count = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (field[i][j] == '1') {
          rec(i, j);
          count++;
        }
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

char field[12][12];

void rec(int i, int j) {
  field[i][j] = '0';

  if (field[i + 1][j] == '1' && i + 1 < 12)
    rec(i + 1, j);
  if (field[i - 1][j] == '1' && i - 1 > -1)
    rec(i - 1, j);
  if (field[i][j + 1] == '1' && j + 1 < 12)
    rec(i, j + 1);
  if (field[i][j - 1] == '1' && j - 1 > -1)
    rec(i, j - 1);
}
int main() {
  int count = 0;
  while (1) {
    for (int i = 0; i < 12; i++) {
      scanf("%s", &field[i]);
    }
    count = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (field[i][j] == '1') {
          rec(i, j);
          count++;
        }
      }
    }
    printf("%d\\n", count);
    putchar('\\n');
  }

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000000 + 7
using namespace std;

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

void dfs(int h, int w) {
  islands[h][w] = '0';
  for (int i = 0; i < 4; i++) {
    if (h + dx[i] >= 0 && h + dx[i] <= 12 && w + dy[i] >= 0 &&
        w + dy[i] <= 12 && islands[h + dx[i]][w + dy[i]] == '1') {
      dfs(h + dx[i], w + dy[i]);
    }
  }
}
int main() {
  string a;
  while (cin >> a) {
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        islands[i][j] = a[j];
      }
      if (i != 11)
        cin >> a;
    }
    int ans = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (islands[i][j] == '1')
          dfs(i, j), ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define INF 1000000000
#define MOD 1000000000 + 7
using namespace std;

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

void dfs(int h, int w) {
  islands[h][w] = '0';
  for (int i = 0; i < 4; i++) {
    if (h + dx[i] >= 0 && h + dx[i] <= 12 && w + dy[i] >= 0 &&
        w + dy[i] <= 12 && islands[h + dx[i]][w + dy[i]] == '1') {
      dfs(h + dx[i], w + dy[i]);
    }
  }
}
int main() {
  while (true) {
    string a;
    for (int i = 0; i < 12; i++) {
      cin >> a;
      for (int j = 0; j < 12; j++) {
        islands[i][j] = a[j];
      }
    }
    int ans = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (islands[i][j] == '1')
          dfs(i, j), ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 99999999
vvi v(60, vi(60, 0));
int w = 12, h = 12;
int mx[] = {0, 0, 0, 1, -1};
int my[] = {1, 0, -1, 0, 0};
void dfs(int y, int x) {
  if (!v[y][x])
    return;
  if (v[y][x]) {
    v[y][x] = 0;
    rep(p, 5) {
      int tx = x + mx[p];
      int ty = y + my[p];
      if (0 <= tx && tx < w && 0 <= ty && ty < h && v[ty][tx] != 0) {
        dfs(ty, tx);
      }
    }
  }
}
int main() {
  char hoge;
  while (cin >> hoge) {
    v[0][0] = hoge - 48;
    char s;
    rep(i, h) rep(j, w) {
      if (!(i + j))
        continue;
      cin >> s;
      v[i][j] = s - 48;
    }
    int ans = 0;
    rep(i, h) rep(j, w) {
      if (v[i][j]) {
        dfs(i, j);
        ans++;
      }
    }
    cout << ans << endl;
    rep(i, 60) rep(j, 60) v[i][j] = 0;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int> PI;
#define MAX 99999999
vvi v(60, vi(60, 0));
int w = 12, h = 12;
int mx[] = {0, 0, 0, 1, -1};
int my[] = {1, 0, -1, 0, 0};
void dfs(int y, int x) {
  if (!v[y][x])
    return;
  if (v[y][x]) {
    v[y][x] = 0;
    rep(p, 5) {
      int tx = x + mx[p];
      int ty = y + my[p];
      if (0 <= tx && tx < w && 0 <= ty && ty < h && v[ty][tx] != 0) {
        dfs(ty, tx);
      }
    }
  }
}
int main() {
  while (true) {
    char s;
    rep(i, h) rep(j, w) {
      cin >> s;
      v[i][j] = s - 48;
    }
    int ans = 0;
    rep(i, h) rep(j, w) {
      if (v[i][j]) {
        dfs(i, j);
        ans++;
      }
    }
    cout << ans << endl;
    rep(i, 60) rep(j, 60) v[i][j] = 0;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define MAX_L 12
using namespace std;
char table[MAX_L][MAX_L];
int fdx[] = {1, 0, -1, 0}, fdy[] = {0, 1, 0, -1};

void dfs(int y, int x) {
  if (x < 0 || x >= MAX_L || y < 0 || y >= MAX_L || table[y][x] == '0')
    return;
  table[y][x]--;
  for (int r = 0; r < 4; r++) {
    dfs(y + fdy[r], x + fdx[r]);
  }
  return;
}

int main(void) {
  while (true) {
    for (int r = 0; r < MAX_L; r++) {
      if (scanf("%s", table[r]) == EOF)
        return (0);
    }
    int cnt = 0;
    for (int r = 0; r < MAX_L; r++) {
      for (int c = 0; c < MAX_L; c++) {
        if (table[r][c] == '1') {
          dfs(r, c);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return (0);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#define MAX_L 12
using namespace std;
char table[MAX_L][MAX_L];
int fdx[] = {1, 0, -1, 0}, fdy[] = {0, 1, 0, -1};

void dfs(int y, int x) {
  if (x < 0 || x >= MAX_L || y < 0 || y >= MAX_L || table[y][x] == '0')
    return;
  table[y][x]--;
  for (int r = 0; r < 4; r++) {
    dfs(y + fdy[r], x + fdx[r]);
  }
  return;
}

int main(void) {
  while (true) {
    for (int r = 0; r < MAX_L; r++) {
      scanf("%s", table[r]);
    }
    int cnt = 0;
    for (int r = 0; r < MAX_L; r++) {
      for (int c = 0; c < MAX_L; c++) {
        if (table[r][c] == '1') {
          dfs(r, c);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return (0);
}