跑得快
Posted maoruimas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跑得快相关的知识,希望对你有一定的参考价值。
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl
const int maxn=1e2;
int a[maxn],p1[15],p2[15],p1_cnt[15],p2_cnt[15];
int order[15]={0,3,4,5,6,7,8,9,10,11,12,13,1,2},inv_order[15]={0,12,13,1,2,3,4,5,6,7,8,9,10,11};
int num,p1_left,p2_left;
vector<int> bomb,pairs,single;
vector<pair<int,int> > straight,aaab,aaabb;
void shuffle(int a[],int lft,int rgt)
{
for(int i=lft;i<rgt;++i)
{
int p=rand()%(rgt-i)+i;
swap(a[i],a[p]);
}
}
bool cmps(pair<int,int> a,pair<int,int> b)
{
return (a.second<b.second)||((a.first<b.first)&&(a.second==b.second));
}
bool cmpa(pair<int,int> a,pair<int,int> b)
{
return a.first<b.first;
}
void init()
{
srand((unsigned)time(NULL));
//deal
for(int i=1;i<=13;++i)
{
for(int j=1;j<=4;++j)
{
a[4*i+j-4]=i;
}
}
shuffle(a,1,53);
p1_left=p2_left=num=15;//num<=26
memset(p1,0,sizeof p1);
memset(p2,0,sizeof p2);
memset(p1_cnt,0,sizeof p1_cnt);
memset(p2_cnt,0,sizeof p2_cnt);
for(int i=1;i<=num;++i)p1[a[i]]++,p1_cnt[a[i]]++,p2[a[i+num]]++,p2_cnt[a[i+num]]++;
//record
bomb.clear();
pairs.clear();
single.clear();
straight.clear();
aaab.clear();
//bomb;
for(int i=1;i<=13;++i)
{
if(p2[i]==4)
{
bomb.push_back(i);
p2[i]=0;
}
}
//straight
int cnt=0;
for(int i=1;i<=13;++i)
{
if(p2[i]&&i<13)cnt++;
else if(cnt>=5)
{
i=i-cnt-1;
straight.push_back(make_pair(i+1,cnt));
for(int j=0;j<cnt;++j)p2[i+j+1]--;
cnt=0;
}
else cnt=0;
}
sort(straight.begin(),straight.end(),cmps);
//aaab
for(int i=13;i>0;--i)
{
if(p2[i]==3)
{
bool flag=true;
for(int j=1;j<=13;++j)
{
if(p2[j]==1)
{
p2[j]=0;
aaab.push_back(make_pair(i,j));
flag=false;
break;
}
}
if(flag)
{
for(int j=1;j<=13;++j)
{
if(p2[j]==2)
{
p2[j]=0;
aaabb.push_back(make_pair(i,j));
flag=false;
break;
}
}
}
if(flag)
{
for(int j=1;j<=13;++i)
{
if(p2[j]>0)
{
p2[j]--;
aaab.push_back(make_pair(i,j));
flag=false;
break;
}
}
}
if(!flag)p2[i]=0;
}
}
sort(aaab.begin(),aaab.end(),cmpa);
sort(aaabb.begin(),aaabb.end(),cmpa);
//pairs
for(int i=1;i<=13;++i)
{
if(p2[i]>=2)
{
pairs.push_back(i);
p2[i]-=2;
}
}
//single
for(int i=1;i<=13;++i)
{
if(p2[i])single.push_back(i);
}
}
int take(vector<int> &v,int n)
{
int sz=v.size(),ret=v[n];
static int c[maxn];
for(int i=0;i<sz;++i)
{
if(i==n)continue;
c[i]=v[i];
}
v.clear();
for(int i=0;i<sz;++i)
{
if(i==n)continue;
v.push_back(c[i]);
}
return ret;
}
pair<int,int> take(vector<pair<int,int> > &v,int n)
{
int sz=v.size();
pair<int,int> ret=v[n];
static pair<int,int> c[maxn];
for(int i=0;i<sz;++i)
{
if(i==n)continue;
c[i]=v[i];
}
v.clear();
for(int i=0;i<sz;++i)
{
if(i==n)continue;
v.push_back(c[i]);
}
return ret;
}
bool play(int id,int a,int b)
{
int sz;
if(id==0)
{
if(p1_left>=5)
for(int i=6;i>=1;--i)
{
if(play(i,0,0))
{
return true;
}
}
else
for(int i=1;i<=6;++i)
{
if(play(i,0,0))
{
return true;
}
}
}
else if(id==1)
{
sz=bomb.size();
if(sz==0)return false;
for(int i=0;i<sz;++i)
{
if(bomb[i]>a)
{
p2_cnt[bomb[i]]-=4;
printf("Your opponent play %d %d %d %d
",order[bomb[i]],order[bomb[i]],order[bomb[i]],order[bomb[i]]);
take(bomb,i);
p2_left-=4;
return true;
}
}
}
else if(id==2)
{
sz=straight.size();
if(sz==0)return false;
if(b==0)
{
int sta=straight[0].first,len=straight[0].second;
for(int i=0;i<len;++i)p2_cnt[sta+i]--;
take(straight,0);
p2_left-=len;
printf("Your opponent play ");for(int i=0;i<len;++i)printf("%d ",order[sta+i]);printf("
");
return true;
}
else
{
for(int i=0;i<sz;++i)
{
if(straight[i].second==b&&straight[i].first>a)
{
for(int j=0;j<b;++j)p2_cnt[straight[i].first+j]--;
p2_left-=b;
printf("Your opponent play ");for(int j=0;j<b;++j)printf("%d ",order[straight[i].first+j]);printf("
");
take(straight,i);
return true;
}
}
}
}
else if(id==3)
{
sz=aaabb.size();
if(sz==0)return false;
for(int i=0;i<sz;++i)
{
if(aaabb[i].first>a)
{
p2_cnt[aaabb[i].first]-=3;
p2_cnt[aaabb[i].second]-=2;
printf("Your opponent play %d %d %d %d %d
",order[aaabb[i].first],order[aaabb[i].first],order[aaabb[i].first],order[aaabb[i].second],order[aaabb[i].second]);
p2_left-=5;
take(aaabb,i);
return true;
}
}
}
else if(id==4)
{
sz=aaab.size();
if(sz==0)return false;
for(int i=0;i<sz;++i)
{
if(aaab[i].first>a)
{
p2_cnt[aaab[i].first]-=3;
p2_cnt[aaab[i].second]-=1;
printf("Your opponent play %d %d %d %d
",order[aaab[i].first],order[aaab[i].first],order[aaab[i].first],order[aaab[i].second]);
p2_left-=4;
take(aaab,i);
return true;
}
}
}
else if(id==5)
{
sz=pairs.size();
if(sz==0)return false;
for(int i=0;i<sz;++i)
{
if(pairs[i]>a)
{
p2_cnt[pairs[i]]-=2;
printf("Your opponent play %d %d
",order[pairs[i]],order[pairs[i]]);
p2_left-=2;
take(pairs,i);
return true;
}
}
}
else if(id==6)
{
sz=single.size();
if(sz==0)return false;
for(int i=0;i<sz;++i)
{
if(single[i]>a)
{
p2_cnt[single[i]]--;
printf("Your opponent play %d
",order[single[i]]);
p2_left-=1;
take(single,i);
return true;
}
}
}
return false;
}
int main()
{
init();
/*
for(int i=0;i<bomb.size();++i)printf("%d ",bomb[i]);printf("
");
for(int i=0;i<straight.size();++i)printf("(%d,%d) ",straight[i].first,straight[i].second);printf("
");
for(int i=0;i<aaabb.size();++i)printf("(%d,%d) ",aaabb[i].first,aaabb[i].second);printf("
");
for(int i=0;i<aaab.size();++i)printf("(%d,%d) ",aaab[i].first,aaab[i].second);printf("
");
for(int i=0;i<pairs.size();++i)printf("%d ",pairs[i]);printf("
");
for(int i=0;i<single.size();++i)printf("%d ",single[i]);printf("
");
//getchar();
*/
int id,a,b;
id=a=b=0;
while(1)
{
if(!play(id,a,b))printf("Your opponent skips
");
printf("(He has %d cards left...)
",p2_left);
if(p2_left==0)
{
printf("You lose!
");
break;
}
/*
printf("Your opponent‘s cards : ");
for(int i=1;i<=13;++i)
{
for(int j=1;j<=p2_cnt[i];++j)printf("%d ",order[i]);
}
*/
printf("
Your cards : ");
for(int i=1;i<=13;++i)
{
for(int j=1;j<=p1[i];++j)printf("%d ",order[i]);
}
printf("
Play : ");
vector<int> in;
in.clear();
int n,tmp;
scanf("%d",&n);
if(n==0)
{
id=a=b=0;
system("cls");
continue;
}
for(int i=1;i<=n;++i)scanf("%d",&tmp),in.push_back(inv_order[tmp]),p1[inv_order[tmp]]--;
if(in.size()==1)
{
p1_left--;
id=6,a=in[0];
}
else if(in.size()==2)
{
p1_left-=2;
id=5,a=in[0];
}
else if(in.size()==4)
{
p1_left-=4;
if(in[0]==in[3])id=1,a=in[0];
else id=4,a=in[0],b=in[3];
}
else if(in.size()==5)
{
p1_left-=5;
if(in[0]!=in[1])id=2,a=in[0],b=5;
else id=3,a=in[0],b=in[3];
}
else
{
p1_left-=in.size();
id=1,a=in[0],b=in.size();
}
if(p1_left==0)
{
printf("You win
");
break;
}
system("cls");
}
}
以上是关于跑得快的主要内容,如果未能解决你的问题,请参考以下文章