跳到正文

题解

「ZMOI Round #4-C」纸牌(Cards) 题解

linziyang1约 16 分钟
原文
文章信息与版本

文章信息

发布
2024年4月10日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 16 分钟

版本

  1. v1当前

题目大意

相较于 UNO,本题的改动大致如下:

  1. 加入了团队规则,使出牌、罚牌、结算等都以团队为单位;
  2. 若无牌可出,则摸一张牌后无法直接打出;
  3. 当只剩两个玩家时,出 ReverseReverse 效果不为 SkipSkip,即跳过另外一个玩家;
  4. 抢牌只能抢点数牌。

具体详见题目描述。

思路

本题为大模拟,时间、空间上也具有限制,可能需要 set,mapset,map 等数据结构存储。

其余按照题目描述模拟即可。

具体详见代码。

代码

数据生成器(方便对拍):Link。

C++
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
inline int read(){int ret=0;char ch;do{ch=getchar();}while(!isdigit(ch));do{ret=ret*10+(ch-'0'),ch=getchar();}while(isdigit(ch));return ret;}
inline string Read(){string ret="";char ch;do{ch=getchar();}while(!isdigit(ch)&&!isupper(ch));do{ret+=ch,ch=getchar();}while(isdigit(ch)||isupper(ch));return ret;}
struct Card{
	int op, col, num;
	bool operator < (const Card y) const {return op!=y.op? op<y.op:num!=y.num? num < y.num:col<y.col;}
	bool operator == (const Card y) const {return num==y.num&&col==y.col;} // 全等
};
struct Color{
	int col, cnt;
	bool operator < (const Color y) const {
		return cnt!=y.cnt?cnt>y.cnt:col<y.col;
	}
};
Card cards[500005]; // 牌堆中的卡牌
Card lst; // 当前最后一个玩家打的牌
map<Card, set<int> > owners; // 拥有这张牌的玩家
map<int, multiset<Card> > crdd[50005]; // 每名玩家拥有花色的手牌
map<int, int> cntt[50005];//每个团队拥有每种花色牌的数量
multiset<Card> crd[50005]; // 每名玩家的手牌
set<int> plist; // 当前玩家列表
set<int> tmlist; // 存活的团队编号
set<int> member[50005]; // 当前团队中成员列表
set<Color> se[50005]; // 每个团队拥有每种花色牌的数量为第一关键字,该花色编号为第二关键字自动排序
set<int>::iterator npp; // 当前回合玩家在 plist 内的指针
int n,m,k,l;
int cnt; // 出局的玩家数量
int np; // 当前回合的玩家
int ord; // 当前出牌的方向,顺时针为 1,逆时针为 -1
int nowcrd; // 之前摸的牌是牌堆中第几张牌
int pun,skp;// pun:当前罚牌的累计数量,skp:当前跳过的累计数量
int tmpun,tmskp;// tmpun:当前最后累计罚牌的人所在的团队,tmskp:当前最后累计跳过的人所在的团队
int score[50005]; // 团队在本局的得分
int t[50005]; // 每名玩家的团队
bool flag; // 当前是否有未生效的 Wild draw 4
void init(){
	for (int i = 1; i <= n; ++i) crd[i].clear(),crdd[i].clear();
	for (int i = 1; i <= m; ++i) member[i].clear(),cntt[i].clear(),score[i] = 0,se[i].clear();
	tmlist.clear();
	owners.clear();
	plist.clear();
	nowcrd=flag=cnt=np=skp=tmpun=tmskp=pun=0;ord=1;
}
int getNum(string st) { // 获取牌面对应的牌
	if (st.size()==1&&!isdigit(st[0])) {
		if (st[0]=='J') return 11;
		if (st[0]=='Q') return 12;
		if (st[0]=='K') return 13;
	}else if (st.size()==2&&!isdigit(st[0])) {
		if (st=="SJ") return 14;
		if (st=="BJ") return 15;
	}else{
		int ret = 0;
		for (char it:st){
			if (isdigit(it)) ret = ret*10+(int)(it-'0');
		}
		return ret;
	}
}
void finish(int no) { // 判断一个玩家是否打完牌
	if (!crd[no].size()) {
		++cnt;
		score[t[no]]+=n-cnt;
		if (*npp==no&&ord==1){ // 这里要注意在删除前进行指针转移,否则出牌顺序会出现错误
			if (npp==plist.begin()) npp=plist.end();
			--npp;
		}else if (*npp==no&&ord==-1){
			++npp;
			if (npp==plist.end()) npp=plist.begin();
		}
		plist.erase(no);
		member[t[no]].erase(no);
		if (!member[t[no]].size()) {
			tmlist.erase(t[no]);
		}
	}
}
void grab(Card da) { // 尝试抢牌操作
	if (owners[da].size()) {
		auto noo = owners[da].lower_bound(np);
		if (ord==-1) {
			if (noo!=owners[da].end()) np=*(noo);
			else np=*owners[da].begin();
		}else{
			np=*(--noo);
		}
		npp=plist.find(np);
		for (int no:owners[da]){ // 对于每个抢牌者进行出牌操作
			int sz1=crd[no].size();
			crd[no].erase(da);
			int modify = sz1-(int)crd[no].size();
			if (cntt[t[np]][da.col]) se[t[np]].erase({da.col, cntt[t[np]][da.col]});
			if (cntt[t[np]][da.col]-modify>0)se[t[np]].insert({da.col, cntt[t[np]][da.col]-modify});
			cntt[t[np]][da.col]-=modify;
			crdd[no][da.col].erase(da);
			finish(no);
		}
	}
	owners[da].clear(); // 清空本牌的拥有者列表
}
void wild(Card noww) { // 处理万能牌
	auto it=se[t[np]].begin();
	int coll=(*it).col; // 目标颜色
	if (coll==0&&se[t[np]].size()>1) ++it,coll=(*it).col;
	else if(coll==0) coll=lst.col;
	lst.col=coll; // 更改颜色
	if (noww.num==15) { // Wild draw 4
		pun+=4;
		flag = 1; // 标记 Wild draw 4 未处理
		tmpun=t[np]; // 更新最后出罚牌的人所在的团队
	}
}
bool punish() { // 处理罚牌和过程中摸牌操作
	int punn = ((tmpun!=t[np]&&tmpun!=0)?pun:1); // 当前玩家应摸的牌
	for (int i = 1; i <= punn; ++i) {
		if (nowcrd >= k-1) return puts("-1"),1;
		crd[np].insert(cards[++nowcrd]);
		owners[cards[nowcrd]].insert(np);
		crdd[np][cards[nowcrd].col].insert(cards[nowcrd]);
		if (cntt[t[np]][cards[nowcrd].col]) se[t[np]].erase({cards[nowcrd].col, cntt[t[np]][cards[nowcrd].col]});
		se[t[np]].insert({cards[nowcrd].col, ++cntt[t[np]][cards[nowcrd].col]});
	}
	if (punn>1) flag=tmpun=0, pun=0; // 执行了罚牌,重置罚牌数量和不生效团队
	return 0;
}
bool play() { // 出牌
	if (!(skp==0||tmskp==t[np])) { // 有累计禁牌效果作用且不为一个团队
		--skp;
		return 0;
	}
	Card da; // 当前玩家打出的牌
	if (crd[np].size()==crd[np].count(*crd[np].begin())&&(*crd[np].begin()).op!=1) {
		return punish();
	}
	if (flag) {
		Card lstt={3,0,15};
		auto mncrd=crd[np].lower_bound(lstt);
		if (mncrd!=crd[np].end()&&(*mncrd).num==15) { // 有 Wild draw 4
			da = *mncrd;
			int sz1 = crdd[da.col].size();
			crdd[np][da.col].erase(da); // 删除牌
			int modify = (sz1-(int)crdd[da.col].size()); // 当前牌数变化量
			if (cntt[t[np]][da.col]) se[t[np]].erase({da.col, cntt[t[np]][da.col]}); // 删除旧数据
			if (cntt[t[np]][da.col]-modify>0)se[t[np]].insert({da.col, cntt[t[np]][da.col]-modify}); // 放入新数据
			cntt[t[np]][da.col]-=modify; // 更新牌数
			owners[da].erase(np);
			crd[np].erase(da);
		}else { // 无牌可出
			return punish();
		}
	}else{
		Card lstt={lst.op, 1,lst.num};
		auto mncrd=crd[np].lower_bound(lstt);
		if (mncrd!=crd[np].end()&&(*mncrd).num==lst.num) { // 有和他点数相等的牌,优先出
			da = *mncrd;
			int sz1 = crdd[da.col].size();
			crdd[np][da.col].erase(da);
			int modify = (sz1-(int)crdd[da.col].size());
			if (cntt[t[np]][da.col]) se[t[np]].erase({da.col, cntt[t[np]][da.col]});
			if (cntt[t[np]][da.col]-modify>0)se[t[np]].insert({da.col, cntt[t[np]][da.col]-modify});
			cntt[t[np]][da.col]-=modify;
			owners[da].erase(np);
			crd[np].erase(da);
		}else if (crdd[np][lst.col].size()) { // 有同花色的牌
			da = *(crdd[np][lst.col].begin());
			int sz1 = crdd[da.col].size();
			crdd[np][da.col].erase(da);
			int modify = (sz1-(int)crdd[da.col].size());
			if (cntt[t[np]][da.col]) se[t[np]].erase({da.col, cntt[t[np]][da.col]});
			if (cntt[t[np]][da.col]-modify>0)se[t[np]].insert({da.col, cntt[t[np]][da.col]-modify});
			cntt[t[np]][da.col]-=modify;
			owners[da].erase(np);
			crd[np].erase(da);
		}else if (crdd[np][0].size()){ // 有万能牌
			da = *(crdd[np][0].begin());
			int sz1 = crdd[da.col].size();
			crdd[np][da.col].erase(da);
			int modify = (sz1-(int)crdd[da.col].size());
			if (cntt[t[np]][da.col]) se[t[np]].erase({da.col, cntt[t[np]][da.col]});
			if (cntt[t[np]][da.col]-modify>0)se[t[np]].insert({da.col, cntt[t[np]][da.col]-modify});
			cntt[t[np]][da.col]-=modify;
			owners[da].erase(np);
			crd[np].erase(da);
		}else {
			return punish();
		}
	}
	if (da.num==13) ord=-ord; // reverse
	finish(np); // 尝试出局
	lst = da;
	if (da.op==1) grab(da);
	else if (da.op==2) {
		if (da.num==11) pun+=2, tmpun=t[np]; // Draw 2
		if (da.num==12) skp+=1, tmskp=t[np]; // Skip
	}
	else wild(da);
	return 0;
}
void solve() {
	init();
	int N=read();
	if (N*n>=k) return puts("-1"), void();
	for (int i = 1; i <= n; ++i) {
		member[t[i]].insert(i);
		plist.insert(i);
		tmlist.insert(t[i]);
		for (int j = 1; j <= N; ++j) {
			Card noww = cards[(i-1)*N+j];
			crd[i].insert(noww);
			owners[noww].insert(i);
			crdd[i][noww.col].insert(noww);
			if (cntt[t[i]][noww.col]) se[t[i]].erase({noww.col, cntt[t[i]][noww.col]});
			se[t[i]].insert({noww.col, ++cntt[t[i]][noww.col]});
		}
	}
	nowcrd = N*n+1;
	while (nowcrd<=k&&cards[nowcrd].op!=1) ++nowcrd; // 一直取直到取出的牌为点数牌
	if (nowcrd > k) return puts("-1"), void();
	lst = cards[nowcrd];
	npp=plist.begin();
	while (tmlist.size()>1){ // 有超过 1 个团队未全部出局
		np=*npp;
		if (play()) return;
		if (ord==1) {
			++npp;
			if (npp == plist.end()) npp=plist.begin();
		} else {
			if (npp==plist.begin()) npp=plist.end();
			--npp;
		}
	}
	for (int i = 1; i <= m; ++i) {
		printf("%d ",score[i]);
	}
	putchar('\n');
}
int main() {
	n=read(),m=read(),k=read(),l=read();
	for (int i = 1; i <= n; ++i) t[i]=read();
	for (int i = 1; i <= k; ++i) cards[i].col=read();
	for (int i = 1; i <= k; ++i) {
		string st=Read();
		cards[i].num = getNum(st);
		if (cards[i].num <= 10)cards[i].op=1;
		else if (cards[i].num <= 13) cards[i].op=2;
		else cards[i].op=3;
	}
	int T=read();
	while (T--) solve();
	return 0;
}
原文