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

#include <iostream>
#include <string>

using namespace std;

bool field[12][12];

void dfs(int x, int y) {
  field[x][y] = false;
  if (x != 11 && field[x + 1][y])
    dfs(x + 1, y);
  if (x != 0 && field[x - 1][y])
    dfs(x - 1, y);
  if (y != 11 && field[x][y + 1])
    dfs(x, y + 1);
  if (y != 0 && field[x][y - 1])
    dfs(x, y - 1);
}

int main(void) {
  string str;
  int count;
  while (1) {
    count = 0;
    for (int i = 0; i < 12; ++i) {
      cin >> str;
      for (int j = 0; j < 12; ++j) {
        field[j][i] = (str[j] - '0' == 1) ? true : false;
      }
    }
    if (cin.eof())
      break;

    for (int i = 0; i < 12; ++i) {
      for (int j = 0; j < 12; ++j) {
        if (field[j][i]) {
          dfs(j, i);
          ++count;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

bool inrange(int x, int y) {
  if (x < 0 || y < 0 || x > 11 || y > 11)
    return false;
  else
    return true;
}

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

void search(int x, int y) {
  REP(i, 4) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (!inrange(nx, ny))
      continue;
    if (data[nx][ny] == 1) {
      data[nx][ny] = 0;
      search(nx, ny);
    }
  }
}

int main() {
  while (1) {
    string str;
    REP(i, 12) {
      if (!(cin >> str))
        break;
      REP(j, 12) { data[i][j] = str[j] - '0'; }
    }
    ll int cnt = 0;
    REP(i, 12) {
      REP(j, 12) {
        if (data[i][j] == 1) {
          search(i, j);
          cnt++;
        }
      }
    }
    cout << cnt << endl;

    // if(cin.eof()) break;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
vector<vector<char>> in(14, vector<char>(14));
int x[4] = {0, 0, 1, -1};
int y[4] = {1, -1, 0, 0};
void func(int a, int b) {
  in[a][b] = 0;
  rep(k, 4) if (in[a + x[k]][b + y[k]] != 0) func(a + x[k], b + y[k]);
}

int main() {
  char d;
  while (cin >> d) {
    rep(i, 12) rep(j, 12) {
      if (!i && !j)
        in[i + 1][j + 1] = d;
      else
        cin >> in[i + 1][j + 1];
      if (in[i + 1][j + 1] == '0')
        in[i + 1][j + 1] = 0;
      else
        in[i + 1][j + 1] = 1;
    }
    int out = 0;
    rep(i, 12) rep(j, 12) if (in[i + 1][j + 1] != 0) {
      out++;
      func(i + 1, j + 1);
    }
    cout << out << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
vector<vector<char>> in(14, vector<char>(14));
int x[4] = {0, 0, 1, -1};
int y[4] = {1, -1, 0, 0};
void func(int a, int b) {
  in[a][b] = 0;
  rep(k, 4) if (in[a + x[k]][b + y[k]] == 1) func(a + x[k], b + y[k]);
}

int main() {
  char d;
  while (cin >> d) {
    rep(i, 12) rep(j, 12) {
      if (!i && !j)
        in[i][j] = d;
      else
        cin >> in[i][j];
      if (in[i][j] == '0')
        in[i][j] = 0;
      else
        in[i][j] = 1;
    }
    int out = 0;
    rep(i, 12) rep(j, 12) if (in[i][j] == 1) {
      out++;
      func(i, j);
    }
    cout << out << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;

char tmap[12][12];
int c;

void Terrain(int i, int l) {

  if (tmap[i][l] == '0' || i < 0 || i > 11 || l < 0 || l > 11)
    return;

  tmap[i][l] = '0';

  Terrain(i + 1, l);
  Terrain(i - 1, l);
  Terrain(i, l + 1);
  Terrain(i, l - 1);

  return;
}
int main() {
  int cunt = 0;
  while (cin >> tmap[0]) {
    for (int i = 1; i < 12; i++)
      cin >> tmap[i];
    for (int i = 0; i < 12; i++) {
      for (int l = 0; l < 12; l++) {
        if (tmap[i][l] == '1') {
          Terrain(i, l);
          cunt++;
        }
      }
    }
    cout << cunt << endl;
    cunt = 0;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;

char tmap[16][16];

void Terrain(int i, int l) {

  if (i < 0 || i > 11 || l < 0 || i > 11 || tmap[i][l] == '0') {
    return;
  }

  tmap[i][l] = '0';

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);
char w[14][14] = {};
LL ans = 0;
void dfs(LL x, LL y) {
  w[x][y] = '0';
  if (x < 11)
    if (w[x + 1][y] == '1')
      dfs(x + 1, y);
  if (y < 11)
    if (w[x][y + 1] == '1')
      dfs(x, y + 1);
  if (x > 0)
    if (w[x - 1][y] == '1')
      dfs(x - 1, y);
  if (y > 0)
    if (w[x][y - 1] == '1')
      dfs(x, y - 1);
}
int main() {
  while (cin >> w[0][0]) {
    ans = 0;
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++) {
        if (!(i == 0 && j == 0))
          cin >> w[i][j];
      }
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++) {
        if (w[i][j] == '1') {
          ans++;
          dfs(i, j);
        }
      }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);
char w[14][14] = {};
LL ans = 0;
void dfs(LL x, LL y) {
  w[x][y] = '0';
  if (x < 11)
    if (w[x + 1][y] == '1')
      dfs(x + 1, y);
  if (y < 11)
    if (w[x][y + 1] == '1')
      dfs(x, y + 1);
  if (x > 0)
    if (w[x - 1][y] == '1')
      dfs(x - 1, y);
  if (y > 0)
    if (w[x][y - 1] == '1')
      dfs(x, y - 1);
}
int main() {
  while (1) {
    ans = 0;
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++) {
        cin >> w[i][j];
      }
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++) {
        if (w[i][j] == '1') {
          ans++;
          dfs(i, j);
        }
      }

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

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

vector<string> v(12);
bool used[12][12];
void dfs(int i, int j) {
  if (i < 0 || i > 11 || j < 0 || j > 11)
    return;
  if (!used[i][j] && v[i][j] == '1') {
    used[i][j] = true;
    dfs(i + 1, j);
    dfs(i, j + 1);
    dfs(i - 1, j);
    dfs(i, j - 1);
  }
  return;
}

int main() {
  while (cin >> v[0]) {
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++)
        used[i][j] = false;

    for (int i = 1; i < 12; i++) {
      cin >> v[i];
    }
    int cnt = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (!used[i][j] && v[i][j] == '1') {
          cnt++;
          dfs(i, j);
        }
      }
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

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

vector<string> v(12);
bool used[12][12];
void dfs(int i, int j) {
  if (i < 0 || i > 11 || j < 0 || j > 11)
    return;
  if (!used[i][j] && v[i][j] == '1') {
    used[i][j] = true;
    dfs(i + 1, j);
    dfs(i, j + 1);
    dfs(i - 1, j);
    dfs(i, j - 1);
  }
  return;
}

int main() {
  while (true) {
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++)
        used[i][j] = false;

    for (int i = 0; i < 12; i++)
      cin >> v[i];
    int cnt = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (!used[i][j] && v[i][j] == '1') {
          cnt++;
          dfs(i, j);
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <complex>
#include <list>
#include <stack>
#include <vector>
using namespace std;

char m[12][12];
int w[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void sol(int i, int j) {
  int ni, nj;
  m[i][j] = '0';
  for (int k = 0; k < 4; ++k) {
    ni = i + w[k][0];
    nj = j + w[k][1];
    if (ni >= 0 && ni <= 11 && nj >= 0 && nj <= 11) {
      if (m[ni][nj] == '1')
        sol(ni, nj);
    }
  }
  return;
}

int main() {
  int ans;
  while (scanf("%s", m[0]) != EOF) {
    ans = 0;
    for (int i = 1; i < 12; ++i) {
      scanf("%s", m[i]);
    }

    for (int i = 0; i < 12; ++i) {
      for (int j = 0; j < 12; ++j) {
        if (m[i][j] == '1') {
          sol(i, j);
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <complex>
#include <list>
#include <stack>
#include <vector>
using namespace std;

char m[14][14] = {0};
int w[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

void sol(int i, int j) {
  int ni, nj;
  m[i][j] == '0';
  for (int k = 0; k < 4; ++k) {
    ni = i + w[k][0];
    nj = j + w[k][1];
    if (m[ni][nj] == '1')
      sol(ni, nj);
  }
}

int main() {
  int ans;
  for (;;) {
    ans = 0;
    for (int i = 1; i <= 12; ++i) {
      if (scanf("%s", &m[i][1]) == EOF)
        return 0;
    }

    for (int i = 1; i <= 12; ++i) {
      for (int j = 1; j <= 12; ++j) {
        if (m[i][j] == '1') {
          sol(i, j);
          ans++;
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

char nyuryoku[12][12];

void fun(int y, int x, char masume) {
  if ((x < 0) || (12 <= x) || (y < 0) || (12 <= y)) {
    return;
  }
  if (masume != nyuryoku[y][x])
    return;
  else
    nyuryoku[y][x] = '+';
  fun(y + 1, x, masume);
  fun(y - 1, x, masume);
  fun(y, x + 1, masume);
  fun(y, x - 1, masume);
}

int main() {
  while (cin >> nyuryoku[0][0]) {
    for (int j = 1; j < 12; j++)
      cin >> nyuryoku[0][j];
    for (int i = 1; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        cin >> nyuryoku[i][j];
      }
    }
    int count = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (nyuryoku[i][j] != '+') {
          if (nyuryoku[i][j] == '1')
            count++;
          fun(i, j, nyuryoku[i][j]);
        }
      }
    }
    cout << count << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

char nyuryoku[12][12];

void fun(int y, int x, char masume) {
  if ((x < 0) || (12 <= x) || (y < 0) || (12 <= y)) {
    return;
  }
  if (masume != nyuryoku[y][x])
    return;
  else
    nyuryoku[y][x] = '+';
  fun(y + 1, x, masume);
  fun(y - 1, x, masume);
  fun(y, x + 1, masume);
  fun(y, x - 1, masume);
}

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

#define _CRT_SECURE_NO_WARNINGS

#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <string>

using namespace std;

queue<int> q;
list<int> l;
int *map = new int[12 * 12 * 20];
static int count;

void loop(int i) {
  map[i] = 0;
  if (i / 12 > 0) {
    if (map[i - 12] == 1) {
      loop(i - 12);
    }
  }
  if (i / 12 < 11) {
    if (map[i + 12] == 1) {
      loop(i + 12);
    }
  }
  if (i % 12 > 0) {
    if (map[i - 1] == 1) {
      loop(i - 1);
    }
  }
  if (i % 12 < 11) {
    if (map[i + 1] == 1) {
      loop(i + 1);
    }
  }
}

int main(int argc, char **argv) {
  int count = 0;
  string str;

  while (cin >> str) {
    for (int j = 0; j < 12; j++) {
      map[j] = str[j] - '0';
    }
    for (int i = 1; i < 12 && cin >> str; i++) {
      for (int j = 0; j < 12; j++) {
        map[i * 12 + j] = str[j] - '0';
      }
    }
    count = 0;
    for (int i = 0; i < 12 * 12; i++) {
      if (map[i] == 1) {
        loop(i);
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stack>
#include <string>

using namespace std;

queue<int> q;
list<int> l;
int *map = new int[12 * 12];
static int count;

void loop(int i, int j) {
  map[i * 12 + j] = 0;
  if (i > 0) {
    if (map[(i - 1) * 12 + j] == 1) {
      l.push_front((i - 1) * 12 + j);
    }
  }
  if (i < 11) {
    if (map[(i + 1) * 12 + j] == 1) {
      l.push_front((i + 1) * 12 + j);
    }
  }
  if (j > 0) {
    if (map[i * 12 + j - 1] == 1) {
      l.push_front(i * 12 + j - 1);
    }
  }
  if (j < 11) {
    if (map[i * 12 + j + 1] == 1) {
      l.push_front(i * 12 + j + 1);
    }
  }
  l.unique();
  if (l.empty()) {
    return;
  }
  int front = l.front();
  l.pop_front();
  loop(front / 12, front % 12);
}

int main(int argc, char **argv) {
  while (true) {
    int count = 0;
    string str;
    for (int i = 0; i < 12 && cin >> str; i++) {
      for (int j = 0; j < 12; j++) {
        map[i * 12 + j] = str[j] - '0';
      }
    }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (map[i * 12 + j] == 1) {
          l.push_front(i * 12 + j);
          loop(i, j);
          count++;
        }
      }
    }
    cout << count << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
string map[20] = {""};
void check(int x, int y);
int main(void) {
  while (1) {
    int ans = 0;
    for (int i = 0; i < 12; i++) {
      cin >> map[i];
      if (cin.eof())
        goto end;
    }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (map[i][j] == '1') {
          check(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
end:;
  return 0;
}
void check(int x, int y) {
  map[x][y] = '0';
  if (x != 11 && map[x + 1][y] == '1')
    check(x + 1, y);
  if (y != 11 && map[x][y + 1] == '1')
    check(x, y + 1);
  if (x != 0 && map[x - 1][y] == '1')
    check(x - 1, y);
  if (y != 0 && map[x][y - 1] == '1')
    check(x, y - 1);
  return;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool a[100][100] = {{0}};
bool b[100][100] = {{0}};
bool f[100][100] = {{0}};
int cnt = 0, ans = 0;
int p = 0, q = 0;
string map[20];
string bas[20];
void check(int x, int y);
int main(void) {
  while (1) {
    for (int i = 1; i <= 12; i++) {
      cin >> map[i];
      if (cin.eof())
        goto end;
    }
    ans = 0;
    for (int i = 1; i <= 12; i++) {
      bas[i] = map[i];
    }

    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (map[i][j] == '1') {
          check(i, j);
          ans++;
        }
        for (int i = 1; i <= 12; i++) {
          map[i] = bas[i];
        }
      }
    }
    cout << ans << endl;
  }
end:;
  return 0;
}
void check(int x, int y) {
  map[x][y] = '0';
  // p++,cout<<p<<"+"<<x<<","<<y<<endl;
  cnt++;
  int flag = 0;
  if (map[x + 1][y] == '1') {
    flag = 1;
    check(x + 1, y);
    map[x + 1][y] = '1';
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }

  if (map[x][y + 1] == '1') {
    flag = 1;
    check(x, y + 1);
    map[x][y + 1] = '1';
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }
  if (map[x - 1][y] == '1') {
    flag = 1;
    check(x - 1, y);
    map[x - 1][y] = '1';
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }

  if (map[x][y - 1] == '1') {
    flag = 1;
    check(x, y - 1);
    map[x][y - 1] = '1';
    cnt--;
    // p--,cout<<"-"<<x<<","<<y<<endl;
  }
  bas[x][y] = '0';

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

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

string f[15];
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

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

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

Error: TLE

Fixed submission:

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

string f[15];
int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

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

int main() {
  while (1) {
    int ans = 0;
    for (int i = 0; i < 12; i++) {
      cin >> f[i];
    }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (f[i][j] == '1') {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
int field[12 + 2][12 + 2] = {0};

void island(int x, int y) {
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, -1, 0, 1};
  field[x][y] = 0;
  for (int i = 0; i < 4; i++) {
    if ((field[x + dx[i]][y + dy[i]] == 1) && (1 <= x) && (x <= 12) &&
        (1 <= y) && (y <= 12)) {
      island(x + dx[i], y + dy[i]);
    }
  }
}

int main() {
  string str;
  int ans = 0;
  while (getline(cin, str)) {
    if (str.empty())
      break;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        field[i + 1][j + 1] = atoi(str.substr(j, 1).c_str());
      }
      getline(cin, str);
    }

    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (field[i + 1][j + 1] == 1) {
          island(i + 1, j + 1);
          ans++;
        }
      }
    }
    cout << ans << endl;
    ans = 0;
    // int field[12+2][12+2] ={0};
    memset(field, 0, sizeof(field));
    /*     getline(cin,str);//erase \\n
           if(cin.eof()) break;*/
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
int field[12 + 2][12 + 2] = {0};

void island(int x, int y) {
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, -1, 0, 1};
  field[x][y] = 0;
  for (int i = 0; i < 4; i++) {
    if ((field[x + dx[i]][y + dy[i]] == 1) && (1 <= x) && (x <= 12) &&
        (1 <= y) && (y <= 12)) {
      island(x + dx[i], y + dy[i]);
    }
  }
}

int main() {
  string str;
  int ans = 0;
  while (1) {
    for (int i = 0; i < 12; i++) {
      getline(cin, str);
      for (int j = 0; j < 12; j++) {
        field[i + 1][j + 1] = atoi(str.substr(j, 1).c_str());
      }
    }

    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (field[i + 1][j + 1] == 1) {
          island(i + 1, j + 1);
          ans++;
        }
      }
    }
    cout << ans << endl;
    ans = 0;
    // int field[12+2][12+2] ={0};
    memset(field, 0, sizeof(field));
    getline(cin, str); // erase \\n
    if (cin.eof())
      break;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;
char a[12][12];
void irand(int i, int j) {
  a[i][j] = '0';
  if (i > 0 && a[i - 1][j] == '1')
    irand(i - 1, j);
  if (j > 0 && a[i][j - 1] == '1')
    irand(i, j - 1);
  if (i < 11 && a[i + 1][j] == '1')
    irand(i + 1, j);
  if (j < 11 && a[i][j + 1] == '1')
    irand(i, j + 1);
  return;
}
int main() {
  while (scanf("%s", a[0]) != EOF) {
    int cnt = 0;
    for (int i = 1; i < 12; i++)
      scanf("%s", a[i]);
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (a[i][j] == '1') {
          cnt++;
          irand(i, j);
        }
      }
    }
    printf("%d\\n", cnt);
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
char a[12][12];
void irand(int i, int j) {
  a[i][j] == '0';
  if (i > 0 && a[i - 1][j] == '1')
    irand(i - 1, j);
  if (j > 0 && a[i][j - 1] == '1')
    irand(i, j - 1);
  if (i < 11 && a[i + 1][j] == '1')
    irand(i + 1, j);
  if (j < 11 && a[i][j + 1] == '1')
    irand(i, j + 1);
  return;
}
int main() {
  while (scanf("%s", a[0]) != EOF) {
    int cnt = 0;
    for (int i = 1; i < 12; i++)
      scanf("%s", a[i]);
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (a[i][j] == '1') {
          cnt++;
          irand(i, j);
        }
      }
    }
    printf("%d\\n", cnt);
  }
}