UVA 131 - The Psychic Poker Player
连接是:http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=67
又是暴力流的一个题目。
View Code
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 const int MAXLENGTH = 100, MAXCARD = 5, MAXPOSS = 32; 6 const int HIGHEST_CARD = 1, ONE_PAIR = 2, TWO_PAIRS = 3, 7 THREE_OF_A_KIND = 4, STRAIGHT = 5, FLUSH = 6, 8 FULL_HOUSE = 7, FOUR_OF_A_KIND = 8, STRAIGHT_FLUSH = 9; 9 10 const char suit[][100] = { 11 "highest-card", 12 "one-pair", 13 "two-pairs", 14 "three-of-a-kind", 15 "straight", 16 "flush", 17 "full-house", 18 "four-of-a-kind", 19 "straight-flush" 20 }; 21 22 struct Poke{ 23 int face; 24 char suit; 25 }; 26 27 void enumerate(int poss[][5], int base, int length, int level){ 28 if (!length) 29 return; 30 for (int i = base; i < base+length; i++){ 31 poss[i][level] = 0; 32 poss[i+length][level] = 1; 33 } 34 enumerate(poss, base, length/2, level+1); 35 enumerate(poss, base+length, length/2, level+1); 36 } 37 38 int cmp(const void* _a, const void* _b){ 39 Poke* a = (Poke*) _a; 40 Poke* b = (Poke*) _b; 41 return a->face - b->face; 42 } 43 44 int judge(Poke temp[]){ 45 int best_hand = HIGHEST_CARD; 46 qsort(temp, MAXCARD, sizeof(Poke), cmp); 47 48 bool flush = false, straight = false; 49 int s_inc = 1, f_inc = 1;; 50 51 int first_pair = 0, first_counter = 1, 52 second_pair = 0, second_counter = 1; 53 54 for (int i = 1; i < MAXCARD; i++){ 55 if (temp[i].suit == temp[i-1].suit) 56 f_inc++; 57 58 if (temp[i].face == temp[i-1].face){ 59 if (!first_pair){ 60 first_pair = temp[i].face; 61 first_counter++; 62 } 63 else if (first_pair == temp[i].face) 64 first_counter++; 65 else if (!second_pair){ 66 second_pair = temp[i].face; 67 second_counter++; 68 } 69 else if(second_pair == temp[i].face) 70 second_counter++; 71 } 72 else if (temp[i].face == (temp[i-1].face+1)){ 73 s_inc++; 74 } 75 } 76 if (temp[4].face == 14 && temp[0].face == 2 && temp[3].face != 13) 77 s_inc++; 78 79 if (s_inc == 5) 80 straight = true; 81 if (f_inc == 5) 82 flush = true; 83 84 if (straight){ 85 if (flush) 86 best_hand = STRAIGHT_FLUSH; 87 else 88 best_hand = STRAIGHT; 89 } 90 else if (flush){ 91 best_hand = FLUSH; 92 } 93 else{ 94 if (first_pair || second_pair) 95 best_hand = ONE_PAIR; 96 if (first_pair && second_pair) 97 best_hand = TWO_PAIRS; 98 if (first_counter == 3 || second_counter == 3) 99 best_hand = THREE_OF_A_KIND; 100 if ((first_counter + second_counter) == 5){ 101 if (first_counter == 3 || second_counter == 3) 102 best_hand = FULL_HOUSE; 103 else 104 best_hand = FOUR_OF_A_KIND; 105 } 106 } 107 108 return best_hand; 109 } 110 111 112 int main(int argc, char *argv[]){ 113 int poss[MAXPOSS][MAXCARD]; 114 enumerate(poss, 0, 16, 0); 115 116 char str[MAXLENGTH]; 117 Poke hand[MAXCARD], deck[MAXCARD]; 118 while (cin.getline(str, MAXLENGTH)){ 119 int str_length = strlen(str), h_index = 0, d_index = 0, i = 0; 120 while (h_index < 5 && i < str_length){ 121 if (isalpha(str[i])){ 122 switch(str[i]){ 123 case 'A': 124 hand[h_index].face = 14; 125 break; 126 case 'T': 127 hand[h_index].face = 10; 128 break; 129 case 'J': 130 hand[h_index].face = 11; 131 break; 132 case 'Q': 133 hand[h_index].face = 12; 134 break; 135 case 'K': 136 hand[h_index].face = 13; 137 break; 138 } 139 hand[h_index].suit = str[++i]; 140 } 141 else{ 142 hand[h_index].face = str[i] - '0'; 143 hand[h_index].suit = str[++i]; 144 } 145 h_index++; 146 i += 2; 147 } 148 149 while (d_index < 5 && i < str_length){ 150 if (isalpha(str[i])){ 151 switch(str[i]){ 152 case 'A': 153 deck[d_index].face = 14; 154 break; 155 case 'T': 156 deck[d_index].face = 10; 157 break; 158 case 'J': 159 deck[d_index].face = 11; 160 break; 161 case 'Q': 162 deck[d_index].face = 12; 163 break; 164 case 'K': 165 deck[d_index].face = 13; 166 break; 167 } 168 deck[d_index].suit = str[++i]; 169 } 170 else{ 171 deck[d_index].face = str[i] - '0'; 172 deck[d_index].suit = str[++i]; 173 } 174 d_index++; 175 i += 2; 176 } 177 178 Poke temp[MAXCARD]; 179 int best = -1; 180 for (int i = 0; i < MAXPOSS; i++){ 181 int d = 0; 182 for (int j = 0; j < MAXCARD; j++){ 183 if (poss[i][j]) 184 temp[j] = hand[j]; 185 else 186 temp[j] = deck[d++]; 187 } 188 189 int result = judge(temp); 190 if (best < result) 191 best = result; 192 } 193 cout << "Hand: "; 194 for (int i = 0; i < MAXCARD; i++) 195 cout << hand[i].face << hand[i].suit << " "; 196 197 cout << "Deck: "; 198 for (int i = 0; i < MAXCARD; i++) 199 cout << deck[i].face << deck[i].suit << " "; 200 201 cout << "Best hand: "; 202 cout << suit[best-1] << endl; 203 } 204 return 0; 205 }
这里的东西都是自娱自乐的.我只是抱着构思算法的心态去做UVA的题.
我所有的code都不可能AC的.因为我就没想着让它能AC.