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的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...
随机推荐
- 1287. Mars Canals(DP)
1287 水DP #include <iostream> #include<cstdio> #include<cstring> #include<algori ...
- javac编译过程
编译器把一种语言规范转化为另一种语言规范的这个过程需要哪些步骤:
- memcache的最佳实践方案。
基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...
- 小技巧--让JS代码只执行一次
有时候实在是没办法,就像我这个比赛系统中,有一个弹出框,这个弹出框之外都是模糊的(这是在ajax写出弹出框时,加了一个水印). 然而遇到的问题,也是蹊跷古怪,因为这个弹出框的事件是数据查询事件,但是因 ...
- C#中常见的winform控件命名规范
我们知道Button 常常简称为btn,那么Winform中的其它控件呢,这篇文章在C#的winform控件命名规范 的基础上对一些控件的名称的简称进行了整理. 1. 标准控件 NO. 控件类型简写 ...
- C ~ char int 等数据转换问题
1,char型数字转换为int型 "; printf(]-');//输出结果为3 2,int转化为char (1)字符串转换成数字,用atoi,atol,atof,分别对应的是整型,long ...
- 数据库(class0507)
局部变量_先声明再赋值 声明局部变量 DECLARE @变量名 数据类型 DECLARE @name varchar(20) DECLARE @id int 赋值 SET @变量名 =值 --set用 ...
- Core Java 学习笔记——2.基本数据类型&类型转换
数据类型(8种基本类型:int/short/long/byte/float/double/char/boolean) 整型 int 4字节 -2 147 483 648~2 147 483 647 s ...
- [LeetCode] Add Digits (a New question added)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Python闭包与javascript闭包比较
实例一 python def line_conf(): def line(x): return 2*x+1 print(line(5)) # within the scope line_con ...