斗地主 (NOIP2015 Day1 T3)

时间:2022-05-14 15:14:49

            斗地主

斗地主 (NOIP2015 Day1 T3)

思路 :读入时注意将A作为第14张牌,因为它可以连在K后,

总体思路为 先出炸弹和四带二 再出三带一 再把对牌和单牌出完 记录并更新Answer,后枚举顺子,并继续向下搜索。

注意:弄明白题意,题目描述不太清楚。。。。另外,我觉的牌的花色只是能用来区分大小王。另外在整顺子之前也可以用贪心来搞其他类似四带二,三带一等的牌

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int Max = ;
int T, N, Answer ;
int card [Max];
int Count [Max];
void DFS (int X)
{
if (X > Answer ) return;
int rest = ;
memset (Count, , sizeof (Count));
for (int i = ; i <= ; i++)
Count [card[i]]++;
for (; Count []; )
{
Count []--; //炸弹
rest++;
if (Count [] >= ) //判断能不能带2
Count [] -= ;
else if (Count [] >= )
Count [] -= ;
}
for (; Count []; )
{
Count []--; //三张牌
rest++;
if (Count []) //能不能带一
Count []--;
else if (Count [])
Count []--;
}
if (card [] && card [] && Count [] >= ) //看看剩下的单牌有没有王炸
rest--;
rest += Count [] + Count [];
Answer = min (rest + X, Answer );
for (int i = , j; i <= ; ++i) //单顺子
{
for (j = i; card [j] && j <= ; ++j)
{
card [j]--;
if (j - i + >= ) //如果剩下的牌多于五张 ,则向下搜索
DFS (X + );
}
for (; j > i; )
card [--j]++;
}
for (int i = , j; i <= ; ++i) //双顺子
{
for (j = i; card [j] >= && j <= ; ++j)
{
card [j] -= ;
if (j - i + >= )
DFS(X + );
}
for (; j > i; )
card [--j] += ;
}
for (int i = , j; i <= ; ++i) // 三顺子
{
for (j = i; card [j] >= && j <= ; ++j)
{
card [j] -= ;
if (j - i + >= )
DFS (X + );
}
for (; j>i;)
card [--j] += ;
}
}
int main()
{
cin >> T >> N;
int A, B;
while (T--)
{
memset (card, , sizeof (card));
Answer = N;
for (int i = ; i <= N; i++)
{
cin >> A >> B;
if (A == ) card [B - ]++; //王 要分开存 ,因为他们不能组成对子 ,不能当对牌出
else if (A == ) card []++;
else card [A]++;
}
DFS ();
cout << Answer << endl;
}
return ;
}