代码中有注释,应该看得懂
判断游戏不会结束时,大概设置了参数为13 if (giveCount[i] > 13*q[i].size() ) 没偿试13换成其他参数行不行,反正2是不行的
#include <cstdlib>
#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
int main()
{
int testCase;
cin >> testCase;
for (int testCount = 1; testCount <= testCase; testCount++){
queue<int> q[11];
int count[11], give[11],end[11],giveCount[11],total=0;
//count是每个人的数数,give是给下一个人的牌,end是每一个玩家最后丢弃的牌,giveCount是每两张丢弃的牌数数的间隔
//total是记录被最后一个人丢弃牌的总数
bool finish = false, unwin = false;
memset(count,0,sizeof(count));
memset(giveCount,0,sizeof(giveCount));
memset(end,0,sizeof(end));
int player;
cin >> player;
int num;
for (int j = 0 ; j < 52; j++){
cin >> num;
q[0].push(num);
}
while (!finish && !unwin)
{
memset(give,0,sizeof(give));
finish = true;
for (int i = 0 ; i < player; i++){
if (!q[i].empty()){
finish = false;
if (count[i] + 1 == q[i].front()){ //数数与当前牌匹配
giveCount[i] = 0;
give[i+1] = q[i].front();
end[i] = q[i].front();
q[i].pop();
if ( i == player - 1){ //最后一个人出完牌
total++;
if (total >=52) //丢弃牌大于52,则游戏结束
finish = true;
}
}
else{//不匹配
q[i].push(q[i].front());
q[i].pop();
giveCount[i]++;
if (giveCount[i] > 13*q[i].size() )
unwin = true;
}
count[i] = (count[i]+1)%13;
}
if (give[i] != 0 ) //加上上一个玩家所给的牌
q[i].push(give[i]);
}
}
cout << "Case " << testCount << ":";
if (finish)
for (int i = 0 ; i < player; i++)
cout <<" "<<end[i];
if (unwin)
cout << " unwinnable";
cout << endl;
}
return 0;
}