Source code for submission s701

rhino.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int RankToInt(char c)
  5. {
  6. switch(c)
  7. {
  8. case 'A': return 12;
  9. case 'K': return 11;
  10. case 'Q': return 10;
  11. case 'J': return 9;
  12. case 'X': return 8;
  13. default: return c - '2';
  14. }
  15. }
  16.  
  17. int SuitToInt(char c)
  18. {
  19. switch(c)
  20. {
  21. case 'h': return 0;
  22. case 'd': return 13;
  23. case 's': return 26;
  24. default: return 39;
  25. }
  26. }
  27.  
  28. int Make(int a, int q = 0, int w = 0, int e = 0, int r = 0, int t = 0)
  29. {
  30. return (a << 20)
  31. | (q << 16)
  32. | (w << 12)
  33. | (e << 8)
  34. | (r << 4)
  35. | (t << 0);
  36. }
  37.  
  38. int FindRank(int* r, int a, int prev = 13)
  39. {
  40. for(int i = prev - 1; 1; i--)
  41. {
  42. if(r[i] == a)
  43. {
  44. return i;
  45. }
  46. }
  47. }
  48.  
  49. int Assess(int a[5])
  50. {
  51. int r[13], rr[13];
  52. for(int i = 0; i < 13; i++)
  53. {
  54. r[i] = 0;
  55. rr[i] = 0;
  56. }
  57.  
  58. for(int i = 0; i < 5; i++)
  59. {
  60. r[a[i] % 13]++;
  61. }
  62.  
  63. for(int i = 0; i < 13; i++)
  64. {
  65. rr[r[i]]++;
  66. }
  67.  
  68. int flush = 1;
  69. for(int i = 1; i < 5; i++)
  70. {
  71. if(a[i] / 13 != a[0] / 13)
  72. {
  73. flush = 0;
  74. break;
  75. }
  76. }
  77.  
  78. int q, w, e, u, t;
  79. for(int i = 12; i >= 3; i--)
  80. {
  81. int ok = 1;
  82. for(int j = 9; j <= 13; j++)
  83. {
  84. if(!(r[(i + j) % 13]))
  85. {
  86. ok = 0;
  87. break;
  88. }
  89. }
  90.  
  91. if(ok)
  92. {
  93. return Make((flush) ? 8 : 4, i);
  94. }
  95. }
  96.  
  97.  
  98. if(rr[4])
  99. {
  100. q = FindRank(r, 4);
  101. w = FindRank(r, 1);
  102. return Make(7, q, w);
  103. }
  104.  
  105. if((rr[3]) && (rr[2]))
  106. {
  107. q = FindRank(r, 3);
  108. w = FindRank(r, 2);
  109. return Make(6, q, w);
  110. }
  111.  
  112. if(rr[3])
  113. {
  114. q = FindRank(r, 3);
  115. w = FindRank(r, 1);
  116. e = FindRank(r, 1, w);
  117. return Make(3, q, w, e);
  118. }
  119.  
  120. if(rr[2] == 2)
  121. {
  122. q = FindRank(r, 2);
  123. w = FindRank(r, 2, q);
  124. e = FindRank(r, 1);
  125. return Make(2, q, w, e);
  126. }
  127.  
  128. if(rr[2])
  129. {
  130. q = FindRank(r, 2);
  131. w = FindRank(r, 1);
  132. e = FindRank(r, 1, w);
  133. t = FindRank(r, 1, e);
  134. return Make(1, q, w, e, t);
  135. }
  136.  
  137. q = FindRank(r, 1);
  138. w = FindRank(r, 1, q);
  139. e = FindRank(r, 1, w);
  140. t = FindRank(r, 1, e);
  141. u = FindRank(r, 1, t);
  142.  
  143. return Make((flush) ? 5 : 0, q, w, e, t, u);
  144. }
  145.  
  146. int Player[20][2];
  147. int Table[5];
  148. char s[10];
  149. int Septuple[20][7];
  150. int a[5];
  151. int choose[5];
  152. int Value[20];
  153.  
  154. int Next()
  155. {
  156. int i;
  157. for(i = 4; i >= 0; i--)
  158. {
  159. choose[i]++;
  160. if(choose[i] < 3 + i)
  161. {
  162. break;
  163. }
  164. }
  165.  
  166. if(i == -1)
  167. {
  168. return 0;
  169. }
  170.  
  171. for(i++; i < 5; i++)
  172. {
  173. choose[i] = choose[i - 1] + 1;
  174. }
  175.  
  176. return 1;
  177. }
  178.  
  179. int main()
  180. {
  181. int n;
  182. while(scanf("%d", &n) > 0)
  183. {
  184. for(int i = 0; i < 5; i++)
  185. {
  186. scanf("%s", s);
  187. Table[i] = RankToInt(s[0]) + SuitToInt(s[1]);
  188. }
  189.  
  190. for(int i = 0; i < n; i++)
  191. {
  192. for(int j = 0; j < 2; j++)
  193. {
  194. scanf("%s", s);
  195. Player[i][j] = RankToInt(s[0]) + SuitToInt(s[1]);
  196. }
  197. }
  198.  
  199. for(int i = 0; i < n; i++)
  200. {
  201. for(int j = 0; j < 5; j++)
  202. {
  203. Septuple[i][j] = Table[j];
  204. }
  205.  
  206. Septuple[i][5] = Player[i][0];
  207. Septuple[i][6] = Player[i][1];
  208. }
  209.  
  210. /*for(int i = 0; i < 5; i++)
  211. {
  212. printf("%d ", Table[i]);
  213. }
  214.  
  215. printf("\n");
  216.  
  217. for(int i = 0; i < n; i++)
  218. {
  219. for(int j = 0; j < 2; j++)
  220. {
  221. printf("%d ", Player[i][j]);
  222. }
  223.  
  224. printf("\n");
  225. }*/
  226.  
  227. for(int i = 0; i < 5; i++)
  228. {
  229. choose[i] = i;
  230. }
  231.  
  232. while(1)
  233. {
  234. /*for(int i = 0; i < 5; i++)
  235. {
  236. printf("%d ", choose[i]);
  237. }
  238. printf("\n");*/
  239.  
  240. for(int i = 0; i < n; i++)
  241. {
  242. for(int j = 0; j < 5; j++)
  243. {
  244. a[j] = Septuple[i][choose[j]];
  245. }
  246.  
  247. int as = Assess(a);
  248. if(as > Value[i])
  249. {
  250. Value[i] = as;
  251. }
  252. }
  253.  
  254. if(!(Next()))
  255. {
  256. break;
  257. }
  258. }
  259.  
  260. int max = 0;
  261. for(int i = 0; i < n; i++)
  262. {
  263. if(Value[i] > max)
  264. {
  265. max = Value[i];
  266. }
  267. }
  268.  
  269. int space = 0;
  270. for(int i = 0; i < n; i++)
  271. {
  272. if(Value[i] == max)
  273. {
  274. printf("%s%d", (space) ? " " : (space = 1, ""), i + 1);
  275. }
  276. }
  277.  
  278. printf("\n");
  279. }
  280.  
  281. return 0;
  282. }
  283.