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

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

int dxy[4] = {-1, 1, 0, 0};
int W, H;
char a[60][60];
int temp[4][60][60];

void huga(int y, int x, int Z) { // cout<<y<<" "<<x<<endl;
  int w = 0, b = 0, s = 0, ans = -3, t;
  queue<P> Q;

  Q.push(P(y, x));

  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int yy = p.first, xx = p.second; // cout<<yy<<" "<<xx<<endl;
    for (int i = 0; i <= 3; i++) {
      int nx = xx + dxy[i],
          ny = yy + dxy[3 - i]; // cout<<" "<<ny<<" "<<nx<<endl;
      if (a[ny][nx] == '.' && !temp[Z + 2][ny][nx]) {
        temp[Z + 2][ny][nx] = temp[Z][ny][nx] = 1;
        Q.push(P(ny, nx));
        // cout<<temp[2][ny][nx]<<" "<<temp[Z][ny][nx]<<" AA"<<endl;
      }
      // int fds; cin>>fds;
    }
  }
}

int main() {

  while (1) {
    int cw = 0, cb = 0;
    cin >> W >> H;

    if (!W && !H)
      break;

    for (int i = 0; i <= H + 1; i++)
      for (int j = 0; j <= W + 1; j++)
        temp[0][i][j] = temp[1][i][j] = temp[2][i][j] = temp[3][i][j] = 0;

    for (int i = 0; i < 60; i++) {
      a[0][i] = a[H + 1][i] = 0;
      a[i][0] = a[i][W + 1] = 0;
    }

    for (int i = 1; i <= H; i++)
      for (int j = 1; j <= W; j++)
        cin >> a[i][j];

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        // if(temp[2][i][j])continue;
        if (a[i][j] == 'B')
          huga(i, j, 0);
        else if (a[i][j] == 'W')
          huga(i, j, 1);
      }
    }

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        if (a[i][j] == '.') {
          if (temp[0][i][j] && !temp[1][i][j])
            cb++;
          else if (!temp[0][i][j] && temp[1][i][j])
            cw++;
        }
      }
    }

    cout << cb << " " << cw << endl;
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

int dxy[4] = {-1, 1, 0, 0};
int W, H;
char a[60][60];
int temp[60][60];

int huga(int x, int y) {                // cout<<y<<" "<<x<<endl;
  int w = 0, b = 0, s = 0, ans = -3, t; // z[60][60] = {};
  vector<vector<int>> z;
  z = vector<vector<int>>(H + 1, vector<int>(W + 1, 0));
  queue<P> Q;

  Q.push(P(y, x));

  while (!Q.empty()) {
    P p = Q.front();
    Q.pop();
    int yy = p.first, xx = p.second;
    z[yy][xx] = 1;
    for (int i = 0; i <= 3; i++) {
      int nx = xx + dxy[i], ny = yy + dxy[3 - i];
      if (temp[ny][nx] == -1)
        return -1;
    }
    for (int i = 0; i <= 3; i++) {
      int nx = xx + dxy[i], ny = yy + dxy[3 - i];
      if (a[ny][nx] == '.' && !z[ny][nx])
        Q.push(P(ny, nx));
      else if (a[ny][nx] == 'B')
        b = 1;
      else if (a[ny][nx] == 'W')
        w = 1;
      if (b && w)
        return ans = -1;
    }
  }

  if (w && b || !w && !b)
    ans = -1;
  else if (!w && b)
    ans = 1;
  else if (!b && w)
    ans = 0;
  else
    ans = -4;

  // cout<<ans<<endl<<endl;
  return ans;
}

int main() {

  while (1) {
    int cw = 0, cb = 0;
    cin >> W >> H;

    if (!W && !H)
      break;
    for (int i = 0; i <= W + 1; i++) {
      a[0][i] = a[H + 1][i] = 0;
      temp[H + 1][i] = temp[0][i] = 2;
    }

    for (int i = 0; i <= H + 1; i++) {
      a[i][0] = a[i][W + 1] = 0;
      temp[i][0] = temp[0][i] = 2;
    }

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> a[i][j];
        temp[i][j] = 2;
      }
    }
    /*
    for(int i = 1; i <= H; i++){
            for(int j = 1; j <= W; j++){
                    if(a[i][j] == '.'){
                            //cout<<"---- "<<i<<" "<<j<<endl;
                            int z[60][60] = {};
                            temp[i][j] = huga(j,i,z);
                            if(temp[i][j] == 1)a[i][j]=='B';
                            else if(temp[i][j] == 0)a[i][j]=='W';
                            //cout<<"---"<<endl<<endl;;
                            printf("%2d" , temp[i][j]);
                    } else cout<<" "<<a[i][j];
            }cout<<endl;

    }*/

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        if (a[i][j] == '.') {
          temp[i][j] = huga(j, i);
          if (temp[i][j] == 1)
            a[i][j] == 'B';
          else if (temp[i][j] == 0)
            a[i][j] == 'W';
          if (temp[i][j] == 1)
            cb++;
          else if (temp[i][j] == 0)
            cw++;
        }
      }
    }

    cout << cb << " " << cw << endl;
  }

  return 0;
}