Have you ever played Hanabi? If not, then you’ve got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya’s cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has).
The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.
A color hint goes like that: a player names some color and points at all the cards of this color.
Similarly goes the value hint. A player names some value and points at all the cards that contain the value.
Determine what minimum number of hints the other players should make for Borya to be certain about each card’s color and value.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of Borya’s cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card’s value (a digit from 1 to 5). Borya doesn’t know exact order of the cards they lie in.
Output
Print a single integer — the minimum number of hints that the other players should make.
Examples
input
2
G3 G3
output
0
input
4
G4 R4 R3 B3
output
2
input
5
B1 Y1 W1 G1 R1
output
4
Note
In the first sample Borya already knows for each card that it is a green three.
In the second sample we can show all fours and all red cards.
In the third sample you need to make hints about any four colors.
Solution:
#include <cstring> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <memory.h> #include <cassert> using namespace std; const int N = 12345; int c[N], num[N]; char card[N]; int mask[N]; vector < pair <int, int> > p; int main() { int n; scanf("%d", &n); p.resize(n); for (int i = 0; i < n; i++) { scanf("%s", card); c[i] = -1; num[i] = 0; if (card[0] == 'R') c[i] = 0; if (card[0] == 'G') c[i] = 1; if (card[0] == 'B') c[i] = 2; if (card[0] == 'Y') c[i] = 3; if (card[0] == 'W') c[i] = 4; num[i] = card[1] - '1'; p[i] = make_pair(c[i], num[i]); } sort(p.begin(), p.end()); p.resize(unique(p.begin(), p.end()) - p.begin()); n = p.size(); for (int i = 0; i < n; i++) { c[i] = p[i].first; num[i] = p[i].second; } int ans = 42; for (int tn = 0; tn < (1 << 5); tn++) { for (int tc = 0; tc < (1 << 5); tc++) { vector <int> hints; for (int i = 0; i < 5; i++) { if (tn & (1 << i)) { hints.push_back(i); } } for (int i = 0; i < 5; i++) { if (tc & (1 << i)) { hints.push_back(~i); } } int cnt = hints.size(); for (int i = 0; i < n; i++) { mask[i] = 0; for (int j = 0; j < cnt; j++) { if (hints[j] >= 0) { if (num[i] == hints[j]) { mask[i] |= (1 << j); } } else { if (c[i] == ~hints[j]) { mask[i] |= (1 << j); } } } } sort(mask, mask + n); bool ok = true; for (int i = 0; i < n - 1; i++) { if (mask[i] == mask[i + 1]) { ok = false; break; } } if (ok) { if (cnt < ans) { ans = cnt; } } } } printf("%d\n", ans); return 0; }