UVA - 11210 Chinese Mahjong(回溯)

本文介绍了一种基于中国麻将规则的听牌算法实现。通过分析手牌中的各种组合,如碰、吃、杠等,来判断玩家是否处于听牌状态,并进一步确定所需的胡牌类型。该算法适用于标准麻将规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem C

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 meld is 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


题目大意:想AC还得会先打麻将,题目的前半段介绍了什么是中国麻将。

麻将牌
全副牌共有6类42种图案,144张。

(一)序数牌合计108张


1.万子牌:从一万至九万,各4张,共36张。
1W, 2W, 3W, 4W, 5W, 6W, 7W, 8W and 9W.

2.饼子牌:从一饼至九饼,各4张,共36张。
1T, 2T, 3T, 4T, 5T, 6T, 7T, 8T and 9T.

3.条子牌:从一条至九条,各4张,共36张。
1S, 2S, 3S, 4S, 5S, 6S, 7S, 8S and 9S.

(二)字牌合计28张
1.风牌:东、南、西、北,各4张,共16张。
DONG, NAN, XI, BEI.

2.箭牌:中、发、白,各4张,共12张。
ZHONG, FA, BAI.

(三)花牌:春、夏、秋、冬,梅、兰、竹、菊,各一张,共8张。这题暂时用不到

听牌:只要再凑一张即可成功胡牌

胡牌规则:打时是十三张,胡时是十四张,要有一对做将,其他四个三张一样的或顺子;特殊的是十三不靠,只有两张相同的,其他都不一样

题目给你13张手牌,手牌的按照上面的符号,现在问你以当前的手牌是否能听牌,如果能听牌,那么差什么牌就能胡牌。

解析:枚举每一张麻将牌,看是否可以胡,在这个枚举之下还有嵌套枚举,就是加入第i个麻将后,枚举手上14张牌中的对子,对子选完后,就去回溯查询是否剩下的牌都是 ABC型 或是 AAA型,如果满足4组,则表示可以胡牌。

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int vis[40];
bool ok,hu;
const char* majiang[] = {
	"1T", "2T", "3T", "4T", "5T", "6T", "7T", "8T", "9T",
	"1S", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S",
	"1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W",
	"DONG", "NAN", "XI", "BEI",
	"ZHONG", "FA", "BAI"
};
int change(char str[]) {
	for(int i = 0; i < 34; i++) {
		if(!strcmp(str,majiang[i])) {
			return i;
		}
	}
	return -1;
}
void dfs(int cur) {
	if(cur == 4) {
		hu = true;
		return ;
	}
	//找到3相同
	for(int i = 0; i < 34; i++) {
		if(vis[i] >= 3) {
			vis[i] -= 3;
			dfs(cur+1);
			vis[i] += 3;
		}
	}
	//找顺子
	for(int i = 0; i < 24; i++) {
		if( i % 9 < 7 && vis[i] >= 1 && vis[i+1] >= 1 && vis[i+2] >= 1) {
			vis[i]--;
			vis[i+1]--;
			vis[i+2]--;
			dfs(cur+1);
			vis[i]++;
			vis[i+1]++;
			vis[i+2]++;
		}
	}
}
bool judge() {
	for(int i = 0; i < 34; i++) {
		if(vis[i] >= 2) { //找到对子减去
			vis[i] -= 2;
			hu = false;
			dfs(0);
			if(hu) {
				return true;
			}
			vis[i] += 2;
		}
	}
	return false;
}
int main() {
	int cas = 1;
	int mj[20];
	char str[20];
	while(scanf("%s",str) != EOF) {
		if(str[0] == '0') {
			break;
		}
		mj[0] = change(str);
		for(int i = 1; i < 13; i++) {
			scanf("%s",str);
			mj[i] = change(str);
		}
		printf("Case %d:",cas++);
		ok = false;
		for(int i = 0; i < 34; i++) {
			memset(vis,0,sizeof(vis));
			for(int j = 0; j < 13; j++) {
				vis[mj[j]]++;
			}
			if(vis[i] >= 4) {
				continue;
			}
			vis[i]++;
			if(judge()) {
				ok = true;
				printf(" %s",majiang[i]);
			}
			vis[i]--;
		}
		if(ok) {
			printf("\n");
		}else {
			printf(" Not ready\n");
		}
	}
	return 0;
}



附上一组样例:

input
4T 5T 6T 6T 6T 7T 8T 9T 9T 9T BAI BAI
4T 5T 6T 6T 6T 6T 7T 8T 9T 9T 9T 1W 1W
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

output
Case 1: 5T
Case 2: Not ready
Case 3: 1S 4S FA
Case 4: Not ready

### Mahjong Helper 麻将辅助工具及相关项目 Mahjong Helper 是一个开源项目,专注于为麻将玩家提供帮助和分析功能。其生态系统中包含多个相关项目,这些项目可以进一步扩展其功能并提升用户体验[^1]。 #### 1. Mahjong AI Mahjong AI 是基于 Mahjong Helper 开发的一个智能系统,主要用于自动分析玩家的手牌并提供决策建议。该工具通过复杂的算法模拟对手的行为,并预测可能的出牌策略,从而帮助用户在游戏中做出更明智的选择[^1]。 #### 2. Mahjong Scoreboard Mahjong Scoreboard 是一个记录和展示比赛得分的工具,能够与 Mahjong Helper 无缝配合使用。它支持多种计分规则,并允许用户自定义比赛设置。通过实时更新比分信息,Mahjong Scoreboard 可以让玩家更加专注于游戏本身,而无需担心繁琐的分数计算问题[^1]。 #### 3. Mahjong Rules Mahjong Rules 是一个详细记录各种麻将规则的仓库,旨在为 Mahjong Helper 提供全面的规则支持。无论是传统中国麻将还是日式立直麻将,该项目都涵盖了丰富的规则说明和示例代码,确保用户能够在不同规则下正确使用 Mahjong Helper[^1]。 #### 4. Mahjong Helper - Majsoul 助手 对于《雀魂Majsoul》的爱好者而言,Mahjong Helper - Majsoul 助手是一个不可或缺的工具。它不仅可以帮助初学者快速掌握游戏技巧,还能为高级玩家提供深入的数据分析和策略优化建议。这款工具极大地提升了用户的竞技乐趣和游戏体验[^2]。 #### 5. Akagi 项目 Akagi 是另一个与麻将相关的项目,主要功能是通过拦截和解析《雀魂Majsoul》的游戏数据来实现自动化操作。在 `settings.json` 文件中,用户可以配置多个参数,例如是否启用自动播放(Autoplay)、是否使用 mahjong-helper(Helper)、以及是否自动和牌(Autohu)。此外,Akagi 还支持 MITM 端口重定向和 XMLRPC 端口绑定等功能,为开发者提供了更大的灵活性[^3]。 ```python # 示例:Akagi 的 settings.json 配置文件 { "Unlocker": true, "Autoplay": false, "Helper": true, "Autohu": true, "Port": 8080, "XMLRPC": 9090, "Playwright": { "viewport": { "width": 1920, "height": 1080 } } } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值