1. 程式人生 > >ACM/ICPC 2018亞洲區預選賽北京賽站網路賽 C.Cheat [大模擬]

ACM/ICPC 2018亞洲區預選賽北京賽站網路賽 C.Cheat [大模擬]

題意

有一副撲克,四個人玩遊戲,每個人一開始有13張牌。每個人有各自的策略,問這個遊戲結束後每個人手中牌的情況。

題解

按照題意模擬即可。

AC程式碼

#include<stdio.h>
#include<vector>
#include<string>
#include<map>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
string A[13]={"10","2","3","4","5","6"
,"7","8","9","A","J","K","Q"},S;//字典序 string B[13]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; int ppnum[13]; int NOW,renp,WHO; map<string,int>mp,ans; vector<string>vt[4]; vector<string>Table,Temp; int ishave(int who,int which)//看是否有請求的牌 { int num=0; for(int i=0;i<vt[who]
.size();i++) if(vt[who][i]==B[which]) num++; return num; } string findmi(int who)//找到最小字典序的牌 { int mi=mp[vt[who][0]]-1; for(int i=1;i<vt[who].size();i++) mi=min(mi,mp[vt[who][i]]-1); return A[mi]; } void erasep(int who,string which)//刪除牌 { for(int i=0;i<vt[who].size();i++) if(vt[
who][i]==which) { vt[who].erase(vt[who].begin()+i); return ; } } void player1holder()//第一個人出牌 { if(ishave(0,NOW)) { Table.push_back(B[NOW]); Temp.push_back(B[NOW]); erasep(0,B[NOW]); renp=1; } else { string mi=findmi(0); Table.push_back(mi); Temp.push_back(mi); erasep(0,mi); renp=1; } } bool challenge1()//第一個人置疑 { if((WHO+1)%4==0&&!ishave(0,(NOW+1)%13))return true; int q=ishave(0,NOW); if(renp+q>4)return true; return false; } void player2holder()//第二個人出牌 { int num=ishave(1,NOW); if(num) { renp=num; while(num--) { erasep(1,B[NOW]); Table.push_back(B[NOW]); Temp.push_back(B[NOW]); } } else { string mi=findmi(1); erasep(1,mi); Table.push_back(mi); Temp.push_back(mi); renp=1; } } bool challenge2()//第二個人置疑 { if((WHO+1)%4==1&&!ishave(1,(NOW+1)%13))return true; return false; } void player3holder()//第三個人出牌 { int num=ishave(2,NOW); if(num) { renp=num; while(num--) { erasep(2,B[NOW]); Table.push_back(B[NOW]); Temp.push_back(B[NOW]); } } else { memset(ppnum,0,sizeof(ppnum)); for(int i=0;i<vt[2].size();i++) ppnum[mp[vt[2][i]]-1]++; int minum=100; for(int i=0;i<13;i++) if(ppnum[i]!=0) minum=min(minum,ppnum[i]); for(int i=0;i<13;i++) if(minum==ppnum[i]) { renp=minum; while(minum--) { Table.push_back(A[i]); Temp.push_back(A[i]); erasep(2,A[i]); } break; } } } bool challenge3()//第三個人置疑 { int num=ishave(2,NOW); return num==4; } void player4holder()//第四個人出牌 { int num=ishave(3,NOW); if(num==3||num==4) { renp=num; while(num--) { Table.push_back(B[NOW]); Temp.push_back(B[NOW]); erasep(3,B[NOW]); } } else { for(int i=0;i<num;i++) { Table.push_back(B[NOW]); Temp.push_back(B[NOW]); erasep(3,B[NOW]); } if(vt[3].size()==0) { renp=num; return ; } string mi=findmi(3); Table.push_back(mi); Temp.push_back(mi); erasep(3,mi); renp=num+1; } } bool challenge4()//第四個人置疑 { return vt[WHO].size()==0; } void judge(int AA,int BB) { int dui=1; for(int i=0;i<Temp.size();i++) if(Temp[i]!=B[NOW]) dui=0; if(dui) { for(int i=0;i<Table.size();i++) vt[BB].push_back(Table[i]); } else { for(int i=0;i<Table.size();i++) vt[AA].push_back(Table[i]); } Table.clear(); } void debug() { for(int i=0;i<4;i++) { for(int j=0;j<vt[i].size();j++) cout<<vt[i][j]<<" "; cout<<"\n"; } } bool cmp(string a,string b) { return ans[a]<ans[b]; } int main() { for(int i=0;i<13;i++) mp[A[i]]=i+1; for(int i=0;i<13;i++) ans[B[i]]=i+1; while(cin>>S) { for(int i=0;i<4;i++)vt[i].clear(); Table.clear(); vt[0].push_back(S); //輸入 for(int i=0;i<12;i++) { cin>>S; vt[0].push_back(S); } for(int j=1;j<4;j++) for(int i=0;i<13;i++) { cin>>S; vt[j].push_back(S); } int ok=1; WHO=NOW=renp=0;//開始 while(ok) { Temp.clear(); if(WHO==0)player1holder(); if(WHO==1)player2holder(); if(WHO==2)player3holder(); if(WHO==3)player4holder(); //printf("-----------%d ",WHO),cout<<B[NOW]<<endl; for(int WHOWHO=(WHO+1)%4;WHOWHO!=WHO;WHOWHO=(WHOWHO+1)%4) { if(WHOWHO==0&&challenge1()){judge(WHO,WHOWHO);break;} if(WHOWHO==1&&challenge2()){judge(WHO,WHOWHO);break;} if(WHOWHO==2&&challenge3()){judge(WHO,WHOWHO);break;} if(WHOWHO==3&&challenge4()){judge(WHO,WHOWHO);break;} } WHO=(WHO+1)%4; NOW=(NOW+1)%13; //debug(); for(int i=0;i<4;i++) if(vt[i].size()==0) ok=0; } for(int i=0;i<4;i++) { sort(vt[i].begin(),vt[i].end(),cmp); if(vt[i].size()==0)printf("WINNER\n"); else { cout<<vt[i][0]; for(int j=1;j<vt[i].size();j++) cout<<" "<<vt[i][j]; cout<<"\n"; } } } }