比赛 |
20120217 |
评测结果 |
WWWWWWEE |
题目名称 |
分球 |
最终得分 |
0 |
用户昵称 |
Citron酱 |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2012-02-17 22:11:42 |
显示代码纯文本
#include <fstream>
#define I_F "balls.in"
#define O_F "balls.out"
const short Maxn=7*2;
const short Maxstep=10;
std::ifstream fin(I_F);
std::ofstream fout(O_F);
struct lb
{
short s[Maxn];
short d, l, x;
lb *prep, *succ;
};
short m,n;
short s[Maxn];
short ansn, ans[Maxstep][Maxn];
bool flag;
inline void Input();
inline bool Ok(const short*);
inline void Copy(const short*, short*);
inline short L(const short, const short);
void Delete(lb*);
bool Search();
inline void Output();
int main()
{
fin>>m;
for (short i=0; i<m; i++)
{
Input();
flag=Search();
Output();
}
fin.close();
fout.close();
}
inline void Input()
{
fin>>n;
short fix=0;
char t=fin.get();
for (short i=0; i<n*2-1; i++)
{
t=fin.get();
if (t=='a')
s[i+fix]=1;
else if (t=='b')
s[i+fix]=2;
else
s[i]=s[i+1]=0,
fix=1;
}
t=fin.get();
}
inline bool Ok(const short *s)
{
short j=0;
for (short i=0; j<n-1; i++)
if (s[i]==1)
j++;
else if (s[i]==2)
return false;
return true;
}
inline void Copy(const short *a, short *b)
{
for (short i=0; i<n*2; i++)
b[i]=a[i];
}
inline short L(const short a, const short b)
{
return (a!=b)?((a==1)?0:1):((a==1)?2:3);
}
void Delete(lb *x)
{
if (x->succ!=NULL)
Delete(x->succ);
delete x;
}
bool Search()
{
if (Ok(s))
{
Copy(s,ans[0]);
ansn=0;
return true;
}
lb *head, *tail;
head=new lb;
head->prep=head->succ=NULL;
head->d=0;
head->l=-1;
Copy(s,head->s);
for (head->x=0; head->s[head->x]>0; head->x++);
tail=head;
for (lb *temp=head; temp!=NULL; temp=temp->succ)
for (short i=0; i<n*2-1; i++)
if (temp->s[i]*temp->s[i+1]>0 && L(temp->s[i],temp->s[i+1])!=temp->l)
{
tail->succ=new lb;
tail=tail->succ;
tail->prep=temp;
Copy(temp->s,tail->s);
tail->s[temp->x]=tail->s[i];
tail->s[temp->x+1]=tail->s[i+1];
tail->s[i]=tail->s[i+1]=0;
tail->d=temp->d+1;
tail->l=L(temp->s[i],temp->s[i+1]);
tail->x=i;
if (Ok(tail->s))
{
ansn=tail->d;
for (lb *j=tail; j!=NULL; j=j->prep)
Copy(j->s,ans[j->d]);
Delete(head);
return true;
}
if (tail->d>Maxstep)
{
Delete(head);
return false;
}
}
Delete(head);
return false;
}
void Output()
{
if (flag)
for (short i=0; i<=ansn; i++)
{
fout<<i<<' ';
for (short j=0; j<n*2; j++)
if (s[j]==1)
fout<<'a';
else if (s[j]==2)
fout<<'b';
else
fout<<' ',
j++;
fout<<std::endl;
}
else
fout<<"NO SOLUTION\n";
}