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

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct Point {
  int x, y;
};
int w, h;
string s[60];
bool check[60][10];

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

bool stable(const int x, const int y, const char c, int *Msum, int *bSum) {
  int M = 2;
  int blockNum = 4;
  int XL = 100, XR = -100;
  queue<Point> q;
  q.push(Point{x, y});
  while (!q.empty()) {
    Point p = q.front();
    q.pop();
    if (check[p.y][p.x])
      continue;
    check[p.y][p.x] = true;
    M += p.x;
    if (p.y == h - 1 || s[p.y + 1][p.x] != '.' && s[p.y + 1][p.x] != c) {
      XL = min(XL, p.x);
      XR = max(XR, p.x + 1);
    }
    if (p.y != 0 && !check[p.y - 1][p.x] && s[p.y - 1][p.x] != '.' &&
        s[p.y - 1][p.x] != c) {
      if (!stable(p.x, p.y - 1, s[p.y - 1][p.x], &M, &blockNum))
        return false;
    }
    for (int k = 0; k < 4; k++) {
      int nx = p.x + dx[k], ny = p.y + dy[k];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (check[ny][nx])
        continue;
      if (s[ny][nx] != c)
        continue;
      q.push(Point{nx, ny});
    }
  }

  *Msum += M;
  *bSum += blockNum;
  return XL < double(M) / blockNum && double(M) / blockNum < XR;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> w >> h, w | h) {
    for (int i = 0; i < h; i++) {
      cin >> s[i];
    }

    int m, b;
    memset(check, false, sizeof check);
    for (int j = 0; j < w; j++) {
      if (s[h - 1][j] != '.') {
        bool res = stable(j, h - 1, s[h - 1][j], &m, &b);
        if (res) {
          cout << "STABLE" << endl;
        } else {
          cout << "UNSTABLE" << endl;
        }
        break;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct Point {
  int x, y;
};
int w, h;
string s[10];
int b[60][10];
bool check[60][10];

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

bool stable(const int x, const int y, const char c, int *Msum, int *bSum) {
  int M = 2;
  int blockNum = 4;
  int XL = 100, XR = 0;
  queue<Point> q;
  q.push(Point{x, y});
  while (!q.empty()) {
    Point p = q.front();
    q.pop();
    if (check[p.y][p.x])
      continue;
    check[p.y][p.x] = true;
    M += p.x;
    if (p.y == h - 1 || s[p.y + 1][p.x] != '.' && s[p.y + 1][p.x] != c) {
      XL = min(XL, p.x);
      XR = max(XR, p.x + 1);
    }
    if (p.y != 0 && !check[p.y - 1][p.x] && s[p.y - 1][p.x] != '.' &&
        s[p.y - 1][p.x] != c) {
      if (!stable(p.x, p.y - 1, s[p.y - 1][p.x], &M, &blockNum))
        return false;
    }
    for (int k = 0; k < 4; k++) {
      int nx = p.x + dx[k], ny = p.y + dy[k];
      if (nx < 0 || w <= nx || ny < 0 || h <= ny)
        continue;
      if (check[ny][nx])
        continue;
      if (s[ny][nx] != c)
        continue;
      q.push(Point{nx, ny});
    }
  }

  *Msum += M;
  *bSum += blockNum;
  return XL < double(M) / blockNum && double(M) / blockNum < XR;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> w >> h, w | h) {
    for (int i = 0; i < h; i++) {
      cin >> s[i];
    }
    int m, b;
    memset(check, false, sizeof check);
    for (int j = 0; j < w; j++) {
      if (s[h - 1][j] != '.' && !check[h - 1][j]) {
        bool res = stable(j, h - 1, s[h - 1][j], &m, &b);
        if (res) {
          cout << "STABLE" << endl;
        } else {
          cout << "UNSTABLE" << endl;
        }
      }
    }
  }
}