Problem p00118 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
// #include <fstream>

using namespace std;

bool dfs(int row, int col, char p, vector<string> d,
         vector<vector<bool>> &used) {
  // order up right down left
  const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  int size = d.size();
  int len = d[0].size();
  int k;

  if (used[row][col]) {
    return false;
  } // end if

  used[row][col] = true;

  for (k = 0; k < 4; ++k) {
    int nrow = row + dir[k][0];
    int ncol = col + dir[k][1];
    if (nrow < 0 || nrow >= size || ncol < 0 || ncol >= len) {
      continue;
    } // end if
    if (!used[nrow][ncol] && d[nrow][ncol] == p) {
      if (dfs(nrow, ncol, p, d, used)) {
        return true;
      } // end if
    }   // end if
  }     // end for

  return false;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.property", "r", stdin );

  string str = "";
  int h, w;

  while (1) {
    cin >> h;
    cin >> w;

    if (h == 0 && w == 0) {
      break;
    } // end if

    vector<string> d(h, string(w, ' '));

    int i, j;

    for (i = 0; i < h; ++i) {
      cin >> str;
      for (j = 0; j < w; ++j) {
        d[i][j] = str[j];
      } // end for
    }   // end for

    vector<vector<bool>> used(h, vector<bool>(w, false));

    int res = 0;

    for (i = 0; i < h; ++i) {
      for (j = 0; j < w; ++j) {
        if (!used[i][j]) {
          dfs(i, j, d[i][j], d, used);
          ++res;
        } // end if
      }   // end for
    }     // end for

    cout << res << endl;
  } // end loop

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
// #include <fstream>

using namespace std;

typedef pair<int, int> P;

void bfs(int curr_row, int curr_col, char p, vector<string> d,
         vector<vector<bool>> &used) {
  // order up right down left
  const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  int size = d.size();
  int len = d[0].size();
  int k;

  queue<P> Q;
  P curr(curr_row, curr_col);
  Q.push(curr);

  int row = curr_row;
  int col = curr_col;

  while (!Q.empty()) {
    curr = Q.front();
    Q.pop();
    row = curr.first;
    col = curr.second;

    used[row][col] = true;

    for (k = 0; k < 4; ++k) {
      int nrow = row + dir[k][0];
      int ncol = col + dir[k][1];
      if (nrow < 0 || nrow >= size || ncol < 0 || ncol >= len) {
        continue;
      } // end if
      if (!used[nrow][ncol] && d[nrow][ncol] == p) {
        P npos(nrow, ncol);
        Q.push(npos);
      } // end if
    }   // end if

  } // end for
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.property", "r", stdin );

  string str = "";
  int h, w;

  while (1) {
    cin >> h;
    cin >> w;

    if (h == 0 && w == 0) {
      break;
    } // end if

    vector<string> d(h, string(w, ' '));

    int i, j;

    for (i = 0; i < h; ++i) {
      cin >> str;
      for (j = 0; j < w; ++j) {
        d[i][j] = str[j];
      } // end for
    }   // end for

    vector<vector<bool>> used(h, vector<bool>(w, false));

    int res = 0;

    for (i = 0; i < h; ++i) {
      for (j = 0; j < w; ++j) {
        if (!used[i][j]) {
          bfs(i, j, d[i][j], d, used);
          ++res;
        } // end if
      }   // end for
    }     // end for

    cout << res << endl;

  } // end loop

  return 0;
}