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

#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
using namespace std;

#define INF 100000000
typedef pair<string, int> P;

int x, y;
string f;
char c, alph[] = {'R', 'G', 'B'};
int ans;
int r, g, b;
map<string, int> memo;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
bool vis[10][10];

void color(string &s, int h, int w, char lu, char nxt) {
  s[h * x + w] = nxt;
  for (int i = 0; i < 4; i++) {
    int ty = h + dy[i], tx = w + dx[i];
    if (ty < 0 || tx < 0 || y <= ty || x <= tx)
      continue;
    if (s[ty * x + tx] != lu)
      continue;
    color(s, ty, tx, lu, nxt);
  }
}

int count(string s, int h, int w, char lu) {
  if (vis[h][w])
    return 0;
  vis[h][w] = true;
  int res = 1;
  for (int i = 0; i < 4; i++) {
    int ty = h + dy[i], tx = w + dx[i];
    if (ty < 0 || tx < 0 || y <= ty || x <= tx)
      continue;
    if (s[ty * x + tx] != lu)
      continue;
    res += count(s, ty, tx, lu);
  }
  return res;
}

int rec(string field, int depth) {
  if (ans < depth)
    return INF;
  if (memo.find(field) != memo.end()) {
    ans = min(ans, depth + memo[field]);
    return memo[field];
  }

  for (int j = 0; j < y; j++)
    for (int k = 0; k < x; k++)
      vis[j][k] = false;
  int mount = count(field, 0, 0, field[0]);
  if (mount == y * x) {
    ans = min(ans, depth);
    return memo[field] = 0;
  }

  string G[2];
  int num[2], cnt = 0;
  for (int i = 0; i < 3; i++) {
    G[cnt] = field;
    if (alph[i] != G[cnt][0]) {
      color(G[cnt], 0, 0, G[cnt][0], alph[i]);
      for (int j = 0; j < y; j++)
        for (int k = 0; k < x; k++)
          vis[j][k] = false;
      num[cnt] = count(G[cnt], 0, 0, alph[i]);
      cnt++;
    }
    if (cnt >= 2)
      break;
  }

  if (num[0] < num[1]) {
    swap(num[0], num[1]);
    swap(G[0], G[1]);
  }

  if (num[0] == y * x) {
    ans = min(ans, depth + 1);
    return memo[field] = 1;
  } else {
    int tmp = rec(G[0], depth + 1);
    if (mount != num[1])
      tmp = min(tmp, rec(G[1], depth + 1));
    ans = min(ans, depth + 1 + tmp);
    return memo[field] = tmp + 1;
  }
}

int main() {
  while (cin >> x >> y, x || y) {
    f = "";
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        cin >> c;
        f += c;
      }
    }

    memo.clear();
    ans = INF;
    cout << rec(f, 0) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stack>
using namespace std;

#define INF 100000000
typedef pair<string, int> P;

int x, y;
string f;
char c, alph[] = {'R', 'G', 'B'};
int ans;
int r, g, b;
map<string, int> memo;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
bool vis[4][4];

void color(string &s, int h, int w, char lu, char nxt) {
  s[h * x + w] = nxt;
  for (int i = 0; i < 4; i++) {
    int ty = h + dy[i], tx = w + dx[i];
    if (ty < 0 || tx < 0 || y <= ty || x <= tx)
      continue;
    if (s[ty * x + tx] != lu)
      continue;
    color(s, ty, tx, lu, nxt);
  }
}

int count(string s, int h, int w, char lu) {
  if (vis[h][w])
    return 0;
  vis[h][w] = true;
  int res = 1;
  for (int i = 0; i < 4; i++) {
    int ty = h + dy[i], tx = w + dx[i];
    if (ty < 0 || tx < 0 || y <= ty || x <= tx)
      continue;
    if (s[ty * x + tx] != lu)
      continue;
    res += count(s, ty, tx, lu);
  }
  return res;
}

int rec(string field, int depth) {
  if (ans < depth)
    return INF;
  if (memo.find(field) != memo.end())
    return memo[field];

  for (int j = 0; j < y; j++)
    for (int k = 0; k < x; k++)
      vis[j][k] = false;
  int mount = count(field, 0, 0, field[0]);
  if (mount == y * x)
    return memo[field] = 0;

  string G[2];
  int num[2], cnt = 0;
  for (int i = 0; i < 3; i++) {
    G[cnt] = field;
    if (alph[i] != G[cnt][0]) {
      color(G[cnt], 0, 0, G[cnt][0], alph[i]);
      for (int j = 0; j < y; j++)
        for (int k = 0; k < x; k++)
          vis[j][k] = false;
      num[cnt] = count(G[cnt], 0, 0, alph[i]);
      cnt++;
    }
    if (cnt >= 2)
      break;
  }

  if (num[0] < num[1]) {
    swap(num[0], num[1]);
    swap(G[0], G[1]);
  }

  if (num[0] == y * x)
    return memo[field] = 1;
  else {
    int tmp = rec(G[0], depth + 1);
    if (mount != num[1])
      tmp = min(tmp, rec(G[1], depth + 1));
    return memo[field] = tmp + 1;
  }
}

int main() {
  while (cin >> x >> y, x || y) {
    f = "";
    for (int i = 0; i < y; i++) {
      for (int j = 0; j < x; j++) {
        cin >> c;
        f += c;
      }
    }

    memo.clear();
    ans = INF;
    cout << rec(f, 0) << endl;
  }
}