数据结构线性表,写起来就是蛋疼。。。
下面是代码,用vector写的2.7飘过,还是数组快
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=60;
vector<vector<string> > grid(maxn,vector<string>(maxn));
int cnt[maxn];
int x,y;
bool process(int i)
{
int num=0,pre=1;
x=-1,y=-1;
for(int j=i-1; j>=1; j--)
{
if(cnt[j]>0) num++;
//num++;
if(num==1&&pre)
{
x=j;
pre=0;
}
if(num==3)
{
y=j;
break;
}
}
if(x>0||y>0)
return true;
return false;
}
bool Match(string a,string b)
{
return a[0]==b[0]||a[1]==b[1];
}
bool exchange(int tmp,int pos)
{
if(tmp<=0) return false;
if(Match(grid[tmp][cnt[tmp]-1],grid[pos][cnt[pos]-1]))
{
grid[tmp][cnt[tmp]]=grid[pos][cnt[pos]-1];
cnt[pos]--;
cnt[tmp]++;
return true;
}
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
string a;
while(cin>>a)
{
if(a=="#")
break;
memset(cnt,0,sizeof(cnt));
grid[1][0]=a;
cnt[1]++;
for(int i=2; i<=52; i++)
{
cin>>grid[i][cnt[i]++];
}
for(int i=2; i<=52;)
{
x=-1,y=-1;
if(cnt[i]==0)
{
i++;
continue;
}
if(!process(i))
{
i++;
continue;
}
if(y>0)
{
if(y>0&&Match(grid[y][cnt[y]-1],grid[i][cnt[i]-1]))
{
grid[y][cnt[y]]=grid[i][cnt[i]-1];
cnt[i]--;
cnt[y]++;
i=y;
continue;
}
}
if(x>0)
{
if(x>0&&Match(grid[x][cnt[x]-1],grid[i][cnt[i]-1]))
{
grid[x][cnt[x]]=grid[i][cnt[i]-1];
cnt[i]--;
cnt[x]++;
i=x;
continue;
}
}
i++;
}
int ans=0;
for(int i=1; i<=52; i++)
if(cnt[i]) ans++;
ans>1?printf("%d piles remaining: ",ans):printf("%d pile remaining: ",ans);
bool first=true;
for(int i=1; i<=52; i++)
{
if(cnt[i]&&first)
{
printf("%d",cnt[i]);
first=false;
}
else if(cnt[i])printf(" %d",cnt[i]);
}
cout<<endl;
}
return 0;
}