HDOJ4431 Mahjong
来源:互联网 发布:程序员去哪个招聘网站 编辑:程序博客网 时间:2024/06/04 18:17
思路:模拟麻将。我之前用递归TLE了,后来修改后就WA,发现以下两种情况没有考虑到。
T1:2s 2s 3s 3s 4s 4s 2p 2p 3p 3p 4p 4p 5p
ans: 2 2p 5p
T2: 1p 1p 1s 9s 1m 9m 1c 2c 3c 4c 5c 6c 7c
ans: 1 9p
(总之感谢妈咪从小就让我认识了麻将=w=)(上次有一道国际象棋的题目我就不会做,所以生活积累很重要)
Mahjong
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2665 Accepted Submission(s): 548
Problem Description
Japanese Mahjong is a four-player game. The game needs four people to sit around a desk and play with a set of Mahjong tiles. A set of Mahjong tiles contains four copies of the tiles described next:
One to nine Man, which we use 1m to 9m to represent;
One to nine Sou, which we use 1s to 9s to represent;
One to nine Pin, which we use 1p to 9p to represent;
Character tiles, which are:Ton, Nan, Sei, Pei, Haku, Hatsu, Chun, which we use 1c to 7c to represent.
A winning state means a set of 14 tiles that normally contains a pair of same tiles (which we call "eyes") and four melds. A meld is formed by either three same tiles(1m, 1m, 1m or 2c, 2c, 2c for example) or three continuous non-character tiles(1m, 2m, 3m or 5s, 6s, 7s for example).
However, there are two special winning states that are different with the description above, which are:
"Chii Toitsu", which means 7 different pairs of tiles;
"Kokushi Muso", which means a set of tiles that contains all these tiles: 1m, 9m, 1p, 9p, 1s, 9s and all 7 character tiles. And the rest tile should also be one of the 13 tiles above.
And the game starts with four players receiving 13 tiles. In each round every player must draw one tile from the deck one by one. If he reaches a winning state with these 14 tiles, he can say "Tsu Mo" and win the game. Otherwise he should discard one of his 14 tiles. And if the tile he throws out can form a winning state with the 13 tiles of any other player, the player can say "Ron" and win the game.
Now the question is, given the 13 tiles you have, does there exist any tiles that can form a winning state with your tiles?
(Notes: Some of the pictures and descriptions above come from Wikipedia.)
One to nine Man, which we use 1m to 9m to represent;
One to nine Sou, which we use 1s to 9s to represent;
One to nine Pin, which we use 1p to 9p to represent;
Character tiles, which are:Ton, Nan, Sei, Pei, Haku, Hatsu, Chun, which we use 1c to 7c to represent.
A winning state means a set of 14 tiles that normally contains a pair of same tiles (which we call "eyes") and four melds. A meld is formed by either three same tiles(1m, 1m, 1m or 2c, 2c, 2c for example) or three continuous non-character tiles(1m, 2m, 3m or 5s, 6s, 7s for example).
However, there are two special winning states that are different with the description above, which are:
"Chii Toitsu", which means 7 different pairs of tiles;
"Kokushi Muso", which means a set of tiles that contains all these tiles: 1m, 9m, 1p, 9p, 1s, 9s and all 7 character tiles. And the rest tile should also be one of the 13 tiles above.
And the game starts with four players receiving 13 tiles. In each round every player must draw one tile from the deck one by one. If he reaches a winning state with these 14 tiles, he can say "Tsu Mo" and win the game. Otherwise he should discard one of his 14 tiles. And if the tile he throws out can form a winning state with the 13 tiles of any other player, the player can say "Ron" and win the game.
Now the question is, given the 13 tiles you have, does there exist any tiles that can form a winning state with your tiles?
(Notes: Some of the pictures and descriptions above come from Wikipedia.)
Input
The input data begins with a integer T(1≤T≤20000). Next are T cases, each of which contains 13 tiles. The description of every tile is as above.
Output
For each cases, if there actually exists some tiles that can form a winning state with the 13 tiles given, print the number first and then print all those tiles in order as the description order of tiles above. Otherwise print a line "Nooten"(without quotation marks).
Sample Input
21s 2s 3s 2c 2c 2c 2p 3p 5m 6m 7m 1p 1p1p 1p 2p 3p 4s 5s 6s 7c 7c 3s 3s 2m 2m
Sample Output
2 1p 4pNooten
#include <cstdio>#include <cstring>#include <iostream>#include <vector>using namespace std;const char* mahjong[] = { "1m", "2m", "3m", "4m", "5m", "6m", "7m", "8m", "9m", "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "1c", "2c", "3c", "4c", "5c", "6c", "7c"};int num[34];int str[13] = {0, 8, 9, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33};int convert(char *s){ for(int i = 0; i < 34; i++){ if(!strcmp(mahjong[i],s)) return i; } return -1;}int c[34];bool search(){ for(int i = 0; i < 34; i++){ num[i] = c[i]; } for(int i = 0; i < 34; i++){ if(num[i] >= 3) num[i]-=3; if(num[i] > 0){ int ans = 0; while(1){ if(ans>18) break; if(i>=ans &&i<=(ans+6) && num[i] >= 1 && num[i+1] >= num[i] &&num[i+2] >=num[i]){ int tmp = num[i]; num[i] = 0; num[i+1]-=tmp; num[i+2]-=tmp; } ans+=9; } } } bool flag = 0; for(int i = 0; i < 34; i++) if(num[i]>0){ flag = 1; } if(!flag) return 1; return 0;}bool check(){ //case2 int sum = 0; for(int i = 0; i < 34; i++){ num[i] = c[i]; if(num[i]==2) sum++; } if(sum == 7) return 1; //case3 sum = 0; int ans = 0; for(int i = 0; i < 13; i++){ if(num[str[i]]){ sum += num[str[i]]; ans++; } } if(ans == 13 && sum == 14) return 1; //case1 for(int i = 0; i < 34; i++){ if(c[i] >= 2){ c[i]-=2; if(search()){ c[i]+=2; return 1; } c[i]+=2; } } return 0;}vector<int> G;int main(){ bool ok; char s[100]; int mj[15]; int cases; scanf("%d", &cases); while(cases--){ G.clear(); scanf("%s", s); mj[0] = convert(s); for(int i = 1; i < 13; i++){ scanf("%s", s); mj[i] = convert(s); if(mj[i]==-1) continue; } ok = false; memset(c, 0, sizeof(c)); for(int i = 0; i < 13; i++){ c[mj[i]]++; } for(int i = 0; i < 34; i++){ if(c[i] >= 4) continue; c[i]++; if(check()){ ok = true; G.push_back(i); } c[i]--; } if(!ok){ printf("Nooten\n"); continue; } printf("%d", G.size()); for(int i = 0; i < G.size(); i++){ printf(" %s", mahjong[G[i]]); } printf("\n"); } return 0;}
- HDOJ4431 Mahjong
- HDOJ4431 Mahjong[编码题]
- Mahjong
- HDU4431 Mahjong
- Chinese Mahjong
- hdu4431 Mahjong
- A. Mahjong
- hdu4431 Mahjong
- uva11210Chinese Mahjong
- Chinese Mahjong
- Chinese Mahjong
- Chinese Mahjong
- Mahjong Tales: Ancient Wisdom
- [HDU4431][TJ01A]Mahjong
- Mahjong HDU 4431
- hdu - 4431 - Mahjong - 模拟
- 11210 - Chinese Mahjong
- uva 11210 Chinese Mahjong
- ORACLE FLASHBACK DROP 知识整理
- Android 浏览器自定义scheme:market://协议
- 文本相似度算法(余弦定理)
- gdb调试器的常用命令
- python中用正则表达式检测邮件是否合法
- HDOJ4431 Mahjong
- 开源Word读写组件DocX 的深入研究和问题总结
- 重要宏收录
- HTTP权威指南 -- 第1章 HTTP概述
- 简单的Vector遍历
- Linux下各种主要文件系统的读写性能测试
- HDU 2602 01背包中两种写法
- 解决struts2 属性参数乱码问题
- 2013.9.9杂感