viernes, 21 de octubre de 2011

127 - Accordian Patience - UVA

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#define db(a) cout << #a << " = " << a << endl;
#define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl;
using namespace std;
int total = 0;
struct carta {
 char *kind;
 carta(char *kind) : kind(kind) {}
};
void move(vector< stack<carta> >& cartas,const int& target,const int& source) {
 cartas[target].push(cartas[source].top());
 cartas[source].pop();
 if(cartas[source].empty()) total--, cartas.erase(cartas.begin() + source);
 
}
bool can(vector< stack<carta> >& cartas,const int& target,const int& source) {
 return cartas[target].top().kind[0] == cartas[source].top().kind[0] ||
 cartas[target].top().kind[1] == cartas[source].top().kind[1];
}
void jugar(vector< stack<carta> >& cartas) {
int i = 0;
again:
 for (; i < total; i++) {
  if (i >= 3 && can(cartas, i - 3, i)) {
   move(cartas, i - 3, i);
   i = i - 3;
   goto again;
  }
  else if( i >= 1 && can(cartas, i - 1, i)) {
   move(cartas, i - 1, i); 
   i = i - 1;
   goto again;
  }
 }
}
int main() {
 char line[200], line2[200];
 bool fin = false;
 while(1) {
  total = 52;
  vector< stack<carta> > cartas(52);
  gets(line);
  if(line[0] == '#') break;
  char *st, sep[] = " ";
  st = strtok(line, sep);
  int i = 0;
  while (st) {
   carta card(st);
   cartas[i++].push(card);
   st = strtok(0, sep);
  }
  gets(line2);
  st = strtok(line2, sep);
  while (st) {
   carta card(st);
   cartas[i++].push(card);
   st = strtok(0, sep);
  }
  jugar(cartas);
  printf("%d pile%s remaining:", total, (total > 1) ? "s" : "");
  for (int i = 0; i < total; i++) {
   if(!cartas[i].empty()) printf(" %d", cartas[i].size());
  }
  printf("\n");
 }
 return 0;
}

No hay comentarios:

Publicar un comentario