uva 11210 Chinese Mahjong(暴力搜索)
Chinese Mahjong
Mahjong (
) is a game of Chinese origin usually played by four persons with tiles resembling dominoes and bearing various designs, which are drawn and discarded until one player wins with a hand of four combinations of three tiles each and a pair of matching tiles.
A set of Mahjong tiles will usually differ from place to place. It usually has at least 136 tiles, most commonly 144, although sets originating from America or Japan will have more. The 136-tile Mahjong includes:
Dots: named as each tile consists of a number of circles. Each circle is said to represent copper (tong) coins with a square hole in the middle. In this problem, they're represented by 1T, 2T, 3T, 4T, 5T, 6T, 7T, 8T and 9T.

Bams: named as each tile (except the 1 Bamboo) consists of a number of bamboo sticks. Each stick is said to represent a string (suo) that holds a hundred coins. In this problem, they're represented by 1S, 2S, 3S, 4S, 5S, 6S, 7S, 8S and 9S.

Craks: named as each tile represents ten thousand (wan) coins, or one hundred strings of one hundred coins. In this problem, they're represented by 1W, 2W, 3W, 4W, 5W, 6W, 7W, 8W and 9W.

Wind tiles: East, South, West, and North. In this problem, they're represented by DONG, NAN, XI, BEI.

Dragon tiles: red, green, and white. The term dragon tile is a western convention introduced by Joseph Park Babcock in his 1920 book introducing Mahjong to America. Originally, these tiles are said to have something to do with the Chinese Imperial Examination. The red tile means you pass the examination and thus will be appointed a government official. The green tile means, consequently you will become financially well off. The white tile (a clean board) means since you are now doing well you should act like a good, incorrupt official. In this problem, they're represented by ZHONG, FA, BAI.

There are 9*3+4+3=34 kinds, with exactly 4 tiles of each kind, so there are 136 tiles in total.
To who may be interested, the 144-tile Mahjong also includes:
Flower tiles:
typically optional components to a set of mahjong tiles, often contain artwork on their tiles. There are exactly one tile of each kind, so 136+8=144 tiles in total. In this problem, we don��t consider these tiles.

Chinese Mahjong is very complicated. However, we only need to know very few of the rules in order to solve this problem. A meldis a certain set of tiles in one's hand. There are three kinds of melds you need to know (to who knows Mahjong already, kong is not considered):
Pong: A set of three identical titles. Example:
;
.
Chow: A set of three suited tiles in sequence. All three tiles must be of the same suites. Sequences of higher length are not permissible (unless it forms more than one meld). Obviously, wind tiles and dragon tiles can never be involved in chows. Example:
;
.
Eye: The pair, while not a meld, is the final component to the standard hand. It consists of any two identical tiles.
A player wins the round by creating a standard mahjong hand. That means, the hand consists of an eye and several (possible zero) pongs and chows. Note that each title can be involved in exactly one eye/pong/chow.
When a hand is one tile short of wining, the hand is said to be a ready hand, or more figuratively, 'on the pot'. The player holding a ready hand is said to be waiting for certain tiles. For example

is waiting for
,
and
.
To who knows more about Mahjong: don't consider special winning hands such as '
'.
Input
The input consists of at most 50 test cases. Each case consists of 13 tiles in a single line. The hand is legal (e.g. no invalid tiles, exactly 13 tiles). The last case is followed by a single zero, which should not be processed.
Output
For each test case, print the case number and a list of waiting tiles sorted in the order appeared in the problem description (1T~9T, 1S~9S, 1W~9W, DONG, NAN, XI, BEI, ZHONG, FA, BAI). Each waiting tile should be appeared exactly once. If the hand is not ready, print a message 'Not ready' without quotes.
Sample Input
1S 1S 2S 2S 2S 3S 3S 3S 7S 8S 9S FA FA
1S 2S 3S 4S 5S 6S 7S 8S 9S 1T 3T 5T 7T
0
Output for the Sample Input
Case 1: 1S 4S FA
Case 2: Not ready
题目大意:给出13张麻将牌,问在取一张牌就可以胡牌的牌,所处所有满足的情况。这里的胡牌不需要考虑太多,只需要满足存在一个对子, 而其他的全是3个顺或者3个相同的就可以了,一些特殊的胡牌不需要考虑。
解题思路:将所有给出的麻将牌转化成数字进行处理,对应的用一个数组统计牌的个数cnt[i]表示标号为i的麻将牌有cnt[i]张。因为存在特殊的牌面,所以可以将特殊牌面的标号分开,这样在dfs的过程中就无需考虑连续的标号是否是顺子的问题。接下来就是枚举所有可能拿到的牌(出现过4次的不能再取),加入后用dfs判断是否可以胡牌,因为只有三种情况,dfs的时候直接写出来就可以了,注意对子只出现一次。
#include <stdio.h>
#include <string.h>
const int N = 105; int cnt[N], answer[N];
const char sign[] = "TSW";
const char sen[][10] = {"BAI", "DONG", "NAN", " ", "XI", "BEI", " ", "ZHONG", "FA"}; int changeOne(char c) {
if (c == 'T') return 0;
else if (c == 'S') return 1;
else if (c == 'W') return 2;
} int changeTwo(char a, char b) {
if (a == 'D' && b == 'O') return 31;
else if (a == 'N' && b == 'A') return 32;
else if (a == 'X' && b == 'I') return 34;
else if (a == 'B' && b == 'E') return 35;
else if (a == 'Z' && b == 'H') return 37;
else if (a == 'F' && b == 'A') return 38;
else if (a == 'B' && b == 'A') return 40;
else return -1;
} void handle(char str[]) {
memset(cnt, 0, sizeof(cnt));
cnt[10] = cnt[20] = cnt[30] = cnt[33] = cnt[36] = cnt[39] = 5;
int n = 0, len = strlen(str), cur;
for (int i = 0; i < len - 1; i++) {
if (str[i] > '0' && str[i] <= '9') {
cur = 0;
cur += str[i++] - '0';
cur += changeOne(str[i++]) * 10;
cnt[cur]++;
}
else {
cur = changeTwo(str[i], str[i + 1]);
if (cur > 0)
cnt[cur]++;
}
}
} bool dfs(int sum, int flag) {
if (sum >= 14) return true; for (int i = 1; i <= 40; i++) {
if (cnt[i] >= 3 && cnt[i] < 5) {
cnt[i] -= 3;
if (dfs(sum + 3, flag)) {
cnt[i] += 3;
return true;
}
cnt[i] += 3;
}
if (!flag && cnt[i] >= 2 && cnt[i] < 5) {
cnt[i] -= 2;
if (dfs(sum + 2, 1)) {
cnt[i] += 2;
return true;
}
cnt[i] += 2;
} if (cnt[i] && cnt[i + 1] && cnt[i + 2] && cnt[i] < 5 && cnt[i + 1] < 5 && cnt[i + 2] < 5) {
cnt[i]--, cnt[i + 1]--, cnt[i + 2]--;
if (dfs(sum + 3, flag)) {
cnt[i]++, cnt[i + 1]++, cnt[i + 2]++;
return true;
}
cnt[i]++, cnt[i + 1]++, cnt[i + 2]++;
}
}
return false;
} void put(int cur) {
int a = cur / 10, b = cur % 10;
if (a < 3)
printf(" %d%c", b, sign[a]);
else
printf(" %s", sen[b]);
} int main() {
char str[N];
int cas = 1;
while (gets(str)) {
if (strcmp(str, "0") == 0)
break;
memset(answer, 0, sizeof(answer));
int t = 0; handle(str); for (int i = 1; i <= 40; i++) {
if (cnt[i] >= 4) continue;
cnt[i]++;
if (dfs(0, 0))
answer[t++] = i;
cnt[i]--;
}
printf("Case %d:", cas++);
if (t)
for (int i = 0; i < t; i++)
put(answer[i]);
else
printf(" Not ready");
printf("\n");
}
return 0;
}
uva 11210 Chinese Mahjong(暴力搜索)的更多相关文章
- UVa 11210 Chinese Mahjong (暴力,递归寻找)
题意:这个题意.有点麻烦,就是说给定13张牌,让你求能“听”的牌.(具体的见原题) 原题链接: https://uva.onlinejudge.org/index.php?option=com_onl ...
- UVa 11210 - Chinese Mahjong
解题报告:麻将的规则这里就不说了,这题我们可以用暴力的方法,所以我们应该这样枚举,即将34张牌的每一张牌都放到原来的十三张牌里面去,所以这时我们只要判断这十四张牌能不能胡,因为若要胡的话一定要有一个对 ...
- UVa 11210 - Chinese Mahjong 模拟, 枚举 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- hdu 4740 The Donkey of Gui Zhou(暴力搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- 随手练——洛谷-P1151(枚举与暴力搜索)
枚举 #include <iostream> using namespace std; int main() { ; cin >> k; ; i < ; i++) { ) ...
- UVA.10305 Maximum Product (暴力)
UVA.10305 Maximum Product (暴力) 题意分析 直接枚举起点和重点,然后算出来存到数组里面,sort然后取最大值即可. 代码总览 #include <iostream&g ...
随机推荐
- 【C疯狂的教材】(九)C语言指针(一)
1.什么是地址? 内存是由若干个1个字节的内存单元组成的存储器 计算机为了管理内存单元.给每一个字节都进行编号 编号的就是地址 2.什么是指针? 地址就是指针 地址: 地址指向的内存单元值:编号相应的 ...
- qt 3d 绘图
首先不得不说,要感谢北京邮电大学的阿科.感谢他慷慨的分享和极具科学态度的记录,将自己搜集到的众多资料收集整理发布,拯救众多苦逼寻找方案的程序员于苦海之中.因为最近接手新的项目,涉及到使用opengl做 ...
- 【转】Xcode升到6.4插件失效,与添加插件不小心点击Skip Bundle解决办法
转载自:http://www.jianshu.com/p/d51547d29309 今天升级了xcode到6.4 发现之前装的插件不能使用了.这里有一个解决的方案: 步骤如下: 一.查看Xcode的U ...
- My way to Python - Day012 - 消息中间件
消息中间件介绍 消息中间件的概念 消息中间件是在消息传输过程中保存消息的容器.消息中间件在将消息从它的源中继到它的目标时充当中间人的作用.队列的主要作用是提供路由并保证消息的传递:如果发生消息接收者不 ...
- 通过JS控制textarea的输入长度
废话不多说,直接上代码(因为自己也只是遇到的时候然后上网查到的解决办法,放在此处只是为了方便各位看以及以后再碰到用起来方便) <ul> <li> <textarea ro ...
- 未在本地计算机上注册"Microsoft.ACE.OLEDB.12.0"提供程序
运行时出现了错误,提示未在本地计算机上注册"Microsoft.ACE.OLEDB.12.0"提供程序. 这个问题的原因是64位PC上安装了Office的32位版本,所以Micro ...
- 修改UITextfield的Placeholder字体的颜色
- (void)viewDidLoad { [super viewDidLoad]; self.title=@"修改UITextField的placeholder字体颜色"; UI ...
- OpenGL ES 2.0 符点精度
片元着色器中使用符点相关类型的变量时与顶点着色器中有所不同,在顶点着色器中直接声明使用即可,而在片元着色器中必须指定精度. lowp 低 mediump 中 highp 高 指定整个着色器中符点相关类 ...
- 费马小定理&欧拉定理
在p是素数的情况下,对任意整数x都有xp≡x(mod p).这个定理被称作费马小定理其中如果x无法被p整除,我们有xp-1≡1(mod p).利用这条性质,在p是素数的情况下,就很容易求出一个数的逆元 ...
- 关于毕设WiFi选型
毕设做基于WiFi的家庭灯光影音控制,现在主要是WiFi模块上的选型,自己选了ESP8266. 一开始是看到了机智云的内容,想尝试机智云.但是目前没有母版,不知道怎么下手. 接下来就先尝试通过通用类型 ...