Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5.  
  6. //#define VERBOSE
  7.  
  8. int ans[] = {0, 3, 4, 3, 4, 1, 2, 5, 5, 1, 2};
  9. const char* sim(int a, int b) {
  10. char* resp = new char[16];
  11. if (ans[a] == ans[b]) {
  12. sprintf(resp, "MATCH\n");
  13. } else {
  14. sprintf(resp, "%d %d\n", ans[a], ans[b]);
  15. }
  16. return resp;
  17. }
  18.  
  19. int main(int argc, char* argv[]) {
  20. int N, L;
  21. int j, k;
  22. int* a;
  23. int x, y;
  24. std::map<int,int> seen;
  25. std::string str;
  26.  
  27.  
  28. std::cin >> N;
  29. L = 2*N;
  30. #ifdef VERBOSE
  31. printf("N=%d\n", N);
  32. #endif
  33.  
  34. int cards[L+1];
  35. cards[0] = 0;
  36. for (j=1; j<=L; ++j) {
  37. cards[j] = -1;
  38. }
  39.  
  40. // cheat first values
  41. if (false) {
  42. for (j=3; j<8; ++j) {
  43. cards[j] = ans[j];
  44. }
  45. }
  46.  
  47. while (true) {
  48. y = 0;
  49. // search for pairs
  50. for (j=1; j<=L; ++j) {
  51. if (cards[j] > 0) {
  52. a = &seen[cards[j]];
  53. #ifdef VERBOSE
  54. printf(" * seen[%d] = %d\n", cards[j], *a);
  55. #endif
  56. if (*a > 0) {
  57. // found pair
  58. x = *a;
  59. y = j;
  60. #ifdef VERBOSE
  61. printf("pair cards[%d]=%d, cards[%d]=%d\n",
  62. x, cards[x], y, cards[y]
  63. );
  64. #endif
  65. break;
  66. } else {
  67. *a = j;
  68. }
  69. }
  70. }
  71. seen.clear();
  72.  
  73. // search for unknowns
  74. if (y == 0) {
  75. x = 0;
  76. for (j=1; j<=L; ++j) {
  77. if (cards[j] < 0) {
  78. if (x == 0) {
  79. x = j;
  80. } else {
  81. y = j;
  82. #ifdef VERBOSE
  83. printf("pair unknowns cards[%d]=%d, cards[%d]=%d\n",
  84. x, cards[x], y, cards[y]
  85. );
  86. #endif
  87. break;
  88. }
  89. }
  90. }
  91. }
  92.  
  93. if (y == 0) {
  94. for (j=1; j<=L; ++j) {
  95. if (cards[j] > 0) {
  96. y = j;
  97. #ifdef VERBOSE
  98. printf("mixed cards[%d]=%d, cards[%d]=%d\n",
  99. x, cards[x], y, cards[y]
  100. );
  101. #endif
  102. break;
  103. }
  104. }
  105. }
  106.  
  107. if (y == 0 && x == 0) {
  108. printf("-1\n");
  109. return 0;
  110. }
  111.  
  112. std::cout << x << " " << y << std::endl;
  113. std::cout.flush();
  114.  
  115. std::cin >> str;
  116. if (str[0] == 'M') {
  117. cards[x] = 0;
  118. cards[y] = 0;
  119. } else if (str[0] == '-') {
  120. return 0;
  121. } else {
  122. if (1 != sscanf(str.c_str(), "%d", &j)) {
  123. printf("real bad\n");
  124. return 0;
  125. } else {
  126. std::cin >> k;
  127. cards[x] = j;
  128. cards[y] = k;
  129. }
  130. }
  131.  
  132. //delete resp;
  133.  
  134. #ifdef VERBOSE
  135. printf("[ ");
  136. for (j=1; j<=L; ++j) {
  137. printf("%d ", cards[j]);
  138. }
  139. printf("]\n\n");
  140. #endif
  141. }
  142.  
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement