127 - "Accordian" Patience

 1 #include<stdio.h>
 2 typedef struct CARD
 3 {
 4 char rank;
 5 char suit;        
 6 }CARD;
 7 bool move(CARD card[][52],int top[],int *position)
 8 {
 9     int index=*position;
10     if(top[index]==-1)
11         return false;
12     int temp=index;
13     int count=0;
14     while(count!=3&&temp>=1)
15     if(top[--temp]!=-1)
16         count++;
17      if(count==3)
18      {
19          if(card[index][top[index]].rank==card[temp][top[temp]].rank||card[index][top[index]].suit==card[temp][top[temp]].suit)
20          {                                                                                                               
21           top[temp]++;
22           card[temp][top[temp]].rank=card[index][top[index]].rank;
23           card[temp][top[temp]].suit=card[index][top[index]].suit;
24           top[index]--; 
25           *position=temp;
26           return true;
27          }
28      }
29      temp=index;
30      count=0;
31      while(count!=1&&temp>=1)
32        if(top[--temp]!=-1)
33         count++;
34      if(count==1)
35      {
36          if(card[index][top[index]].rank==card[temp][top[temp]].rank||card[index][top[index]].suit==card[temp][top[temp]].suit)
37          {                                                                                                                        
38           top[temp]++;
39           card[temp][top[temp]].rank=card[index][top[index]].rank;
40           card[temp][top[temp]].suit=card[index][top[index]].suit;
41           top[index]--; 
42           *position=temp;
43           return true;
44          }
45      }
46      return false;
47 }
48 int main()
49 {
50 char lineone[78];
51 char linetwo[78];
52 int i,j;
53 while(gets(lineone))
54 {
55     if(lineone[0]=='#')
56     break;
57     gets(linetwo);
58     CARD card[52][52];
59     int top[52]={0};
60     for(i=0,j=0;i<78;)
61     {
62         card[j][0].rank=lineone[i++];
63         card[j][0].suit=lineone[i++];
64         i++;
65         j++;
66     }
67     for(i=0,j=26;i<78;)
68     {
69         card[j][0].rank=linetwo[i++];
70         card[j][0].suit=linetwo[i++];
71         i++;
72         j++;
73     }    
74     int position=1;
75     while(position!=52)
76     {
77         if(!(move(card,top,&position)&&top[position]!=-1))
78         position++;                           
79     }
80     int total=0;
81     for(i=0;i<52;i++)
82     if(top[i]!=-1)
83        total++;
84     if(total!=1)
85     printf("%d piles remaining:",total);
86     else
87     printf("%d pile remaining:",total);
88     for(i=0;i<52;i++)
89     if(top[i]!=-1)
90     printf(" %d",top[i]+1);
91     printf("\n");
92 }
93 return 0;   
94 }

 

posted @ 2013-03-30 23:58  sooflow  阅读(116)  评论(0)    收藏  举报