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

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

/*
const int MAX=1000;
vector<vector<int> > memo(MAX,vector<int>(MAX));
vector<vector<int> > done(MAX,vector<int>(MAX));
int dfs(vector<vector<int> > vi,int i,int j,int sum);
int main(){
        //ios::sync_with_stdio(false);
        //cin.tie(0);
        int m,n;
        while(cin>>m>>n&&m!=0&&n!=0){
                vector<vector<int> > ice(m,vector<int>(n));
                int ans=0;
                /*
                for(int i=0; i<MAX; ++i){
                        for(int j=0; j<MAX; ++j){
                                memo[i][j]=0;
                                done[i][j]=0;
                        }
                }
                */
/*
                for(int i=0; i<n; ++i){
                        for(int j=0; j<m; ++j){
                                cin >> ice[j][i];
                        }
                }
                for(int i=0; i<n; ++i){
                        for(int j=0; j<m; ++j){
                                if(ice[j][i]==1) {
                                        ans=max(ans,dfs(ice,j,i,0));
                                        /*for(int k=0; k<n; ++k){
                                                for(int l=0; l<m; ++l){
                                                        memo[j][i]=0;
                                                        done[j][i]=0;
                                                }
                                        }*/
/*
                                }
                        }
                }
                cout << ans << "\\n";
        }
        return 0;
}

int dfs(vector<vector<int> > vi,int i,int j,int sum){
        vector<int> f(4);
        //if(done[i][j]==1) return memo[i][j];

        if(i+1>=vi.size()) {
                f[0]=1;
        }else if(vi[i+1][j]==0) {
                f[0]=1;
        }

        if(j+1>=vi[i].size()){
                f[1]=1;
        } else if(vi[i][j+1]==0){
                f[1]=1;
        }

        if(i-1<0){
                f[2]=1;
        }else if(vi[i-1][j]==0){
                f[2]=1;
        }

        if(j-1<0){
                f[3]=1;
        } else if(vi[i][j-1]==0){
                f[3]=1;
        }

        if(f[0]==1&&f[1]==1&&f[2]==1&&f[3]==1) return sum;

        int res=0;
        //done[i][j]=1;
        if(i+1<vi.size()){
                if(vi[i+1][j]==1){
                        res=max(res,dfs(vi,i+1,j,sum+1));
                }
        }
        if(j+1<vi[i].size()) {
                if(vi[i][j+1]==1){
                        res=max(res,dfs(vi,i,j+1,sum+1));
                }
        }
        if(i-1>=0){
                if(vi[i-1][j]==1){
                        res=max(res,dfs(vi,i-1,j,sum+1));
                }
        }
        if(j-1>=0) {
                if(vi[i][j-1]==1){
                        res=max(res,dfs(vi,i,j-1,sum+1));
                }
        }
        //done[i][j]=0;
        return res;
}
*/

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

void dfs(vector<string> &vss, int x, int y) {
  vss[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 && vss[nx][ny] == '1') {
      dfs(vss, nx, ny);
    }
  }
}

int main() {
  vector<string> land(12);
  int n = 0;
  while (cin >> land[0]) {
    int cnt = 0;
    for (int i = 1; i < 12; ++i) {
      cin >> land[i];
    }
    for (int i = 0; i < 12; ++i) {
      for (int j = 0; j < 12; ++j) {
        if (land[i][j] == '1') {
          ++cnt;
          dfs(land, i, j);
        }
      }
    }
    cout << cnt << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

/*
const int MAX=1000;
vector<vector<int> > memo(MAX,vector<int>(MAX));
vector<vector<int> > done(MAX,vector<int>(MAX));
int dfs(vector<vector<int> > vi,int i,int j,int sum);
int main(){
        //ios::sync_with_stdio(false);
        //cin.tie(0);
        int m,n;
        while(cin>>m>>n&&m!=0&&n!=0){
                vector<vector<int> > ice(m,vector<int>(n));
                int ans=0;
                /*
                for(int i=0; i<MAX; ++i){
                        for(int j=0; j<MAX; ++j){
                                memo[i][j]=0;
                                done[i][j]=0;
                        }
                }
                */
/*
                for(int i=0; i<n; ++i){
                        for(int j=0; j<m; ++j){
                                cin >> ice[j][i];
                        }
                }
                for(int i=0; i<n; ++i){
                        for(int j=0; j<m; ++j){
                                if(ice[j][i]==1) {
                                        ans=max(ans,dfs(ice,j,i,0));
                                        /*for(int k=0; k<n; ++k){
                                                for(int l=0; l<m; ++l){
                                                        memo[j][i]=0;
                                                        done[j][i]=0;
                                                }
                                        }*/
/*
                                }
                        }
                }
                cout << ans << "\\n";
        }
        return 0;
}

int dfs(vector<vector<int> > vi,int i,int j,int sum){
        vector<int> f(4);
        //if(done[i][j]==1) return memo[i][j];

        if(i+1>=vi.size()) {
                f[0]=1;
        }else if(vi[i+1][j]==0) {
                f[0]=1;
        }

        if(j+1>=vi[i].size()){
                f[1]=1;
        } else if(vi[i][j+1]==0){
                f[1]=1;
        }

        if(i-1<0){
                f[2]=1;
        }else if(vi[i-1][j]==0){
                f[2]=1;
        }

        if(j-1<0){
                f[3]=1;
        } else if(vi[i][j-1]==0){
                f[3]=1;
        }

        if(f[0]==1&&f[1]==1&&f[2]==1&&f[3]==1) return sum;

        int res=0;
        //done[i][j]=1;
        if(i+1<vi.size()){
                if(vi[i+1][j]==1){
                        res=max(res,dfs(vi,i+1,j,sum+1));
                }
        }
        if(j+1<vi[i].size()) {
                if(vi[i][j+1]==1){
                        res=max(res,dfs(vi,i,j+1,sum+1));
                }
        }
        if(i-1>=0){
                if(vi[i-1][j]==1){
                        res=max(res,dfs(vi,i-1,j,sum+1));
                }
        }
        if(j-1>=0) {
                if(vi[i][j-1]==1){
                        res=max(res,dfs(vi,i,j-1,sum+1));
                }
        }
        //done[i][j]=0;
        return res;
}
*/

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

void dfs(vector<string> &vss, int x, int y) {
  vss[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 && vss[nx][ny] == '1') {
      dfs(vss, nx, ny);
    }
  }
}

int main() {
  vector<string> land(12);
  int n = 0;
  while (1) {
    int cnt = 0;
    for (int i = 0; i < 12; ++i) {
      cin >> land[i];
    }
    for (int i = 0; i < 12; ++i) {
      for (int j = 0; j < 12; ++j) {
        if (land[i][j] == '1') {
          ++cnt;
          dfs(land, i, j);
        }
      }
    }
    cout << cnt << "\\n";
  }
  return 0;
}