2012年9月12日

UVa 127 - "Accordian" Patience


#include <cstdio>
#include <vector>
using std::vector;

class Card {
public:
  Card(char suit, char value) {
    this -> suit = suit;
    this -> value = value;
  }
  char suit;
  char value;
};

int match(Card a, Card b) {
  if (a.suit == b.suit) return 1;
  if (a.value == b.value) return 1;
  return 0;
}

int main() {
  char cmd;
  while ((cmd = getchar()) && cmd != '#') {
    vector<vector<Card> > pile(52);
    for (int i = 0; i < 52; i++) {
      char value, suit;
      if (!i) value = cmd;
      else value = getchar();
      suit = getchar();
      pile[i].push_back(Card(suit, value));
      getchar();
    }
    for (int i = 1; i < pile.size(); i++) {
      while ((i && match(pile[i].back(), pile[i - 1].back())) || (i > 2 && match(pile[i].back(), pile[i - 3].back()))) {
        if (i > 2 && match(pile[i].back(), pile[i - 3].back())) pile[i - 3].push_back(pile[i].back());
        else pile[i - 1].push_back(pile[i].back());
        pile[i].pop_back();
        if (!pile[i].size()) pile.erase(pile.begin() + i);
        i = 0;
      }
    }
    if (pile.size() == 1) {
      puts("1 pile remaining: 52");
      continue;
    }
    printf("%d piles remaining:", pile.size());
    for (int i = 0; i < pile.size(); i++)
      printf(" %d", pile[i].size());
    puts("");
  }
  return 0;
}

沒有留言:

張貼留言