UVa中国麻将(Chinese Mahjong,Uva 11210)

简单的回溯题

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 char *mahjong[]={
 8                                 "1T","2T","3T","4T","5T","6T","7T","8T","9T",
 9                                 "1S","2S","3S","4S","5S","6S","7S","8S","9S",
10                                 "1W","2W","3W","4W","5W","6W","7W","8W","9W",
11                                 "DONG","NAN","XI","BEI",
12                                 "ZHONG","FA","BAI"
13                                 };
14
15 int convert(char *s)//映射处理
16 {
17     for(int i=0;i<34;i++)
18         if(strcmp(mahjong[i],s)==0)
19             return i;
20     return -1;
21 }
22
23 int c[34];
24
25 bool search(int dep)
26 {
27     for(int i=0;i<34;i++)//枚举刻子
28         if(c[i]>=3)
29         {
30             if(dep==3) return true;
31             c[i]-=3;
32             if(search(dep+1)) return true;
33             c[i]+=3;
34         }
35     for(int i=0;i<34;i++)//枚举顺子
36         if(i%9<=6&&c[i]>=1&&c[i+1]>=1&&c[i+2]>=1)
37         {
38             if(dep==3) return true;
39             c[i]--;c[i+1]--;c[i+2]--;
40             if(search(dep+1)) return true;
41             c[i]++;c[i+1]++;c[i+2]++;
42         }
43     return false;
44 }
45
46 bool check()
47 {
48     for(int i=0;i<34;i++)//枚举将牌
49         if(c[i]>=2)
50         {
51             c[i]-=2;
52             if(search(0)) return true;
53             c[i]+=2;
54         }
55     return false;
56 }
57
58 int main()
59 {
60     int casen=0;
61     int mj[15];
62     char s[100];
63     bool ok;
64     while(cin>>s)
65     {
66         if(s[0]==‘0‘) break;
67         printf("Case %d:",++casen);
68         mj[0]=convert(s);
69         for(int i=1;i<13;i++)
70         {
71             cin>>s;
72             mj[i]=convert(s);
73         }
74         ok=false;
75         for(int i=0;i<34;i++)//枚举听牌
76         {
77             memset(c,0,sizeof(c));
78             for(int j=0;j<13;j++) c[mj[j]]++;
79             if(c[i]>=4) continue;
80             c[i]++;
81             if(check())
82             {
83                 ok=true;
84                 printf(" %s",mahjong[i]);
85             }
86             c[i]--;
87         }
88         if(!ok)
89             printf(" Not ready");
90         printf("\n");
91     }
92     return 0;
93 }
时间: 2024-08-06 15:41:09

UVa中国麻将(Chinese Mahjong,Uva 11210)的相关文章

uva 11210 Chinese Mahjong(暴力枚举)

uva 11210 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

11210 - Chinese Mahjong(dfs)

题目:11210 - Chinese Mahjong 题目大意:给出十三个麻将, 问再取哪一个能胡?把所有的情况列出来,并且按照题目要求的顺序.胡的条件需要一个而且仅一个对,然后剩下要么是三个相同的,要么是三个连续的(前提是后缀相同,并且只有 T, S, W在考虑范围内) 解题思路:把要取的情况一个个枚举出来,然后dfs, 找是否加入这个可以胡就可以了,找的话就三种情况去判断一下.但是要注意:三个连续要注意需要后缀相同,我这里是把麻将转成数字,然后统计这个麻将出现的次数,所以需要些边界的判断.

初识中国余数定理 (Chinese Remainder Theorem)

初识中国余数定理 (Chinese Remainder Theorem) 中国余数定理介绍 起源: 在<孙子算经>中有这样一个问题: "今有物不知其数,三三数之剩二(除以3余2),五五数之剩三(除以5余3),七七数之剩二(除以7余2),问物几何?" 这个问题称为"孙子问题",该问题的一般解法国际上称为"中国剩余定理". 解法: 设未知数为X 1. 找到三个数: 1). 从3和5的公倍数中找到被7除余1的数,15: 2). 从5和7的公

UVA 11210 Chinese Mahjong(中国麻将 , 大模拟)

解题思路: 关键在于如何判断十四张牌能否和牌,这里采用dfs来判断,详情看代码. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map>

UVa 11210 Chinese Mahjong (暴力,递归寻找)

题意:这个题意.有点麻烦,就是说给定13张牌,让你求能“听”的牌.(具体的见原题) 原题链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2151 析:看到这个题,真是麻烦啊,我又不懂麻将,看了好久才明白什么是“听”.分析一下思路. 首先对所有的牌都进行编号,然后暴力,首先的是先判断是哪个是将,然后再进一步判断, 哪一个是刻子,和顺子

UVA 11210 中国麻将

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2151 http://7xjob4.com1.z0.glb.clouddn.com/f1186ae9a93d903ab533e5fce524bac6 题意:给你一副手牌,输出这手牌所有的听牌 思路:枚举所有34种牌,依次判断是否听这牌,先枚举选出将,再枚举顺子.刻子等,递归判断. 1 #in

UVA 11210 Chinese Mahjong

#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #inc

UVa 11210 (DFS) Chinese Mahjong

大白书第一章的例题,当时看起来很吃力,现如今A这道题的话怎么写都无所谓了. 思路很简单,就是枚举胡哪张牌,然后枚举一下将牌,剩下如果能找到4个顺子或者刻子就胡了. 由于粗心,34个字符串初始化写错,各种WA. 1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <iostream> 5 using namespace std; 6 7 int a[13], c[34]

UVA - 315 Network 和 UVA - 10199 (求割顶)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21278 求割顶的裸题. UVA - 315 #include <algorithm> #include <iostream> #include <sstream> #include <cstrin