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

#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int ans;

void solve(char a[][15], int x, int y) {
  a[x][y] = '0';
  if (a[x + 1][y] == '1')
    solve(a, x + 1, y);
  if (a[x - 1][y] == '1')
    solve(a, x - 1, y);
  if (a[x][y + 1] == '1')
    solve(a, x, y + 1);
  if (a[x][y - 1] == '1')
    solve(a, x, y - 1);
}

int main() {
  string inp;
  char map[15][15] = {0};
  while (scanf("%s", map[0]) == 1) {
    ans = 0;
    range(i, 1, 12) { scanf("%s", map[i]); }
    rep(i, 12) {
      rep(j, 12) {
        if (map[i][j] == '1') {
          ans++;
          solve(map, i, j);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int ans;

void solve(char a[][15], int x, int y) {
  a[x][y] = '0';
  if (a[x + 1][y] == '1')
    solve(a, x + 1, y);
  if (a[x - 1][y] == '1')
    solve(a, x - 1, y);
  if (a[x][y + 1] == '1')
    solve(a, x, y + 1);
  if (a[x][y - 1] == '1')
    solve(a, x, y - 1);
}

int main() {
  string inp;
  char map[15][15] = {0};
  while (true) {
    ans = 0;
    rep(i, 12) { scanf("%s", map[i]); }
    rep(i, 12) {
      rep(j, 12) {
        if (map[i][j] == '1') {
          ans++;
          solve(map, i, j);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

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

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

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

vector<string> mp(12);
bool visited[12][12];

int bfs(int x, int y) {
  queue<P> que;
  que.push(P{x, y});

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (nx < 0 || ny < 0 || 12 <= nx || 12 <= ny)
        continue;
      if (visited[ny][nx] || mp[ny][nx] == '0')
        continue;

      visited[ny][nx] = true;

      que.push(P{nx, ny});
    }
  }

  return 1;
}

int main() {
  while (cin >> mp[0]) {
    rep(i, 11) cin >> mp[i + 1];
    rep(i, 12) rep(j, 12) visited[i][j] = false;

    int ans = 0;

    rep(y, 12) rep(x, 12) {
      if (visited[y][x] || mp[y][x] == '0')
        continue;

      ans += bfs(x, y);
    }

    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;
using ll = long long int;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using edge = struct {
  int to, cost;
};

const int MOD = 1e9 + 7;
const int iINF = 1e9;
const long long int llINF = 1e18;
const double PI = acos(-1.0);

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

vector<string> mp(12);
bool visited[12][12];

int bfs(int x, int y) {
  queue<P> que;
  que.push(P{x, y});

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (nx < 0 || ny < 0 || 12 <= nx || 12 <= ny)
        continue;
      if (visited[ny][nx] || mp[ny][nx] == '0')
        continue;

      visited[ny][nx] = true;

      que.push(P{nx, ny});
    }
  }

  return 1;
}

int main() {
  for (;;) {
    rep(i, 12) cin >> mp[i];
    rep(i, 12) rep(j, 12) visited[i][j] = false;

    int ans = 0;

    rep(y, 12) rep(x, 12) {
      if (visited[y][x] || mp[y][x] == '0')
        continue;

      ans += bfs(x, y);
    }

    cout << ans << endl;
  }

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

#include <stdio.h>
bool setMap();
void search(int x, int y);
int map[12][12];
int w = 12;
int h = 12;

int main(void) {
  while (setMap()) {
  }
}

bool setMap() {
  char t;
  int count = 0;
  int r;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {

      r = scanf(" %c ", &t);
      if (r < 1) {
        return false;
      }

      map[i][j] = t - '0';
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (map[i][j] == 1) {
        search(j, i);
        count++;
      }
    }
  }
  printf("%d\\n", count);
  return true;
}

void search(int x, int y) {
  if (w > x && x >= 0 && h > y && y >= 0 && map[y][x] == 1) {
    map[y][x] = 0;
    search(x + 1, y);
    search(x - 1, y);
    search(x, y + 1);
    search(x, y - 1);
  }
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>

typedef struct {
  int color;
  int number;
} Kind;

Kind U[1000000];

int main() {
  int i, j, a, c, b;

  while (~scanf("%d", &a)) {
    if (a == 0)
      break;

    memset(U, 0, sizeof(U));
    b = a;
    for (i = 0; i < a; i++) {
      scanf("%d", &c);
      for (j = 0; j < b; j++) {
        if (U[j].color == c || U[j].color == 0) {
          U[j].color = U[j].color > 0 ? U[j].color : c;
          ++U[j].number;
          break;
        }
      }
      b = j + 1;
    }

    j = -1;
    for (i = 0; i < a; i++) {
      if (2 * U[i].number > a) {
        j = i;
        break;
      }
    }

    if (j == -1) {
      printf("NO COLOR\\n");
    } else {
      printf("%d\\n", U[j].color);
    }
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int a[12][12] = {0};
void remove(int y, int x) {
  if (y > 11 || x > 11 || y < 0 || x < 0 || a[y][x] == 0)
    return;
  a[y][x] = 0;
  remove(y + 1, x);
  remove(y - 1, x);
  remove(y, x + 1);
  remove(y, x - 1);
}

int main() {
  string s;
  do {
    for (int i = 0; i < 12; i++) {
      getline(cin, s);
      for (int j = 0; j < 12; j++) {
        a[i][j] = s[j] - '0';
      }
    }

    int count = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (a[i][j] == 1) {
          remove(i, j);
          count++;
        }
      }
    }
    cout << count << endl;
    getline(cin, s);
  } while (cin);
  return 0;
}

Error: TLE

Fixed submission:

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

int a[12][12] = {0};
void remove(int y, int x) {
  a[y][x] = 0;
  if (y + 1 < 12 && a[y + 1][x] == 1)
    remove(y + 1, x);
  if (y - 1 >= 0 && a[y - 1][x] == 1)
    remove(y - 1, x);
  if (x + 1 < 12 && a[y][x + 1] == 1)
    remove(y, x + 1);
  if (x - 1 >= 0 && a[y][x - 1] == 1)
    remove(y, x - 1);
}

int main() {
  string s;
  while (true) {
    for (int i = 0; i < 12; i++) {
      cin >> s;
      for (int j = 0; j < 12; j++) {
        a[i][j] = s[j] - '0';
      }
    }

    int count = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (a[i][j] == 1) {
          remove(i, j);
          count++;
        }
      }
    }
    cout << count << endl;
    getline(cin, s);
    if (s.size() != 0)
      break;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <sstream>
#include <string>
using namespace std;
int data[12][12];
vector<string> s(12);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

void dfs(int x, int y) {
  s[y][x] = '0';
  for (int i = 0; i < 4; ++i) {
    int px = x + dx[i];
    int py = y + dy[i];
    if (px < 0 || px >= 12 || py < 0 || py >= 12)
      continue;
    if (s[py][px] == '0')
      continue;
    dfs(px, py);
  }
}

int main() {
  while (1) {
    int ans = 0;

    for (int i = 0; i < 12; ++i) {
      cin >> s[i];
      if (cin.eof())
        return 0;
    }
    for (int i = 0; i < 12; ++i) {
      for (int j = 0; j < 12; ++j) {
        if (s[i][j] == '0')
          continue;
        dfs(j, i);
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <sstream>
#include <string>
using namespace std;
int data[12][12];
vector<string> s(12);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

void dfs(int x, int y) {
  s[y][x] = '0';
  for (int i = 0; i < 4; ++i) {
    int px = x + dx[i];
    int py = y + dy[i];
    if (px < 0 || px >= 12 || py < 0 || py >= 12)
      continue;
    if (s[py][px] == '0')
      continue;
    dfs(px, py);
  }
}

int main() {
  while (1) {
    int ans = 0;

    for (int i = 0; i < 12; ++i) {
      cin >> s[i];
      if (cin.eof())
        break;
    }
    for (int i = 0; i < 12; ++i) {
      for (int j = 0; j < 12; ++j) {
        if (s[i][j] == '0')
          continue;
        dfs(j, i);
        ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int a;
string s[12];
void dfs(int y, int x) {
  s[y][x] = '0';
  if (y + 1 < 12 && s[y + 1][x] == '1')
    dfs(y + 1, x);
  if (y - 1 >= 0 && s[y - 1][x] == '1')
    dfs(y - 1, x);
  if (x + 1 < 12 && s[y][x + 1] == '1')
    dfs(y, x + 1);
  if (x - 1 >= 0 && s[y][x - 1] == '1')
    dfs(y, x - 1);
}
int main() {
  while (cin >> s[0]) {
    a = 0;
    r(i, 11) cin >> s[i + 1];
    r(i, 12) r(j, 12) if (s[i][j] == '1') {
      dfs(i, j);
      a++;
    }
    cout << a << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int a;
string s[13];
void dfs(int y, int x) {
  s[y][x] = '0';
  if (s[y + 1][x] == '1')
    dfs(y + 1, x);
  if (s[y - 1][x] == '1')
    dfs(y - 1, x);
  if (s[y][x + 1] == '1')
    dfs(y, x + 1);
  if (s[y][x - 1] == '1')
    dfs(y, x - 1);
}
int main() {
  while (cin >> s[0]) {
    a = 0;
    r(i, 11) cin >> s[i + 1];
    r(i, 12) r(j, 12) if (s[i][j] == '1') {
      dfs(i, j);
      a++;
    }
    cout << a << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;

int bfs(int stage[12][12]) {

  int count = 0;
  for (int y = 0; y < 12; y++) {
    for (int x = 0; x < 12; x++) {
      if (stage[y][x] == 0)
        continue;
      count++;
      queue<P> que;
      que.push(P(x, y));

      while (!que.empty()) {
        int x = que.front().first;
        int y = que.front().second;

        que.pop();

        stage[y][x] = 0;

        for (int i = -1; i <= 1; i++) {
          for (int j = -1; j <= 1; j++) {
            if (i * j != 0 || (i == j && i == 0))
              continue;
            int dx = x + i;
            int dy = y + j;

            if (dx < 0 || dx >= 12 || dy < 0 || dy >= 12)
              continue;
            if (stage[dy][dx] == 0)
              continue;
            que.push(P(dx, dy));
          }
        }
      }
    }
  }
  return count;
}

int main() {
  string str;

  int stage[12][12];

  while (getline(cin, str)) {
    if (str.empty())
      continue;

    for (int y = 0; y < 12; y++) {
      for (int x = 0; x < 12; x++) {
        stage[y][x] = str[x] - '0';
      }
      getline(cin, str);
    }

    cout << bfs(stage) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int ans = 0, i;
char t[15][15] = {0};
int x, y, zx[4] = {0, 0, 1, -1}, zy[4] = {1, -1, 0, 0};
int sp(int n, int m) {
  t[n][m] = '0';
  if (t[n][m + 1] == '0' && t[n + 1][m] == '0' && t[n - 1][m] == '0' &&
      t[n][m - 1] == '0')
    return 0;
  else {
    for (int i = 0; i < 4; i++) {
      int nx = n + zx[i];
      int ny = m + zy[i];
      if (t[nx][ny] == '1') {
        t[nx][ny] = '0';
        sp(nx, ny);
      }
    }
  }
  return 0;
}
int main() {
  while (1) {
    ans = 0;
    for (int i = 0; i < 12; i++) {
      cin >> t[i];
    }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (t[i][j] == '1') {
          ans++;
          sp(i, j);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool isLand[12][12];

bool deleteOneLand(int x, int y) {
  if (!isLand[y][x])
    return false;
  isLand[y][x] = false;
  if (x > 0)
    deleteOneLand(x - 1, y);
  if (y > 0)
    deleteOneLand(x, y - 1);
  if (x < 11)
    deleteOneLand(x + 1, y);
  if (y < 11)
    deleteOneLand(x, y + 1);
  return true;
}

int main() {
  int x, y, ans;
  while (!cin.eof()) {
    ans = 0;
    for (y = 0; y < 12; y++) {
      for (x = 0; x < 12; x++)
        isLand[y][x] = getchar() == '1';
      getchar();
    }
    for (y = 0; y < 12; y++)
      for (x = 0; x < 12; x++)
        if (deleteOneLand(x, y))
          ans++;
    cout << ans << endl;
    if (getchar() != '\\n')
      break;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool isLand[12][12];

bool deleteOneLand(int x, int y) {
  if (!isLand[y][x])
    return false;
  isLand[y][x] = false;
  if (x > 0)
    deleteOneLand(x - 1, y);
  if (y > 0)
    deleteOneLand(x, y - 1);
  if (x < 11)
    deleteOneLand(x + 1, y);
  if (y < 11)
    deleteOneLand(x, y + 1);
  return true;
}

int main() {
  int x, y, ans;
  while (!cin.eof()) {
    ans = 0;
    for (y = 0; y < 12; y++) {
      for (x = 0; x < 12; x++)
        isLand[y][x] = getchar() == '1';
      getchar();
    }
    for (y = 0; y < 12; y++)
      for (x = 0; x < 12; x++)
        if (deleteOneLand(x, y))
          ans++;
    cout << ans << endl;
    getchar();
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

char map[16][16];

void island(int x, int y) {
  int v[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  map[x][y] = '0';
  for (int i = 0; i < 4; i++) {
    int xx = x + v[i][0], yy = y + v[i][1];
    if (0 <= xx && xx < 12 && 0 <= yy && yy < 12 && map[xx][yy] == '1') {
      island(xx, yy);
    }
  }
}

int main() {
  while (true) {
    if (scanf("%s", map[0]) == EOF)
      break;
    for (int i = 1; i < 12; i++) {
      scanf("%s", map[i]);
    }
    int count = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (map[i][j] == '1') {
          count++;
          island(i, j);
        }
      }
    }
    printf("%d\\n", count);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

char map[16][16];

void island(int x, int y) {
  int v[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  map[x][y] = '0';
  for (int i = 0; i < 4; i++) {
    int xx = x + v[i][0], yy = y + v[i][1];
    if (0 <= xx && xx < 12 && 0 <= yy && yy < 12 && map[xx][yy] == '1') {
      island(xx, yy);
    }
  }
}

int main() {
  char dummy[4];
  while (true) {
    for (int i = 0; i < 12; i++) {
      fgets(map[i], 16, stdin);
    }
    int count = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (map[i][j] == '1') {
          count++;
          island(i, j);
        }
      }
    }
    printf("%d\\n", count);
    if (map[11][12] != '\\n')
      break;
    fgets(dummy, 4, stdin);
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

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

queue<int> Q;
int z[100][100], x[100][100], sum;
char a;

void BFS(int col, int x1, int y1) {
  Q.push(x1 * 100 + y1);
  x[x1][y1] = col;
  int a, b;
  while (!Q.empty()) {
    a = Q.front() / 100;
    b = Q.front() % 100;
    if (z[a + 1][b] == 1 && x[a + 1][b] == 0) {
      Q.push(Q.front() + 100);
      x[a + 1][b] = col;
    }
    if (z[a - 1][b] == 1 && x[a - 1][b] == 0) {
      Q.push(Q.front() - 100);
      x[a - 1][b] = col;
    }
    if (z[a][b + 1] == 1 && x[a][b + 1] == 0) {
      Q.push(Q.front() + 1);
      x[a][b + 1] = col;
    }
    if (z[a][b - 1] == 1 && x[a][b - 1] == 0) {
      Q.push(Q.front() - 1);
      x[a][b - 1] = col;
    }
    Q.pop();
  }
}

int main() {
  while (cin >> a) {
    if (a == '1') {
      z[1][1] = 1;
    }
    sum = 0;
    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (i + j != 2) {
          cin >> a;
          if (a == '1') {
            z[i][j] = 1;
          }
        }
      }
    }
    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (x[i][j] == 0 && z[i][j] == 1) {
          sum++;
          BFS(sum, i, j);
        }
      }
    }
    cout << sum << endl;
    memset(x, 0, sizeof(x));
    memset(z, 0, sizeof(z));
  }
}

Error: WA

Fixed submission:

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

queue<int> Q;
int z[100][100], x[100][100], sum;
char a;

void BFS(int col, int x1, int y1) {
  Q.push(x1 * 100 + y1);
  int a, b;
  while (!Q.empty()) {
    a = Q.front() / 100;
    b = Q.front() % 100;
    if (z[a + 1][b] == 0 && x[a + 1][b] == 0) {
      Q.push(Q.front() + 100);
      x[a + 1][b] = col;
    }
    if (z[a - 1][b] == 0 && x[a - 1][b] == 0) {
      Q.push(Q.front() - 100);
      x[a - 1][b] = col;
    }
    if (z[a][b + 1] == 0 && x[a][b + 1] == 0) {
      Q.push(Q.front() + 1);
      x[a][b + 1] = col;
    }
    if (z[a][b - 1] == 0 && x[a][b - 1] == 0) {
      Q.push(Q.front() + 1);
      x[a][b - 1] = col;
    }
    Q.pop();
  }
}

int main() {
  while (cin >> a) {
    if (a == '1') {
      z[1][1] = 1;
    }
    sum = 0;
    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (i + j != 2) {
          cin >> a;
          if (a == '1') {
            z[i][j] = 1;
          }
        }
      }
    }
    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (x[i][j] == 0) {
          sum++;
          BFS(sum, i, j);
        }
      }
    }
    cout << sum << endl;
    memset(x, 0, sizeof(x));
  }
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
vector<string> table(12);
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void dfs(int x, int y) {
  table[y][x] = '0';
  for (int i = 0; i < 4; i++) {
    int px = x + dx[i];
    int py = y + dy[i];
    if (px < 0 || px >= 12 || py < 0 || py >= 12)
      continue;
    if (table[py][px] == '0')
      continue;
    dfs(px, py);
  }
}

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


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};
int table[12][12];
int cnt = 0;

void dfs(int x, int y) {
  // cout << y << " " << x << endl;
  table[x][y] == 0;
  rep(i, 4) {
    int px = x + dx[i];
    int py = y + dy[i];
    if (px < 0 || px >= 12 || py < 0 || py >= 12)
      continue;
    if (table[py][px] == 0)
      continue;
    dfs(px, py);
  }
}
int main() {
  string str;
  while (1) {
    rep(i, 12) {
      cin >> str;
      if (cin.eof())
        return 0;
      rep(j, str.size()) { table[i][j] = str[j] - '0'; }
    }
    rep(i, 12) {
      rep(j, 12) {
        if (table[i][j] == 0)
          continue;
        dfs(j, i);
        cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

char arr[12][12];

void dfs(int i, int j) {
  arr[i][j] = '0';
  // cout<<i<<" "<<j<<endl;
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  for (int s = 0; s < 4; s++) {
    if ((i + dx[s]) >= 0 && (i + dx[s]) < 12 && (j + dy[s]) >= 0 &&
        (j + dy[s]) < 12 && arr[i + dx[s]][j + dy[s]] == '1') {
      int nx = i + dx[s];
      int ny = j + dy[s];
      dfs(nx, ny);
    }
  }
  return;
}

int main() {
  string s;
  bool flag = false;
  while (cin >> s) {
    int count = 0;
    for (int i = 0; i < 12; i++)
      arr[0][i] = s[i];
    for (int i = 1; i < 12; i++) {
      cin >> s;
      for (int j = 0; j < 12; j++)
        arr[i][j] = s[j];
    }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (arr[i][j] == '1') {
          // cout<<"start"<<i<<" "<<j<<endl;
          dfs(i, j);
          count++;
        }
      }
    }
    cout << count << endl;
    getline(cin, s);
  }
  // while(1);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

char arr[12][12];

void dfs(int i, int j) {
  arr[i][j] = '0';
  // cout<<i<<" "<<j<<endl;
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  for (int s = 0; s < 4; s++) {
    if ((i + dx[s]) >= 0 && (i + dx[s]) < 12 && (j + dy[s]) >= 0 &&
        (j + dy[s]) < 12 && arr[i + dx[s]][j + dy[s]] == '1') {
      int nx = i + dx[s];
      int ny = j + dy[s];
      dfs(nx, ny);
    }
  }
  return;
}

int main() {
  string s;
  while (1) {
    int count = 0;
    for (int i = 0; i < 12; i++) {
      cin >> s;
      for (int j = 0; j < 12; j++)
        arr[i][j] = s[j];
    }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (arr[i][j] == '1') {
          // cout<<"start"<<i<<" "<<j<<endl;
          dfs(i, j);
          count++;
        }
      }
    }
    cout << count << endl;
    getline(cin, s);
  }
  // while(1);
  return 0;
}