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

#include <cassert>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define LMAX 10
#define HMAX 128

int now[LMAX], now_hits[HMAX], now_blows[HMAX];
bool used[10];
int L, H, hits[HMAX], blows[HMAX], T[HMAX][LMAX];
bool digs[HMAX][10];
string res;
bool cancel = false;

void dfs(int depth) {
  if (cancel)
    return;
  if (depth == L) {
    bool ng = false;
    for (int i = 0; i < H; ++i) {
      if (now_hits[i] != hits[i])
        ng = true;
      if (now_blows[i] - now_hits[i] != blows[i])
        ng = true;
    }
    if (ng)
      return;
    string s;
    for (int i = 0; i < L; ++i) {
      s += '0' + now[i];
    }
    if (res.length() > 0)
      cancel = true;
    res = s;
    return;
  }
  for (int i = 0; i < 10; ++i) {
    int thit[HMAX] = {
        0,
    };
    int tblows[HMAX] = {
        0,
    };
    bool skip = false;

    if (!used[i]) {
      used[i] = true;
      now[depth] = i;
      for (int j = 0; j < H; ++j) {
        if (T[j][depth] == i) {
          ++now_hits[j];
          ++thit[j];
        }
        if (now_hits[j] > hits[j]) {
          skip = true;
          break;
        }
      }
      for (int j = 0; j < H; ++j) {
        if (digs[j][i]) {
          ++now_blows[j];
          ++tblows[j];
        }
        if (now_blows[j] - now_hits[j] > blows[j]) {
          skip = true;
          break;
        }
      }
      if (!skip)
        dfs(depth + 1);
      for (int j = 0; j < H; ++j) {
        now_hits[j] -= thit[j];
        now_blows[j] -= tblows[j];
      }
      used[i] = false;
    }
  }
}

int main() {
  while (true) {
    cin >> L >> H;
    if (cin.eof())
      break;
    if (L == 0 && H == 0)
      break;
    for (int i = 0; i < HMAX; ++i) {
      for (int j = 0; j < 10; ++j) {
        digs[i][j] = false;
      }
    }
    for (int i = 0; i < H; ++i) {
      string s;
      cin >> s >> hits[i] >> blows[i];
      for (int j = 0; j < (int)s.length(); ++j) {
        T[i][j] = s[j] - '0';
        digs[i][s[j] - '0'] = true;
      }
    }
    for (int i = 0; i < HMAX; ++i) {
      now_hits[i] = 0;
      now_blows[i] = 0;
    }
    cancel = false;
    res = "";
    dfs(0);
    if (cancel || res.length() == 0) {
      cout << "NO" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define MAX 12

int now[MAX], now_hits[MAX], now_blows[MAX];
bool used[MAX];
int L, H, hits[MAX], blows[MAX], T[MAX][MAX];
bool digs[MAX][MAX];
string res;
bool cancel = false;

void dfs(int depth) {
  if (depth > MAX * 4)
    return;
  if (cancel)
    return;
  if (depth == L) {
    bool ng = false;
    for (int i = 0; i < H; ++i) {
      if (now_hits[i] != hits[i])
        ng = true;
      if (now_blows[i] - now_hits[i] != blows[i])
        ng = true;
    }
    if (ng)
      return;
    string s;
    for (int i = 0; i < L; ++i) {
      s += '0' + now[i];
    }
    if (res.length() > 0)
      cancel = true;
    res = s;
    return;
  }
  for (int i = 0; i < 10; ++i) {
    int thit[MAX] = {
        0,
    };
    int tblows[MAX] = {
        0,
    };
    bool skip = false;

    if (!used[i]) {
      used[i] = true;
      now[depth] = i;
      for (int j = 0; j < H; ++j) {
        if (T[j][depth] == i) {
          ++now_hits[j];
          ++thit[j];
        }
        if (now_hits[j] > hits[j]) {
          skip = true;
          break;
        }
      }
      for (int j = 0; j < H; ++j) {
        if (digs[j][i]) {
          ++now_blows[j];
          ++tblows[j];
        }
        if (now_blows[j] - now_hits[j] > blows[j]) {
          skip = true;
          break;
        }
      }
      if (!skip)
        dfs(depth + 1);
      for (int j = 0; j < H; ++j) {
        now_hits[j] -= thit[j];
        now_blows[j] -= tblows[j];
      }
      used[i] = false;
    }
  }
}

int main() {
  while (true) {
    cin >> L >> H;
    if (cin.eof())
      break;
    if (L == 0 && H == 0)
      break;
    for (int i = 0; i < MAX; ++i) {
      for (int j = 0; j < MAX; ++j) {
        digs[i][j] = false;
      }
    }
    for (int i = 0; i < H; ++i) {
      string s;
      cin >> s >> hits[i] >> blows[i];
      for (int j = 0; j < (int)s.length(); ++j) {
        T[i][j] = s[j] - '0';
        digs[i][s[j] - '0'] = true;
      }
    }
    for (int i = 0; i < MAX; ++i) {
      now_hits[i] = 0;
      now_blows[i] = 0;
    }
    cancel = false;
    res = "";
    dfs(0);
    if (cancel || res.length() == 0) {
      cout << "NO" << endl;
    } else {
      cout << res << endl;
    }
  }
  return 0;
}