#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[1000];
void compare(int x, int y, int z) {
for (int i = 123; i <= 987; i++) {
if (ch[i]) {
int s = 0, b = 0;
int arr1[3] = { i / 100, (i % 100) / 10 ,i % 10 };
int arr2[3] = { x / 100, (x % 100) / 10 ,x % 10 };
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if(j==k && (arr1[j]==arr2[k])){
s++;
}
else if (j != k && arr1[j] == arr2[k]) {
b++;
}
}
}
//후보에서 제외
if (!(y == s && z == b)) {
ch[i] = 0;
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
//ch배열 초기화
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (i != j && k != j && i != k) {
ch[i * 100 + 10 * j + k] = 1;
}
}
}
}
for (int i = 0; i < n; i++) {
int x, y, z;
cin >> x >> y >> z;
compare(x, y, z);
}
int ans = 0;
for (int i = 0; i < 1000; i++) {
if (ch[i]) ans++;
}
cout << ans << '\n';
return 0;
}
두번째 방법
#include <bits/stdc++.h>
using namespace std;
int n;
bool ch[101][1000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
int a, s, b;
cin >> a >> s >> b;
int x = a / 100;
int y = (a % 100) / 10;
int z = a % 10;
if (s == 3 && b == 0) {
cout << 1 << '\n';
return 0;
}
else if (s == 0 && b == 3) {
ch[i][z * 100 + x * 10 + y] = 1;
ch[i][y * 100 + z * 10 + x] = 1;
}
else if (s == 1 && b == 2) {
ch[i][100 * x + 10 * z + y] = 1;
ch[i][100 * z + 10 * y + x] = 1;
ch[i][100 * y + 10 * x + z] = 1;
}
else if (s == 2 && b == 0) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * x + 10 * y + j] = 1;
ch[i][100 * x + 10 * j + z] = 1;
ch[i][100 * j + 10 * y + z] = 1;
}
}
}
else if (s == 1 && b == 1) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * x + 10 * j + y] = 1;
ch[i][100 * x + 10 * z + j] = 1;
ch[i][100 * z + 10 * y + j] = 1;
ch[i][100 * j + 10 * y + x] = 1;
ch[i][100 * j + 10 * x + z] = 1;
ch[i][100 * y + 10 * j + z] = 1;
}
}
}
else if (s == 0 && b == 2) {
for (int j = 1; j <= 9; j++) {
if (j != z && j != x && j != y) {
ch[i][100 * j + 10 * x + y] = 1;
ch[i][100 * y + 10 * x + j] = 1;
ch[i][100 * y + 10 * j + x] = 1;
ch[i][100 * z + 10 * j + x] = 1;
ch[i][100 * z + 10 * x + j] = 1;
ch[i][100 * j + 10 * z + x] = 1;
ch[i][100 * z + 10 * j + y] = 1;
ch[i][100 * y + 10 * z + j] = 1;
ch[i][100 * j + 10 * z + y] = 1;
}
}
}
else if (s == 1 && b == 0) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z) {
ch[i][100 * x + 10 * j + k] = 1;
ch[i][100 * j + 10 * y + k] = 1;
ch[i][100 * j + 10 * k + z] = 1;
}
}
}
}
else if (s==0 && b == 1) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z) {
ch[i][100 * j + 10 * x + k] = 1;
ch[i][100 * j + 10 * k + x] = 1;
ch[i][100 * y + 10 * j + k] = 1;
ch[i][100 * j + 10 * k + y] = 1;
ch[i][100 * z + 10 * j + k] = 1;
ch[i][100 * j + 10 * z + k] = 1;
}
}
}
}
else if (s == 0 && b == 0) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int t = 1; t <= 9; t++) {
if (j != z && j != x && j != y && j != k && k != x && k != y && k != z
&& x != t && y != t && z != t && k != t && j != t) {
ch[i][100 * j + 10 * t + k] = 1;
}
}
}
}
}
}
int ans = 0;
for (int i = 100; i < 1000; i++) {
bool flag = true;
for (int j = 0; j < n; j++) {
if (ch[j][i] == 0) {
flag = false;
break;
}
}
if (flag) ans++;
}
cout << ans << '\n';
return 0;
}