Codeforces Round #573 (Div. 2) B - Tokitsukaze and Mahjong
Codeforces Round #573 (Div. 2)
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s.
In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand.
Do you know the minimum number of extra suited tiles she needs to draw so that she can win?
Here are some useful definitions in this game:
- A mentsu, also known as meld, is formed by a koutsu or a shuntsu;
- A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu;
- A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu.
Some examples:
- [2m, 3p, 2s, 4m, 1s, 2s, 4s] — it contains no koutsu or shuntsu, so it includes no mentsu;
- [4s, 3m, 3p, 4s, 5p, 4s, 5p] — it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu;
- [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] — it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu.
Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite.
Input
The only line contains three strings — the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s.
Output
Print a single integer — the minimum number of extra suited tiles she needs to draw.
Examples
input
1s 2s 3s
output
0
input
9m 9m 9m
output
0
input
3p 9m 2p
output
1
Note
In the first example, Tokitsukaze already has a shuntsu.
In the second example, Tokitsukaze already has a koutsu.
In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile — 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
题意:貌似背景是打麻将赢的两种方式??好巧啊我不会打麻将。题目首先告诉我们赢得方式有两种,
一种是三个牌相同,也就是koutsu,见样例2;另外一种方式是顺子,也就是shuntsu,见样例1。
然后是问给三个牌给我们,问最多还要凑几张牌能赢。
思路:直接暴力枚举所有情况就好了,显然如果是最坏情况,最多再凑两张牌组成三张相同的牌就能赢了......
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<map>
6 #include<set>
7 #include<vector>
8 #include<queue>
9 #include<cmath>
10 using namespace std;
11 #define ll long long
12 const int mod=1e9+7;
13 const int inf=1e9+7;
14
15 //const int maxn=
16 int main()
17 {
18 ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
19
20 string a,b,c;
21 while(cin>>a>>b>>c)
22 {
23 if(a==b&&b==c)// 特判koutsu
24 {
25 cout<<0<<endl;
26 continue;
27 }
28
29 if(a==b||a==c||b==c)//两个牌一样,最优解还是koutsu
30 {
31 cout<<1<<endl;
32 continue;
33 }
34
35 //接下来都是三个不同的牌
36
37 //先排序,排序后更好处理
38 if(a>b) swap(a,b);
39 if(a>c) swap(a,c);
40 if(b>c) swap(b,c);
41
42 if(a[1]==b[1]&&b[1]==c[1])//字母相同
43 {
44 if(a[0]+1==b[0]&&b[0]+1==c[0])
45 {
46 cout<<0<<endl;//特判shuntsu
47 continue;
48 }
49 }
50
51 if(a[1]==b[1])
52 {
53 if(a[0]+1==b[0]||a[0]+2==b[0])//能构成shuntsu中的一部分
54 {
55 cout<<1<<endl;
56 continue;
57 }
58 }
59
60 if(a[1]==c[1])//同上
61 {
62 if(a[0]+1==c[0]||a[0]+2==c[0])
63 {
64 cout<<1<<endl;
65 continue;
66 }
67 }
68
69 if(b[1]==c[1])//同上
70 {
71 if(b[0]+1==c[0]||b[0]+2==c[0])
72 {
73 cout<<1<<endl;
74 continue;
75 }
76 }
77
78 //无特解
79 cout<<2<<endl;
80 }
81
82 return 0;
83 }