2013年8月12日

UVa 131 - The Psychic Poker Player

#include <cstdio>
#include <string>
using namespace std;

const string best[9] = {"straight-flush", "four-of-a-kind", "full-house", "flush", "straight", "three-of-a-kind", "two-pairs", "one-pair", "highest-card"};
char hand[9][9], deck[9][9];
int bestType;

void enumerate(int now, bool * used) {
  if (now == 5) {
    char face[5], suit[5];
    int newL = 0;
    for (int i = 0; i < 5; i++) {
      if (used[i]) {
        face[newL] = hand[i][0];
        suit[newL++] = hand[i][1];
      }
    }
    for (int i = 0; newL < 5; i++, newL++) {
      face[newL] = deck[i][0];
      suit[newL] = deck[i][1];
    }
    int cnt[13] = {};
    for (int i = 0; i < 5; i++) {
      switch (face[i]) {
        case 'A':
          cnt[0]++;
          break;
        case 'T':
          cnt[9]++;
          break;
        case 'J':
          cnt[10]++;
          break;
        case 'Q':
          cnt[11]++;
          break;
        case 'K':
          cnt[12]++;
          break;
        default:
          cnt[face[i] - '1']++;
      }
    }
    bool flush = true;
    for (int i = 1; i < 5; i++) {
      if (suit[i] != suit[0]) {
        flush = false;
      }
    }
    bool straight = false;
    for (int i = 0; i < 10; i++) {
      int num = 0;
      for (int j = 0; j < 5; j++) {
        num += (cnt[(i + j) % 13] > 0);
      }
      if (num == 5) {
        straight = true;
        break;
      }
    }
    bool four = false, three = false;
    int pairs = 0;
    for (int i = 0; i < 13; i++) {
      if (cnt[i] == 4) {
        four = true;
      }
      if (cnt[i] == 3) {
        three = true;
      }
      pairs += cnt[i] == 2;
    }
    if (flush && straight) {
      bestType = 0;
    } else if (four) {
      bestType = 1 < bestType ? 1 : bestType;
    } else if (three && pairs) {
      bestType = 2 < bestType ? 2 : bestType;
    } else if (flush) {
      bestType = 3 < bestType ? 3 : bestType;
    } else if (straight) {
      bestType = 4 < bestType ? 4 : bestType;
    } else if (three) {
      bestType = 5 < bestType ? 5 : bestType;
    } else if (pairs >= 2) {
      bestType = 6 < bestType ? 6 : bestType;
    } else if (pairs) {
      bestType = 7 < bestType ? 7 : bestType;
    }
    return;
  }
  used[now] = true;
  enumerate(now + 1, used);
  used[now] = false;
  enumerate(now + 1, used);
}

int main() {
  while (scanf("%s", hand[0]) == 1) {
    for (int i = 1; i < 5; i++) {
      scanf("%s", hand[i]);
    }
    for (int i = 0; i < 5; i++) {
      scanf("%s", deck[i]);
    }
    bool used[9] = {};
    bestType = 8;
    enumerate(0, used);
    printf("Hand: ");
    for (int i = 0; i < 5; i++) {
      printf("%s ", hand[i]);
    }
    printf("Deck: ");
    for (int i = 0; i < 5; i++) {
      printf("%s ", deck[i]);
    }
    printf("Best hand: %s\n", best[bestType].c_str());
  }
  return 0;
}

沒有留言:

張貼留言