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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i > (b); i--)
#define REP(i, n) FOR(i, 0, n)

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// debug
//-------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

vector<string> island(12);

void dfs(int i, int j) {
  island[i][j] = '0';

  if (i < 11 && island[i + 1][j] == '1') {
    dfs(i + 1, j);
  }
  if (j < 11 && island[i][j + 1] == '1') {
    dfs(i, j + 1);
  }
  if (j > 0 && island[i][j - 1] == '1') {
    dfs(i, j - 1);
  }
  if (i > 0 && island[i - 1][j] == '1') {
    dfs(i - 1, j);
  }

  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (1) {
    REP(i, 12) {
      if (!(cin >> island[i])) {
        return 0;
      }
    }

    int cnt = 0;
    REP(i, 12) REP(j, 12) {
      if (island[i][j] == '1') {
        dfs(i, j);
        cnt++;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i > (b); i--)
#define REP(i, n) FOR(i, 0, n)

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// debug
//-------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

vector<string> island(12);

void dfs(int i, int j) {
  island[i][j] = '0';

  if (i < 11 && island[i + 1][j] == '1') {
    dfs(i + 1, j);
  }
  if (j < 11 && island[i][j + 1] == '1') {
    dfs(i, j + 1);
  }
  if (j > 0 && island[i][j - 1] == '1') {
    dfs(i, j - 1);
  }
  if (i > 0 && island[i - 1][j] == '1') {
    dfs(i - 1, j);
  }

  return;
}

int main() {
  while (1) {
    REP(i, 12) {
      if (!(cin >> island[i])) {
        break;
      }
    }

    int cnt = 0;
    REP(i, 12) REP(j, 12) {
      if (island[i][j] == '1') {
        dfs(i, j);
        cnt++;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}