uva127 ``Accordian'' Patience ——链表模拟题

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=63

题目大意:神牛讲的很清楚~http://blog.csdn.net/camelwombat/article/details/5949508 感觉还是首先要读懂题目意思,这个也是有难度的

题目思路:

  用数组模拟链表,考的是代码能力,关键是逻辑关系搞清楚,然后再敲,再就是细节问题,代码有一点小错就需要调很久。

参考这位神牛的思路写的……http://blog.csdn.net/goomaple/article/details/7802686

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cctype>
  6 #include <stack>
  7 #include <queue>
  8 #include <deque>
  9 #include <map>
 10 #include <set>
 11 #include <vector>
 12 #include <cmath>
 13 #include <algorithm>
 14 #define lson l, m, rt<<1
 15 #define rson m+1, r, rt<<1|1
 16 using namespace std;
 17 typedef long long int LL;
 18 const int MAXN =  0x7fffffff;
 19 const int  MINN =  -0x7fffffff;
 20 const double eps = 1e-9;
 21 const int dir[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{-1,1},
 22   {1,1},{1,-1},{-1,-1}};
 23 typedef struct node{
 24   int len; char rank[54], suit[54];
 25 }node;
 26 node card[54];
 27 void eachmove(int pos, int step) {
 28   int Len = card[pos-step].len, len = card[pos].len;
 29   card[pos-step].suit[Len] = card[pos].suit[len-1];
 30   card[pos-step].rank[Len] = card[pos].rank[len-1];
 31   card[pos-step].len++; card[pos].len--;
 32 }
 33 void moveremain(int pos) {
 34   int i, j;
 35   for (i = pos; card[i+1].len!=0; ++i) {
 36     for (j = 0; j < card[i+1].len; ++j) {
 37       card[i].suit[j] = card[i+1].suit[j];
 38       card[i].rank[j] = card[i+1].rank[j];
 39     }
 40     card[i].len = card[i+1].len;
 41   }
 42 }
 43 int solve() {
 44   int i, cnt =52, len, Len;
 45   char suit, rank;
 46   bool mrk, flag;
 47   while (1) {
 48     flag = false;
 49     for (i= 1; i < cnt;++i) {
 50       len = card[i].len; 
 51       suit = card[i].suit[len-1]; 
 52       rank = card[i].rank[len-1]; 
 53       mrk = false;
 54       if (i >= 3)  {
 55         Len = card[i-3].len;
 56         if (suit == card[i-3].suit[Len-1] || rank== \
 57             card[i-3].rank[Len-1]) {
 58           eachmove(i, 3);
 59           if (card[i].len==0) {
 60             moveremain(i);
 61             cnt--; card[cnt].len = 0;
 62           }
 63           mrk = flag = true;
 64         }
 65       }
 66       if (!mrk && i >= 1) {
 67         Len = card[i-1].len;
 68         if (suit == card[i-1].suit[Len-1] || rank== \
 69             card[i-1].rank[Len-1]) {
 70           eachmove(i, 1);
 71           if (card[i].len==0) {
 72             moveremain(i); 
 73             cnt--; card[cnt].len = 0;
 74           }
 75           flag = true;
 76         }
 77       }
 78       if (flag) break;
 79     }
 80     if (!flag) break;
 81   }
 82   return cnt;
 83 }
 84 int main(void){
 85 #ifndef ONLINE_JUDGE
 86   freopen("uva127.in", "r", stdin);
 87 #endif
 88   int i, j;
 89   while (1) {
 90     scanf("%c", &card[0].rank[0]);
 91     if (card[0].rank[0] == '#') break;
 92     scanf("%c", &card[0].suit[0]);
 93     card[0].len = 1;
 94     for (i = 1; i < 52; ++i) {
 95       getchar();
 96       scanf("%c%c", &card[i].rank[0], &card[i].suit[0]);
 97       card[i].len = 1;
 98     }
 99     card[52].len = 0;
100     int cnt = solve();
101     if (cnt > 1) printf("%d piles remaining:", cnt);
102     else printf("%d pile remaining:", cnt);
103     for (j = 0; j < cnt; ++j) printf(" %d", card[j].len);
104     printf("\n");
105     getchar();
106   }
107 
108   return 0;
109 }

这种题目,非常锻炼代码的准确度什么的……

posted on 2013-05-13 19:10  aries__liu  阅读(485)  评论(0编辑  收藏  举报