Problem p00749 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
char str[60][11];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int S[60][11];
int e[200][200];
int L[200];
int R[200];
int Z[200];
int p;
bool ret;
struct wolf {
  int v[10];
  wolf() {
    for (int i = 0; i < 10; i++)
      v[i] = 0;
  }
};
wolf dat[200];
wolf dfs(int a, int b) {
  // printf("%d\\n",a);
  wolf W = dat[a];
  for (int i = 0; i < p; i++)
    if (e[a][i] && i != b) {
      wolf V = dfs(i, a);
      for (int j = 0; j < 10; j++)
        W.v[j] += V.v[j];
    }
  double ave = 0;
  int div = 0;
  for (int i = 0; i < 10; i++) {
    ave += (0.5 + i) * W.v[i];
    div += W.v[i];
  }
  ave /= div;
  //	printf("%f %d %d\\n",ave,L[a],R[a]);
  if (ave < 0.00000001 + L[a] || ave > 0.9999999 + R[a])
    ret = false;
  return W;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    ret = true;
    for (int i = 0; i < a; i++) {
      scanf("%s", str[a - 1 - i]);
      for (int j = 0; j < b; j++)
        S[i][j] = 0;
    }
    p = 1;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (str[i][j] != '.' && S[i][j] == 0) {
          queue<pair<int, int>> Q;
          Q.push(make_pair(i, j));
          S[i][j] = p;
          while (Q.size()) {
            int row = Q.front().first;
            int col = Q.front().second;
            Q.pop();
            for (int k = 0; k < 4; k++)
              if (0 <= row + dx[k] && row + dx[k] < a && 0 <= col + dy[k] &&
                  col + dy[k] < b &&
                  str[row][col] == str[row + dx[k]][col + dy[k]] &&
                  !S[row + dx[k]][col + dy[k]]) {
                S[row + dx[k]][col + dy[k]] = p;
                Q.push(make_pair(row + dx[k], col + dy[k]));
              }
          }
          p++;
        }
      }
    }
    for (int i = 0; i < p; i++)
      for (int j = 0; j < p; j++)
        e[i][j] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        for (int k = 0; k < 1; k++)
          if (0 <= i + dx[k] && i + dx[k] < a && 0 <= j + dy[k] &&
              j + dy[k] < b && S[i][j] && S[i + dx[k]][j + dy[k]] &&
              S[i][j] != S[i + dx[k]][j + dy[k]]) {
            e[S[i][j]][S[i + dx[k]][j + dy[k]]] = 1;
          }
    for (int i = 0; i < p; i++) {
      L[i] = 100;
      R[i] = -1;
      Z[i] = -1;
      dat[i] = wolf();
    }
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (!S[i][j])
          continue;
        if (i == 0 || (S[i - 1][j] && S[i - 1][j] != S[i][j])) {
          Z[S[i][j]] = i;
          L[S[i][j]] = min(L[S[i][j]], j);
          R[S[i][j]] = max(R[S[i][j]], j);
        }
        dat[S[i][j]].v[j]++;
      }
    }
    for (int i = 0; i < b; i++)
      if (S[0][i]) {
        dfs(S[0][i], -1);
        break;
      }
    if (ret)
      printf("STABLE\\n");
    else
      printf("UNSTABLE\\n");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
using namespace std;
char str[60][11];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int S[60][11];
int e[200][200];
int L[200];
int R[200];
int Z[200];
int p;
bool ret;
struct wolf {
  int v[10];
  wolf() {
    for (int i = 0; i < 10; i++)
      v[i] = 0;
  }
};
wolf dat[200];
wolf dfs(int a, int b) {
  //	printf("%d\\n",a);
  wolf W = dat[a];
  for (int i = 0; i < p; i++)
    if (e[a][i] && i != b) {
      wolf V = dfs(i, a);
      for (int j = 0; j < 10; j++)
        W.v[j] += V.v[j];
    }
  double ave = 0;
  int div = 0;
  for (int i = 0; i < 10; i++) {
    ave += (0.5 + i) * W.v[i];
    div += W.v[i];
  }
  ave /= div;
  //	printf("%f %d %d\\n",ave,L[a],R[a]);
  if (ave < 0.00000001 + L[a] || ave > 0.9999999 + R[a])
    ret = false;
  return W;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    ret = true;
    for (int i = 0; i < a; i++) {
      scanf("%s", str[a - 1 - i]);
      for (int j = 0; j < b; j++)
        S[i][j] = 0;
    }
    p = 1;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (str[i][j] != '.' && S[i][j] == 0) {
          queue<pair<int, int>> Q;
          Q.push(make_pair(i, j));
          S[i][j] = p;
          while (Q.size()) {
            int row = Q.front().first;
            int col = Q.front().second;
            Q.pop();
            for (int k = 0; k < 4; k++)
              if (0 <= row + dx[k] && row + dx[k] < a && 0 <= col + dy[k] &&
                  col + dy[k] < b &&
                  str[row][col] == str[row + dx[k]][col + dy[k]] &&
                  !S[row + dx[k]][col + dy[k]]) {
                S[row + dx[k]][col + dy[k]] = p;
                Q.push(make_pair(row + dx[k], col + dy[k]));
              }
          }
          p++;
        }
      }
    }
    for (int i = 0; i < p; i++)
      for (int j = 0; j < p; j++)
        e[i][j] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        for (int k = 0; k < 4; k++)
          if (0 <= i + dx[k] && i + dx[k] < a && 0 <= j + dy[k] &&
              j + dy[k] < b && S[i][j] && S[i + dx[k]][j + dy[k]] &&
              S[i][j] != S[i + dx[k]][j + dy[k]]) {
            e[S[i][j]][S[i + dx[k]][j + dy[k]]] = 1;
          }
    for (int i = 0; i < p; i++) {
      L[i] = 100;
      R[i] = -1;
      Z[i] = -1;
      dat[i] = wolf();
    }
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (!S[i][j])
          continue;
        if (i == 0 || (S[i - 1][j] && S[i - 1][j] != S[i][j])) {
          Z[S[i][j]] = i;
          L[S[i][j]] = min(L[S[i][j]], j);
          R[S[i][j]] = max(R[S[i][j]], j);
        }
        dat[S[i][j]].v[j]++;
      }
    }
    for (int i = 0; i < b; i++)
      if (S[0][i]) {
        dfs(S[0][i], -1);
        break;
      }
    if (ret)
      printf("STABLE\\n");
    else
      printf("UNSTABLE\\n");
  }
}