Advertisement
shiogem

uva 127

Jul 4th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. /* UVa 127 : "Accordion" Patience - RESOLU LE 2010-08-11 02:29:41 (0.356s)
  2. * Méthode : Manipulation of table de piles
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. struct pile {
  9.     int n;
  10.     char cards[52][3];
  11. };
  12.  
  13. int num_piles;
  14. struct pile piles[52];
  15.  
  16. int
  17. match(int a, int b) {
  18.     if ((piles[a].cards[piles[a].n-1][0] == piles[b].cards[piles[b].n-1][0]) ||
  19.             (piles[a].cards[piles[a].n-1][1] == piles[b].cards[piles[b].n-1][1])) {
  20.         return 1;
  21.     } else {
  22.         return 0;
  23.     }
  24. }
  25.  
  26. void
  27. move(int orig, int dest) {
  28.     strcpy(piles[dest].cards[piles[dest].n], piles[orig].cards[piles[orig].n-1]);
  29.     piles[dest].n++;
  30.     piles[orig].n--;
  31.     if (!piles[orig].n) {
  32.         memmove(&piles[orig], &piles[orig+1], (num_piles-orig-1)*sizeof(struct pile));
  33.         num_piles--;
  34.     }
  35. }
  36.  
  37. void
  38. play(void) {
  39.     int i;
  40.  
  41. again:
  42.     for (i=0; i<num_piles; i++) {
  43.         if (i>=3 && match(i, i-3)) {
  44.             move(i, i-3);
  45.             goto again;
  46.         } else if (i>=1 && match(i, i-1)) {
  47.             move(i, i-1);
  48.             goto again;
  49.         }
  50.     }
  51. }
  52.  
  53. int
  54. main(void) {
  55.     int i;
  56.  
  57.     while (1) {
  58.         num_piles = 52;
  59.         for (i=0; i<52; i++) {
  60.             piles[i].n = 1;
  61.             if (scanf(" %s", piles[i].cards[0])!=1 || piles[i].cards[0][0]=='#') {
  62.                 return 0;
  63.             }
  64.         }
  65.         play();
  66.         printf("%d pile%s remaining:", num_piles, (num_piles>1 ? "s" : ""));
  67.         for (i=0; i<num_piles; i++) {
  68.             printf(" %d", piles[i].n);
  69.         }
  70.         printf("\n");
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement