CF1191B Tokitsukaze and Mahjong
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].
分析:
这题只要看懂了题就知道输出答案只有三种,分别是0,1,2.
但是每一种答案都有几种情况
当答案是0的时候
- 字母必须一样
- 数字三个相等或三个为递增加1
当答案是1的时候
- 字母有两个一样
- 数字两个相等或两个为递增加1或两个为递增加2
剩余的情况输出0
如果我们直接写if/else会很麻烦,同样逻辑不清晰导致代码错误会有很大的几率*(在比赛中我就是这样的) 经过一大佬指点,发现这道题太简单了.我们可以将字母代替成数字,比如说将1代替为m,则1m=1,2m=2,9m=9.在这里s或p不能为10或20了,因为9m===>9,9 + 1 = 10与1s相等.(这样就不好搞了) 所以我将s,p分别代替成100,200.
#include<bits/stdc++.h>
using namespace std;
char a[3];
char b[3];
char c[3];
int sz[5];
int mem(char x[])
{
int A = 0;
if(x[1] == 'm')A += x[0] - '0';
else if(x[1] == 's')A += x[0] - '0' + 100;
else A += x[0] - '0' + 200;
return A;
}
int main()
{
scanf("%s",a);
scanf("%s",b);
scanf("%s",c);
sz[1] = mem(a);
sz[2] = mem(b);
sz[3] = mem(c);
sort(sz + 1,sz + 3 + 1);
if((sz[1] == sz[2] && sz[2] == sz[3]) || (sz[2] - sz[1] == 1 && sz[3] - sz[2] == 1))printf("0\n");
else if((sz[1] == sz[2] || sz[2] == sz[3]) || (sz[2] - sz[1] == 1 || sz[3] - sz[2] == 1) || (sz[2] - sz[1] == 2 || sz[3] - sz[2] == 2))printf("1\n");
else printf("2\n");
return 0;
}
本文介绍了一个基于日本麻将的游戏算法,玩家需要通过获取特定牌型来赢得游戏。文章详细解释了游戏中术语的定义,并提供了解决方案,即如何计算获得胜利所需的最少额外抽牌数。
415

被折叠的 条评论
为什么被折叠?



