pku 1214 "Accordian" Patience STL+模擬
題意:你的任務是模擬一種叫「Accordian」的紙牌游戲,他的游戲規則如下:
一副撲克牌有52張牌,首先把紙牌一張一張由左到右排好(不能有重疊,所以共有52堆牌,每堆一張),當某一張牌與他左邊那張牌或者左邊的第三張牌有「Match」的時候,就把這張牌移到那張牌上面去。在這里兩張牌「Match」指的是這兩張牌的花色(suit)或者點數(rank)一樣。當你做了一個移動之后,要察看是否還可以做其他的移動。在任何時間,只有最上面那張牌可以被移動。如果因為移動一張牌使得產生一個空格(也就是被移動的那堆牌只有一張牌),你必須把右邊所有的牌堆往左移一格。如此不斷的尋找可移動的牌,直到沒有一張牌可以移動游戲就結束了。
在選擇可以移動的牌的時候可能有些狀況會發生。如果有兩張牌都可以移動,你應該要移動最左邊的那張牌。當一張牌可以被移動到左邊一格,或左邊三格的時候,你必須移動到左邊三格
解法:純模擬,外層用STL set,內層用STL stack,然后就是各種調用,各種迭代器~
代碼:
1
# include <list>
2
# include <vector>
3
# include <cstdio>
4
# include <algorithm>
5
using namespace std;
6
list<vector<pair<char,char> > > l;
7
int main()
8

{
9
vector<pair<char,char> >t;
10
while(true)
11
{
12
char tmp[5];
13
scanf("%s",tmp);
14
if(tmp[0]=='#') break;
15
t.clear();
16
l.clear();
17
t.push_back(pair<char,char>(tmp[0],tmp[1]));
18
l.push_back(t);
19
for(int i=1;i<52;i++)
20
{
21
t.clear();
22
scanf("%s",tmp);
23
t.push_back(pair<char,char>(tmp[0],tmp[1]));
24
l.push_back(t);
25
}
26
while(true)
27
{
28
bool flag=false;
29
for(list<vector<pair<char,char> > >::iterator i=l.begin();i!=l.end()&&!flag;)
30
{
31
int count=0;
32
bool remove=false;
33
list<vector<pair<char,char> > >::iterator i3=i;
34
while(i3!=l.begin()&&count<3)
35
{
36
count++;
37
i3--;
38
}
39
if(count==3&&(i3->back().first==i->back().first||i3->back().second==i->back().second))
40
{
41
i3->push_back(i->back());
42
i->pop_back();
43
if(i->empty()) i=l.erase(i),remove=true;
44
flag=true;
45
}
46
else
47
{
48
i3=i;
49
if(i3!=l.begin())
50
{
51
i3--;
52
if(i3->back().first==i->back().first||i3->back().second==i->back().second)
53
{
54
i3->push_back(i->back());
55
i->pop_back();
56
if(i->empty()) i=l.erase(i),remove=true;
57
flag=true;
58
}
59
}
60
}
61
if(!remove) i++;
62
}
63
if(!flag) break;
64
}
65
printf("%d piles remaining:",l.size());
66
for(list<vector<pair<char,char> > >::iterator i=l.begin();i!=l.end();i++)
67
printf(" %d",i->size());
68
printf("\n");
69
}
70
}
71

2

3

4

5

6

7

8



9

10

11



12

13

14

15

16

17

18

19

20



21

22

23

24

25

26

27



28

29

30



31

32

33

34

35



36

37

38

39

40



41

42

43

44

45

46

47



48

49

50



51

52

53



54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

posted on 2011-01-13 21:28 yzhw 閱讀(224) 評論(0) 編輯 收藏 引用 所屬分類: data struct 、simple problem~