2013年8月1日

UVa 170 - Clock Patience

Problem statement is hard to understand.
#include <cstdio>
#include <deque>
#include <string>
using namespace std;

int toIndex(char c) {
  if (c == 'A') return 0;
  if (c >= '2' && c <= '9') return c - '1';
  if (c == 'T') return 9;
  if (c == 'J') return 10;
  if (c == 'Q') return 11;
  if (c == 'K') return 12;
}

struct Card {
  Card (const string & s) : s(s), open(false) {}
  string s;
  bool open;
};

int main() {
  char s[2];
  while (scanf("%s", s) && s[0] != '#') {
    deque<Card> pile[13];
    pile[12].push_front(Card(s));
    for (int i = 0; i < 4; i++) {
      for (int j = 12 - !i; j >= 0; j--) {
        scanf("%s", s);
        pile[j].push_front(Card(s));
      }
    }
    int select = 12, count = 0;
    string last;
    while (!pile[select].back().open) {
      Card now = pile[select].back();
      pile[select].pop_back();
      last = now.s;
      count++;
      now.open = true;
      select = toIndex(now.s[0]);
      pile[select].push_front(now);
    }
    printf("%02d,%s\n", count, last.c_str());
  }
  return 0;
}

沒有留言:

張貼留言