fork(1) download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4. import java.lang.*;
  5.  
  6. public class Main {
  7. private static int hash(char x) {
  8. int res;
  9.  
  10. switch (x) {
  11. case 'G' : res = 1; break;
  12. case 'B' : res = 2; break;
  13. case 'Y' : res = 3; break;
  14. case 'W' : res = 4; break;
  15. default : res = 0; break;
  16. }
  17.  
  18. return res;
  19. }
  20.  
  21. private static int popcount(int x) {
  22. int res = 0, xx = x;
  23.  
  24. while (xx > 0) {
  25. if ((xx & 1) > 0) res ++;
  26. xx /= 2;
  27. }
  28.  
  29. return res;
  30. }
  31.  
  32. public static void main(String[] args) {
  33. InputStream inputStream = System.in;
  34. OutputStream outputStream = System.out;
  35. InputReader in = new InputReader(inputStream);
  36. PrintWriter out = new PrintWriter(outputStream);
  37.  
  38. String s;
  39. int[][] st = new int[6][6];
  40. int[][] cnt = new int[6][6];
  41. int m, h = 0, v = 0, n;
  42.  
  43. n = in.nextInt();
  44.  
  45. for (int[] row : st)
  46. Arrays.fill(row, 0);
  47.  
  48. while (n > 0) {
  49. s = in.next();
  50. st[ hash( s.charAt(0) ) ][ Character.getNumericValue( s.charAt(1) ) ] = 2;
  51. n--;
  52. }
  53.  
  54. for(int i = 0; i < 5; ++i) {
  55. int meme = 0;
  56. for(int j = 0; j < 5; ++j)
  57. if (st[i][j] > 0) meme = 1;
  58. v += meme;
  59. }
  60.  
  61. for(int j = 0; j < 5; ++j) {
  62. int meme = 0;
  63. for(int i = 0; i < 5; ++i)
  64. if (st[i][j] > 0) meme = 1;
  65. h += meme;
  66. }
  67.  
  68. int ans = Integer.MAX_VALUE;
  69. for(int msk = 0; msk < (1 << 10); msk++) {
  70. for(int i = 0; i < 5; ++i)
  71. for(int j = 0; j < 5; ++j)
  72. cnt[i][j] = st[i][j];
  73.  
  74. if (v > 1) {
  75. for(int i = 0; i < 5; ++i)
  76. if ( ( (1<<i) & msk/32 ) > 0)
  77. for(int j = 0; j < 5; ++j)
  78. cnt[i][j]--;
  79. }
  80.  
  81. if (h > 1) {
  82. for(int j = 0; j < 5; ++j)
  83. if ( ( (1<<j) & msk ) > 0)
  84. for(int i = 0; i < 5; ++i)
  85. cnt[i][j]--;
  86. }
  87.  
  88. int banjir = 0;
  89.  
  90. for(int i = 0; i < 5; ++i)
  91. for(int j = 0; j < 5; ++j)
  92. if (cnt[i][j] > 1)
  93. banjir++;
  94.  
  95. boolean stat = (banjir < 2);
  96.  
  97. for(int i = 0; i < 5; ++i) {
  98. if (!stat) break;
  99. banjir = 0;
  100. for(int j = 0; j < 5; ++j)
  101. if (cnt[i][j] == 1)
  102. banjir++;
  103. stat = (banjir < 2);
  104. }
  105.  
  106. for(int j = 0; j < 5; ++j) {
  107. if (!stat) break;
  108. banjir = 0;
  109. for(int i = 0; i < 5; ++i)
  110. if (cnt[i][j] == 1)
  111. banjir++;
  112. stat = (banjir < 2);
  113. }
  114.  
  115. if (stat)
  116. ans = Math.min(ans, popcount(msk));
  117. }
  118.  
  119. out.println(ans);
  120. out.close();
  121. }
  122. }
  123.  
  124. /* Java Template */
  125. class InputReader {
  126. public BufferedReader reader;
  127. public StringTokenizer tokenizer;
  128.  
  129. public InputReader(InputStream stream) {
  130. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  131. tokenizer = null;
  132. }
  133.  
  134. public String next() {
  135. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  136. try {
  137. tokenizer = new StringTokenizer(reader.readLine());
  138. } catch (IOException e) {
  139. throw new RuntimeException(e);
  140. }
  141. }
  142. return tokenizer.nextToken();
  143. }
  144.  
  145. public int nextInt() {
  146. return Integer.parseInt(next());
  147. }
  148.  
  149. public long nextLong() {
  150. return Long.parseLong(next());
  151. }
  152. }
Success #stdin #stdout 0.08s 380480KB
stdin
5
B1 Y1 W1 G1 R1
stdout
2147483647