uva127

你的任务是模拟一种叫「Accordian」的纸牌游戏,他的游戏规则如下:

一副扑克牌有52张牌,首先把纸牌一张一张由左到右排好(不能有重叠,所以共有52堆牌,每堆一张),当某一张牌与他左边那张牌或者左边的第三张牌有「Match」的时候,就把这张牌移到那张牌上面去。在这里两张牌「Match」指的是这两张牌的花色(suit)或者点数(rank)一样。当你做了一个移动之后,要察看是否还可以做其他的移动。在任何时间,只有最上面那张牌可以被移动。如果因为移动一张牌使得产生一个空格(也就是被移动的那堆牌只有一张牌),你必须把右边所有的牌堆往左移一格。如此不断的寻找可移动的牌,直到没有一张牌可以移动游戏就结束了。

在选择可以移动的牌的时候可能有些状况会发生。如果有两张牌都可以移动,你应该要移动最左边的那张牌。当一张牌可以被移动到左边一格,或左边三格的时候,你必须移动到左边三格。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <vector>
 4 using namespace std;
 5 int n;
 6 struct ND
 7 {
 8     char s[5];
 9     ND( char *ch = NULL )
10     {
11         strcpy( s, ch );
12     }
13 };
14 vector <ND> nod[60];
15 
16 bool M_atch( const ND &a, const ND &b )
17 {
18     if( a.s[0] == b.s[0] || a.s[1] == b.s[1] )
19         return true;
20     return false;
21 }
22 
23 void Move( int x )
24 {
25     for( int i = x; i < n; ++i )
26     {
27         nod[i].clear();
28         for( int j = 0; j < nod[i+1].size(); ++j )
29             nod[i].push_back( ND( nod[i+1][j].s ) );
30     }
31 }
32 
33 int main()
34 {
35     char ss[5];
36     while( scanf( "%s", ss ) && ss[0] != '#' )
37     {
38         n = 52;
39         nod[0].push_back( ND( ss ) );
40         for( int i = 1; i < 52; ++i )
41         {
42             scanf( "%s", ss );
43             nod[i].push_back( ND( ss ) );
44         }
45         bool flag = true;
46         while( flag )
47         {
48             flag = false;
49             for( int i = 1; i < n; ++i )
50             {
51                 int y = i - 3;
52                 int cur = nod[i].size() - 1;
53                 int cury = nod[y].size() - 1;
54                 if( y >= 0 && M_atch( nod[i][cur].s, nod[y][cury].s ) )
55                 {
56                     nod[y].push_back( ND( nod[i][cur].s ) );
57                     nod[i].pop_back();
58                     if( nod[i].size() == 0 )
59                     {
60                         --n;
61                         Move( i );
62                     }
63                     flag = true;
64                     break;
65                 }
66                 int x = i - 1;
67                 int curx = nod[x].size() - 1;
68                 if( x >= 0 && M_atch( nod[i][cur].s, nod[x][curx].s ) )
69                 {
70                     nod[x].push_back( ND( nod[i][cur].s ) );
71                     nod[i].pop_back();
72                     if( nod[i].size() == 0 )
73                     {
74                         --n;
75                         Move( i );
76                     }
77                     flag = true;
78                     break;
79                 }
80             }
81         }
82         if( n == 1 )
83             printf( "1 pile remaining:" );
84         else
85             printf( "%d piles remaining:", n );
86         for( int i = 0; i < n; ++i )
87             printf( " %d", nod[i].size() );
88         puts( "" );
89         for( int i = 0; i <= 52; ++i )
90             nod[i].clear();
91     }
92     return 0;
93 }
View Code

 

转载于:https://www.cnblogs.com/ADAN1024225605/p/4048267.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值