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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
int W, H;
char grid[50][50];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int ansB, ansW;

void bfs(int sx, int sy) {
  queue<P> que;
  bool flagB = true, flagW = true;
  int res = 0;
  while (!que.empty())
    que.pop();

  que.push(P(sx, sy));

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.first, y = p.second;
    if (grid[x][y] == '.') {
      res++;
      grid[x][y] = '#';
      if (flagB == true && ((x + 1 < W && grid[x + 1][y] == 'B') ||
                            (x - 1 >= 0 && grid[x - 1][y] == 'B') ||
                            (y + 1 < H && grid[x][y + 1] == 'B') ||
                            (y - 1 >= 0 && grid[x][y - 1] == 'B'))) {
        flagB = false;
      }
      if (flagW == true && ((x + 1 < W && grid[x + 1][y] == 'W') ||
                            (x - 1 >= 0 && grid[x - 1][y] == 'W') ||
                            (y + 1 < H && grid[x][y + 1] == 'W') ||
                            (y - 1 >= 0 && grid[x][y - 1] == 'W'))) {
        flagW = false;
      }
    } else
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];

      if (0 <= nx && nx < W && 0 <= ny && ny < H && grid[nx][ny] == '.') {
        que.push(P(nx, ny));
      }
    }
  }
  if (flagB == false && flagW == true)
    ansB += res;
  else if (flagB == true && flagW == false)
    ansW += res;
N:;
}

int main() {

  while (cin >> W >> H, W + H) {
    ansB = 0, ansW = 0;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[j][i];
      }
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (grid[j][i] == '.') {
          bfs(j, i);
        }
      }
    }
    cout << ansB << " " << ansW << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> P;
char grid[50][50];
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
char c[2] = {'B', 'W'};
/*void p(){
  for(int i = 0 ; i < H ; i++ ){
    for(int j = 0 ; j < W ; j++ ){
      cout << grid[j][i];
    }
    cout << endl;
  }
  }*/
int bfs(int sx, int sy, int k) {
  queue<P> que;
  int res[2] = {0};
  bool flag = true;
  que.push(P(sx, sy));
  while (!que.empty()) {
    P p = que.front();
    que.pop();

    if (grid[p.first][p.second] == '.') {
      grid[p.first][p.second] = c[k];
      res[k]++;
    }

    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < W && 0 <= ny && ny < H && grid[nx][ny] == '.') {
        que.push(P(nx, ny));
        if ((nx + 1 < W && grid[nx + 1][ny] == c[!k]) ||
            (nx - 1 >= 0 && grid[nx - 1][ny] == c[!k]) ||
            (ny + 1 < H && grid[nx][ny + 1] == c[!k]) ||
            (ny - 1 >= 0 && grid[nx][ny - 1] == c[!k]))
          flag = false;
      }
    }
  }
  if (flag == true)
    return res[k];
  else
    return 0;
}

int main() {
  while (cin >> W >> H, W + H) {

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> grid[j][i];
      }
    }
    int ans[2] = {0};
    for (int k = 0; k < 2; k++) {
      for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
          if (grid[j][i] == '.') {
            if ((j + 1 < W && grid[j + 1][i] == c[k]) ||
                (j - 1 >= 0 && grid[j - 1][i] == c[k]) ||
                (i + 1 < H && grid[j][i + 1] == c[k]) ||
                (i - 1 >= 0 && grid[j][i - 1] == c[k]))
              ans[k] += bfs(j, i, k); // p();
          }
        }
      }
    }
    cout << ans[0] << " " << ans[1] << endl;
  }
  return 0;
}