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

#include <cstdio>
#include <cstring>
#include <iostream>

#define Maxn 100010

using namespace std;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

int n, A, B, C, type[Maxn], step[Maxn];
char ch[2];
bool vis[10][10][10][Maxn], flag;

void output() {
  cout << "Yes" << endl;
  for (int i = 1; i <= n; ++i) {
    if (step[i] == 1)
      printf("A\\n");
    if (step[i] == 2)
      printf("B\\n");
    if (step[i] == 3)
      printf("C\\n");
  }
}

void dfs(int a, int b, int c, int x) {
  // cout << a << " " << b << " " << c << " " << x << endl;
  if (vis[a][b][c][x] || flag)
    return;
  if (x == n + 1) {
    flag = 1;
    output();
    return;
  }
  if (type[x] == 1) {
    if (a > 0)
      step[x] = 2, dfs(a - 1, b + 1, c, x + 1);
    if (b > 0)
      step[x] = 1, dfs(a + 1, b - 1, c, x + 1);
    if (!a && !b || !flag)
      vis[a][b][c][x] = 1;
  }
  if (type[x] == 2) {
    if (a > 0)
      step[x] = 3, dfs(a - 1, b, c + 1, x + 1);
    if (c > 0)
      step[x] = 1, dfs(a + 1, b, c - 1, x + 1);
    if (!a && !c || !flag)
      vis[a][b][c][x] = 1;
  }
  if (type[x] == 3) {
    if (b > 0)
      step[x] = 3, dfs(a, b - 1, c + 1, x + 1);
    if (c > 0)
      step[x] = 2, dfs(a, b + 1, c - 1, x + 1);
    if (!b && !c || !flag)
      vis[a][b][c][x] = 1;
  }
}

int main() {
  n = read();
  A = read();
  B = read();
  C = read();
  if (A > 3)
    A = 3;
  if (B > 3)
    B = 3;
  if (C > 3)
    C = 3;
  if (!A && !B && !C) {
    cout << "No" << endl;
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    scanf(" %s", ch + 1);
    if (ch[1] == 'A' && ch[2] == 'B')
      type[i] = 1;
    if (ch[1] == 'A' && ch[2] == 'C')
      type[i] = 2;
    if (ch[1] == 'B' && ch[2] == 'C')
      type[i] = 3;
  }
  dfs(A, B, C, 1);
  if (!flag)
    cout << "No" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

#define Maxn 100010

using namespace std;

inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

int n, A, B, C, type[Maxn], step[Maxn];
char ch[2];
bool vis[7][7][7][Maxn], flag;

void output() {
  cout << "Yes" << endl;
  for (int i = 1; i <= n; ++i) {
    if (step[i] == 1)
      printf("A\\n");
    if (step[i] == 2)
      printf("B\\n");
    if (step[i] == 3)
      printf("C\\n");
  }
}

void dfs(int a, int b, int c, int x) {
  // cout << a << " " << b << " " << c << " " << x << endl;
  if (vis[a][b][c][x] || flag)
    return;
  if (x == n + 1) {
    flag = 1;
    output();
    return;
  }
  if (type[x] == 1) {
    if (a > 0)
      step[x] = 2, dfs(a - 1, b + 1, c, x + 1);
    if (b > 0)
      step[x] = 1, dfs(a + 1, b - 1, c, x + 1);
    if (!a && !b || !flag)
      vis[a][b][c][x] = 1;
  }
  if (type[x] == 2) {
    if (a > 0)
      step[x] = 3, dfs(a - 1, b, c + 1, x + 1);
    if (c > 0)
      step[x] = 1, dfs(a + 1, b, c - 1, x + 1);
    if (!a && !c || !flag)
      vis[a][b][c][x] = 1;
  }
  if (type[x] == 3) {
    if (b > 0)
      step[x] = 3, dfs(a, b - 1, c + 1, x + 1);
    if (c > 0)
      step[x] = 2, dfs(a, b + 1, c - 1, x + 1);
    if (!b && !c || !flag)
      vis[a][b][c][x] = 1;
  }
}

int main() {
  n = read();
  A = read();
  B = read();
  C = read();
  if (A > 6)
    A = 6;
  if (B > 6)
    B = 6;
  if (C > 6)
    C = 6;
  if (!A && !B && !C) {
    cout << "No" << endl;
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    scanf(" %s", ch + 1);
    if (ch[1] == 'A' && ch[2] == 'B')
      type[i] = 1;
    if (ch[1] == 'A' && ch[2] == 'C')
      type[i] = 2;
    if (ch[1] == 'B' && ch[2] == 'C')
      type[i] = 3;
  }
  dfs(A, B, C, 1);
  if (!flag)
    cout << "No" << endl;
  return 0;
}