HDU 4431 Mahjong (DFS,暴力枚举,剪枝)
题意:给定 13 张麻将牌,问你是不是“听”牌,如果是输出“听”哪张。
析:这个题,很明显的暴力,就是在原来的基础上再放上一张牌,看看是不是能胡,想法很简单,也比较好实现,结果就是TLE,一直TLE,这不科学啊。。。
好不容易写出来的,竟然TLE。。。心痛。就是先确定一个将牌,然后再对刻子和顺子进行分析,其实是要剪枝的,就是在如果有1张或者两张牌,而你又不能构成刻子的时候,就要返回false,因为这就已经没解了。
这一个剪枝,就AC了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
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"
};
map<string, int> mp;
inline void init(){
for(int i = 0; i < 34; ++i)
mp[string(mahjong[i])] = i;
}
int c[35]; bool dfs(int d){
for(int i = 0; i < 34; ++i){
int cnt = 0;
if(c[i] >= 3){
if(d == 3) return true;
++cnt;
c[i] -= 3;
if(dfs(d+1)){ c[i] += 3; return true; }
c[i] += 3;
} if(i <= 24 && i % 9 <= 6 && c[i] >= 1 && c[i+1] >= 1 && c[i+2] >= 1){
++cnt;
if(d == 3) return true;
--c[i]; --c[i+1]; --c[i+2];
if(dfs(d+1)){ ++c[i]; ++c[i+1]; ++c[i+2]; return true; }
++c[i]; ++c[i+1]; ++c[i+2];
}
if(cnt == 0 && c[i]) return false; //这种牌有但是不符合顺子或者刻子结束这层搜索
if(c[i]) break; //这里剪枝是因为上面的搜索下次遍历的时候还会继续从他这遍历,所以就不用继续下去了
}
return false;
} inline bool check(){
for(int i = 0; i < 34; ++i){
if(c[i] >= 2){
c[i] -= 2;
if(dfs(0)){ c[i] += 2; return true; }
c[i] += 2;
}
}
return false;
}
int ans[50]; inline bool solvefood(){
int food[35];
memcpy(food, c, sizeof(c));
int cnt = 0;
for(int i = 0; i < 20; i += 9){
--food[i], cnt += food[i];
if(food[i] < 0) return false;
}
for(int i = 8; i < 30; i += 9){
--food[i], cnt += food[i];
if(food[i] < 0) return false;
}
for(int i = 27; i < 34; ++i){
--food[i], cnt += food[i];
if(food[i] < 0) return false;
}
return cnt == 1;
} inline bool solveseven(){
for(int i = 0; i < 34; ++i)
if(c[i] != 2 && c[i] != 0) return false;
return true;
} int main(){
char s[10];
init();
int T; scanf("%d", &T);
while(T--){
memset(c, 0, sizeof(c));
for(int i = 0; i < 13; ++i){
scanf("%s", s);
++c[mp[string(s)]];
} int cnt = 0;
for(int i = 0; i < 34; ++i){
if(c[i] >= 4) continue;
++c[i];
if(solvefood()) ans[cnt++] = i;
else if(solveseven()) ans[cnt++] = i;
else if(check()) ans[cnt++] = i;
--c[i];
} if(!cnt) puts("Nooten");
else{
printf("%d", cnt);
for(int i = 0; i < cnt; ++i)
printf(" %s", mahjong[ans[i]]);
printf("\n");
}
}
return 0;
}
HDU 4431 Mahjong (DFS,暴力枚举,剪枝)的更多相关文章
- HDU - 4431 Mahjong (模拟+搜索+哈希+中途相遇)
题目链接 基本思路:最理想的方法是预处理处所有胡牌的状态的哈希值,然后对于每组输入,枚举每种新加入的牌,然后用哈希检验是否满足胡牌的条件.然而不幸的是,由于胡牌的状态数过多(4个眼+一对将),预处理的 ...
- HDU 5778 abs (暴力枚举)
abs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem De ...
- HDU 1015.Safecracker【暴力枚举】【8月17】
Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is lo ...
- HDU 4431 Mahjong(枚举+模拟)(2012 Asia Tianjin Regional Contest)
Problem Description Japanese Mahjong is a four-player game. The game needs four people to sit around ...
- HDU 4431 Mahjong 模拟
http://acm.hdu.edu.cn/showproblem.php?pid=4431 不能说是水题了,具体实现还是很恶心的...几乎优化到哭但是DFS(还加了几个剪枝)还是不行...搜索一直T ...
- HDU - 5128The E-pang Palace+暴力枚举,计算几何
第一次写计算几何,ac,感动. 不过感觉自己的代码还可以美化一下. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意: 在一个坐标系中,有n个 ...
- HDU 4431 Mahjong(模拟题)
题目链接 写了俩小时+把....有一种情况写的时候漏了...代码还算清晰把,想了很久才开写的. #include <cstdio> #include <cstring> #in ...
- 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)
题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...
- HDU 5339 Untitled (暴力枚举)
题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...
随机推荐
- TC SRM 593 DIV1 250(dfs)
这图最多3色就可以 搜2就行了 #include <iostream> #include<cstdio> #include<cstring> #include< ...
- 【Todo】深入理解Javascript系列
真的很好,要看 http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html
- Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-nu ...
- 用net匹配并替换iOS标准的emoji表情符号
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespaceCommo ...
- Spring MVC 的请求参数获取的几种方法
通过@PathVariabl注解获取路径中传递参数 @RequestMapping(value = "/{id}/{str}") public ModelAndView hello ...
- 用JAVA代码构造一个日历
package day0603; import java.text.ParseException; import java.text.SimpleDateFormat; import java.uti ...
- hdu 4604 Deque(最长不下降子序列)
从后向前对已搜点做两遍LIS(最长不下降子序列),分别求出已搜点的最长递增.递减子序列长度.这样一直搜到第一个点,就得到了整个序列的最长递增.递减子序列的长度,即最长递减子序列在前,最长递增子序列在后 ...
- [Sciter系列] MFC下的Sciter–2.Sciter中的事件,tiscript,语法
[Sciter系列] MFC下的Sciter–2.Sciter中的事件,tiscript,CSS部分自觉学习,重点说明Tiscript部分的常见语法和事件用法. 本系列文章的目的就是一步步构建出一个功 ...
- Python中文乱码的处理
为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x96\x87”的形式? 为什么会报错“UnicodeEncodeError: 'asc ...
- 【Servlet】doGet()与doPost()的区别
doGet与doPost的区别 .Servlet接口只定义了一个服务方法--service .当发出客户端请求时,调用service方法并传递一个请求和响应对象 .使用时经常在doPost()中调用d ...