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

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

void dfs(int x, int y);
char data[16][16];

int main(void) {
  char c;

  while (1) {

    for (int i = 0; i < 12; i++) {
      cin >> data[i];
    }
    if (cin.eof())
      return (0);

    int res = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (data[i][j] == '1') {
          dfs(i, j);
          res++;
        }
      }
    }

    printf("%d\\n", res);
    scanf("%c", &c);
  }

  return (0);
}

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

  if (data[x - 1][y] == '1' && 0 <= (x - 1)) {
    dfs(x - 1, y);
  }
  if (data[x + 1][y] == '1') {
    dfs(x + 1, y);
  }
  if (data[x][y - 1] == '1' && 0 <= (y - 1)) {
    dfs(x, y - 1);
  }
  if (data[x][y + 1] == '1') {
    dfs(x, y + 1);
  }

  return;
}

Error: TLE

Fixed submission:

#include <cstdio>

void dfs(int x, int y);
char data[16][16];

int main(void) {
  char c;

  while (1) {
    for (int i = 0; i < 12; i++) {
      scanf("%s", data[i]);
    }

    int res = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (data[i][j] == '1') {
          dfs(i, j);
          res++;
        }
      }
    }

    printf("%d\\n", res);
    scanf("%c", &c);
  }

  return (0);
}

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

  if (data[x - 1][y] == '1' && 0 <= (x - 1)) {
    dfs(x - 1, y);
  }
  if (data[x + 1][y] == '1') {
    dfs(x + 1, y);
  }
  if (data[x][y - 1] == '1' && 0 <= (y - 1)) {
    dfs(x, y - 1);
  }
  if (data[x][y + 1] == '1') {
    dfs(x, y + 1);
  }

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

#include <iostream>
using namespace std;

char isl[12][12];

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

  for (int i = -1; i <= 1; i += 2) {
    int ax = x + i;
    if (0 <= ax && ax < 12 && isl[ax][y] == '1') {
      dfs(ax, y);
    }
  }

  for (int j = -1; j <= 1; j += 2) {
    int ay = y + j;
    if (0 <= ay && ay < 12 && isl[x][ay] == '1') {
      dfs(x, ay);
    }
  }
  return;
}

int main() {

  while (cin >> isl[0]) {
    int ca = 0;
    for (int i = 1; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        cin >> isl[i][j];
        if (isl[i][j] == EOF) {
          break;
        }
      }
    }

    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (isl[i][j] == '1') {
          dfs(i, j);
          ca++;
        }
      }
    }
    cout << ca << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

char isl[12][12];

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

  for (int i = -1; i <= 1; i += 2) {
    int ax = x + i;
    if (0 <= ax && ax < 12 && isl[ax][y] == '1') {
      dfs(ax, y);
    }
  }

  for (int j = -1; j <= 1; j += 2) {
    int ay = y + j;
    if (0 <= ay && ay < 12 && isl[x][ay] == '1') {
      dfs(x, ay);
    }
  }
  return;
}

int main() {

  while (1) {
    int ca = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        cin >> isl[i][j];
        if (isl[i][j] == EOF) {
          break;
        }
      }
    }

    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (isl[i][j] == '1') {
          dfs(i, j);
          ca++;
        }
      }
    }
    cout << ca << endl;
  }
  return 0;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

//============================================================================
// Name        : aoj0067.cpp
// Author      : afterCmidday
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

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

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

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

Error: TLE

Fixed submission:

//============================================================================
// Name        : aoj0067.cpp
// Author      : afterCmidday
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

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

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

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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
char shima[13][13];
void tansaku(int x, int y) {
  if (x < 0 || x > 11 || y < 0 || y > 11 || shima[y][x] == '0')
    return;
  if (shima[y][x] == '1') {
    shima[y][x] = '0';
  }
  tansaku(x + 1, y);
  tansaku(x - 1, y);
  tansaku(x, y + 1);
  tansaku(x, y - 1);
}
int main() {

  while (cin >> shima[0][0]) {
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (i == 0 && j == 0)
          continue;
        else
          cin >> shima[i][j];
      }
    }
    int a = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (shima[i][j] == '1') {
          tansaku(j, i);
          a++;
        }
      }
    }
    cout << a << endl;
  }
}


Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
char shima[13][13];
void tansaku(int x, int y) {
  if (x < 0 || x > 11 || y < 0 || y > 11 || shima[y][x] == '0')
    return;
  if (shima[y][x] == '1') {
    shima[y][x] = '0';
  }
  tansaku(x + 1, y);
  tansaku(x - 1, y);
  tansaku(x, y + 1);
  tansaku(x, y - 1);
}
int main() {

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

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string str;
int xs[] = {0, 1, 0, -1}, ys[] = {1, 0, -1, 0};
bool m[20][20];
void dfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int tx = x + xs[i], ty = y + ys[i];
    if (tx < 0 || tx >= 12 || ty < 0 || ty >= 12)
      continue;
    if (m[tx][ty]) {
      m[tx][ty] = false;
      dfs(tx, ty);
    }
  }
}

int main() {
  while (true) {
    int res = 0;
    for (int i = 0; i < 12; i++) {
      if (cin >> str) {
        for (int j = 0; j < str.size(); j++) {
          m[i][j] = str[j] == '1';
        }
      } else
        return 0;
    }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (m[i][j]) {
          res++;
          dfs(i, j);
        }
      }
    }
    cout << res << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string str;
int xs[] = {0, 1, 0, -1}, ys[] = {1, 0, -1, 0};
bool m[10][10];
void dfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int tx = x + xs[i], ty = y + ys[i];
    if (tx < 0 || tx >= 12 || ty < 0 || ty >= 12)
      continue;
    if (m[tx][ty]) {
      m[tx][ty] = false;
      dfs(tx, ty);
    }
  }
}

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

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool visited[12][12];

void dfs(int y, int x, char data[12][12]) {
  visited[y][x] = true;

  int d[] = {-1, 0, 1, 0, -1};
  rep(i, 4) {
    if (y + d[i] >= 0 && x + d[i + 1] >= 0 && 12 > y + d[i] &&
        12 > x + d[i + 1])
      if (data[y + d[i]][x + d[i + 1]] == '1' &&
          visited[y + d[i]][x + d[i + 1]] == false)
        dfs(y + d[i], x + d[i + 1], data);
  }
}

int main() {
  char data[12][12];

  while (cin >> data[0]) {
    int sum = 0;
    for (int i = 1; i < 12; i++) {
      cin >> data[i];
    }

    rep(i, 12) rep(j, 12) visited[i][j] = false;

    rep(i, 12) {
      rep(j, 12) {
        if (visited[i][j] == false && data[i][j] == '1') {
          dfs(i, j, data);
          sum++;
        }
      }
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

bool visited[12][12];

void dfs(int y, int x, char data[12][12]) {
  if (data[y][x] == '0' || y < 0 || x < 0 || y >= 12 || x >= 12)
    return;
  visited[y][x] = true;

  int d[] = {-1, 0, 1, 0, -1};
  rep(i, 4) {
    if (!(y + d[i] < 0 || x + d[i + 1] < 0 || y + d[i] >= 12 ||
          x + d[i + 1] >= 12))
      if (data[y + d[i]][x + d[i + 1]] == '1' &&
          visited[y + d[i]][x + d[i + 1]] == false)
        dfs(y + d[i], x + d[i + 1], data);
  }
}

int main() {
  string a;
  while (cin >> a) {
    char data[12][12];
    int sum = 0;
    rep(i, 12) data[0][i] = a[i];
    for (int i = 1; i < 12; i++) {
      cin >> data[i];
    }

    rep(i, 12) rep(j, 12) visited[i][j] = false;

    rep(i, 12) {
      rep(j, 12) {
        if (visited[i][j] == false && data[i][j] == '1') {
          dfs(i, j, data);
          sum++;
        }
      }
    }
    cout << sum << endl;
  }
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

/*********
深さ優先探索
**********/

#include <algorithm>
#include <cstdio>
#include <iostream>
#define MAX 100

// 入力
int n, m;
char field[MAX][MAX] = {-1}; // 庭

// プロトタイプ宣言
void bfs(int x, int y);
void slove();

int main() {
  int i, j, flag;

  while (1) {

    for (i = 0; i < 12; i++) {
      flag = 0;
      if (scanf("%s", field[i]) == EOF)
        flag = -1;
    }

    if (flag == -1)
      break;
    slove();
  }

  return 0;
}

void bfs(int i, int j) {
  // 今いるところを、.に置き換える
  field[i][j] = '0';

  // 移動する4方向を調べる
  if (0 <= j && j < 12 && 0 <= i - 1 && i - 1 < 12 && field[i - 1][j] == '1')
    bfs(i - 1, j);
  if (0 <= j - 1 && j - 1 < 12 && 0 <= i && i < 12 && field[i][j - 1] == '1')
    bfs(i, j - 1);
  if (0 <= j + 1 && j + 1 < 12 && 0 <= i && i < 12 && field[i][j + 1] == '1')
    bfs(i, j + 1);
  if (0 <= j && j < 12 && 0 <= i + 1 && i + 1 < 12 && field[i + 1][j] == '1')
    bfs(i + 1, j);

  return;
}

void slove() {
  int res = 0;
  for (int i = 0; i < 12; i++) {
    for (int j = 0; j < 12; j++) {
      if (field[i][j] == '1') {
        // wが残っているならそこからbfsを始める
        bfs(i, j);
        res++;
      }
    }
  }
  printf("%d\\n", res);
}

Error: TLE

Fixed submission:

/*********
深さ優先探索
**********/

#include <algorithm>
#include <cstdio>
#include <iostream>
#define MAX 100

// 入力
int n, m;
char field[MAX][MAX] = {-1}; // 庭

// プロトタイプ宣言
void bfs(int x, int y);
void slove();

int main() {
  int i, j;
  while (1) {
    for (i = 0; i < 12; i++) {
      scanf("%s", field[i]);
    }
    slove();
  }
  return 0;
}

void bfs(int i, int j) {
  // 今いるところを、.に置き換える
  field[i][j] = '0';

  // 移動する4方向を調べる
  if (0 <= j && j < 12 && 0 <= i - 1 && i - 1 < 12 && field[i - 1][j] == '1')
    bfs(i - 1, j);
  if (0 <= j - 1 && j - 1 < 12 && 0 <= i && i < 12 && field[i][j - 1] == '1')
    bfs(i, j - 1);
  if (0 <= j + 1 && j + 1 < 12 && 0 <= i && i < 12 && field[i][j + 1] == '1')
    bfs(i, j + 1);
  if (0 <= j && j < 12 && 0 <= i + 1 && i + 1 < 12 && field[i + 1][j] == '1')
    bfs(i + 1, j);

  return;
}

void slove() {
  int res = 0;
  for (int i = 0; i < 12; i++) {
    for (int j = 0; j < 12; j++) {
      if (field[i][j] == '1') {
        // wが残っているならそこからbfsを始める
        bfs(i, j);
        res++;
      }
    }
  }
  printf("%d\\n", res);
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

int masu[14][14];
int cont;

void saiki(int x, int y) {
  masu[x][y] = 0;
  if (masu[x + 1][y] == 1) {
    saiki(x + 1, y);
  }
  if (masu[x - 1][y] == 1) {
    saiki(x - 1, y);
  }
  if (masu[x][y + 1] == 1) {
    saiki(x, y + 1);
  }
  if (masu[x][y - 1] == 1) {
    saiki(x, y - 1);
  }
}

int main() {
  while (1) {
    for (int i = 0; i < 14; i++) {
      for (int j = 0; j < 14; j++)
        masu[i][j] = 0;
    }
    cont = 0;

    char str[100];
    for (int i = 1; i <= 12; i++) {
      if (scanf("%s", str) == EOF) {
        goto A;
      }
      for (int j = 1; j <= 12; j++) {
        if (str[j - 1] == '1') {
          masu[i][j] = 1;
        }
      }
    }

    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (masu[i][j] == 1) {
          saiki(i, j);
          cont++;
        }
      }
    }
    printf("%d\\n", cont);
  }
A:;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int masu[14][14];
int cont;

void saiki(int x, int y) {
  masu[x][y] = 0;
  if (masu[x + 1][y] == 1) {
    saiki(x + 1, y);
  }
  if (masu[x - 1][y] == 1) {
    saiki(x - 1, y);
  }
  if (masu[x][y + 1] == 1) {
    saiki(x, y + 1);
  }
  if (masu[x][y - 1] == 1) {
    saiki(x, y - 1);
  }
}

int main() {
  while (1) {
    for (int i = 0; i < 14; i++) {
      for (int j = 0; j < 14; j++)
        masu[i][j] = 0;
    }
    cont = 0;

    char str[100];
    for (int i = 1; i <= 12; i++) {
      if (scanf("%s", str) == EOF) {
        // goto A;
      }
      for (int j = 1; j <= 12; j++) {
        if (str[j] == '1') {
          masu[i][j] = 1;
        }
      }
    }

    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (masu[i][j] == 1) {
          saiki(i, j);
          cont++;
        }
      }
    }
    printf("%d\\n", cont);
  }
A:;
}
Problem p00067 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int masu[14][14];
int cont;

void saiki(int x, int y) {
  masu[x][y] = 0;
  if (masu[x + 1][y] == 1)
    saiki(x + 1, y);
  if (masu[x][y + 1] == 1)
    saiki(x, y + 1);
  if (masu[x - 1][y] == 1)
    saiki(x, y - 1);
  if (masu[x][y - 1] == 1)
    saiki(x, y - 1);
}

int main() {
  while (1) {
    for (int i = 0; i < 14; i++) {
      for (int j = 0; j < 14; j++) {
        masu[i][j] = 0;
      }
    }
    for (int i = 1; i <= 12; i++) {
      char st[13];
      if (scanf("%s", st) == EOF) {
        goto A;
      }
      for (int j = 0; j < 12; j++) {
        if (st[j] == '0') {
          masu[i][j + 1] = 0;
        } else {
          masu[i][j + 1] = 1;
        }
      }
    }
    cont = 0;
    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (masu[i][j] == 1) {
          cont++;
          saiki(i, j);
        }
      }
    }
    printf("%d\\n", cont);
  }
A:;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int masu[14][14];
int cont;

void saiki(int x, int y) {
  masu[x][y] = 0;
  if (masu[x + 1][y] == 1)
    saiki(x + 1, y);
  if (masu[x][y + 1] == 1)
    saiki(x, y + 1);
  if (masu[x - 1][y] == 1)
    saiki(x, y - 1);
  if (masu[x][y - 1] == 1)
    saiki(x, y - 1);
}

int main() {
  while (1) {
    for (int i = 0; i < 14; i++) {
      for (int j = 0; j < 14; j++) {
        masu[i][j] = 0;
      }
    }
    for (int i = 1; i <= 12; i++) {
      char st[13];
      scanf("%s", st);
      if (st[0] == EOF) {
        goto A;
      }
      for (int j = 0; j < 12; j++) {
        if (st[j] == '0') {
          masu[i][j + 1] = 0;
        } else {
          masu[i][j + 1] = 1;
        }
      }
    }
    cont = 0;
    for (int i = 1; i <= 12; i++) {
      for (int j = 1; j <= 12; j++) {
        if (masu[i][j] == 1) {
          cont++;
          saiki(i, j);
        }
      }
    }
    printf("%d\\n", cont);
  }
A:;
}
Problem p00067 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

string field[15];

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

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

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

string field[13];

void dfs(int x, int y) {
  if (x >= 1 && x <= 12 && y >= 1 && y <= 12) {
    if (field[x][y] != 0) {
      field[x][y] = 2;
      dfs(x + 1, y);
      dfs(x - 1, y);
      dfs(x, y + 1);
      dfs(x, y - 1);
    }
  }
}

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

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

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

char map[13][13];
int da[] = {1, 0, -1, 0};
int db[] = {0, 1, 0, -1};

void dfs(int a, int b) {
  if (a < 0 || a >= 12 || b < 0 || b >= 12 || map[a][b] == '0')
    return;
  map[a][b] = '0';
  for (int i = 0; i < 4; i++) {
    dfs(a + da[i], b + db[i]);
  }
  return;
}

int main(void) {
  while (true) {
    int cnt = 0;

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

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

Error: TLE

Fixed submission:

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

char il[12][12];
int cnt;

void search(int x, int y) {
  il[x][y] = 0;
  for (int dx = -1; dx <= 1; dx++) {
    if (dx == 0)
      continue;
    int nx = x + dx;
    int ny = y;
    if (0 <= nx && nx <= 12 && 0 <= ny && ny <= 12 && il[nx][ny] == '1')
      search(nx, ny);
  }

  for (int dy = -1; dy <= 1; dy++) {
    if (dy == 0)
      continue;
    int nx = x;
    int ny = y + dy;
    if (0 <= nx && nx <= 12 && 0 <= ny && ny <= 12 && il[nx][ny] == '1')
      search(nx, ny);
  }
  return;
}

int main() {
  while (1) {
    cnt = 0;
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        scanf("%c", il[i][j]);
      }
    }

    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (il[i][j] == '1') {
          search(i, j);
          cnt++;
        }
      }
    }
    printf("%d\\n", cnt);
  }

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

#include <iostream>
#include <string>
using namespace std;
char dp[12][12];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void DFS(int x, int y) {
  dp[y][x] = '0';
  for (int i = 0; i < 4; i++) {
    if (x + dx[i] >= 0 && x + dx[i] < 12 && y + dy[i] >= 0 && y + dy[i] < 12) {
      if (dp[y + dy[i]][x + dx[i]] == '1')
        DFS(x + dx[i], y + dy[i]);
    }
  }
  return;
}
int main() {
  while (cin >> dp[0][0]) {
    int ans = 0;
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++)
        if (i + j != 0) {
          cin >> dp[i][j];
        }
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (dp[i][j] == '1') {
          ans++;
          DFS(j, i);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
char dp[12][12];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void DFS(int x, int y) {
  dp[y][x] = '0';
  for (int i = 0; i < 4; i++) {
    if (x + dx[i] >= 0 && x + dx[i] < 12 && y + dy[i] >= 0 && y + dy[i] < 12) {
      if (dp[y + dy[i]][x + dx[i]] == '1')
        DFS(x + dx[i], y + dy[i]);
    }
  }
  return;
}
int main() {
  while (true) {
    int ans = 0;
    for (int i = 0; i < 12; i++)
      for (int j = 0; j < 12; j++)
        cin >> dp[i][j];
    for (int i = 0; i < 12; i++) {
      for (int j = 0; j < 12; j++) {
        if (dp[i][j] == '1') {
          ans++;
          DFS(j, i);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}