or<a1 b1>, <a2 b2> and noone didn't fill a triangle, then <a2 b2>, <a1 b1> take the same result as previous.

But I get WA on 6.0 seconds. Is there any trick? Please, help me!<a1 b1>, <a2 b2> and one player filled a triangle in each step, then <a2 b2>, <a1 b1> take the same result as previous.

[cpp]/* Problem 751 Triangle War */

#include <stdio.h>

#define SWAP(a,b) {unsigned c=a;a=b;b=c;}

const unsigned Lines[18][2] = {{1,2},{1,3},{2,3},{2,4},{2,5},{3,5},{3,6},

{4,5},{5,6},{4,7},{4,8},{5,8},{5,9},{6,9},{6,10},{7,8},{8,9},{9,10}} ;

const unsigned Triangles[9][3] = {{0,1,2},{2,4,5},{3,4,7},{5,6,8},

{7,10,11},{8,12,13},{9,10,15},{11,12,16},{13,14,17}};

unsigned A,B,astep,bstep;

unsigned game[18],cc[18],aa[18];

unsigned AddLine(unsigned NLine)

{

unsigned i,j=0,k=0;

for (i=0;i<9;i++)

if (game[Triangles

*[0]] && game[Triangles*

*[1]] && game[Triangles**[2]])*

j++;

game[NLine]=1;

for (i=0;i<9;i++)

if (game[Trianglesj++;

game[NLine]=1;

for (i=0;i<9;i++)

if (game[Triangles

*[0]] && game[Triangles**[1]] && game[Triangles**[2]])*

{

k++ ;

if (astep) A++;

if (bstep) B++;

}

if (!(k-j)) SWAP(astep,bstep);

return k-j;

}

unsigned FAddLine(unsigned NLine)

{

unsigned i,j=0,k=0;

for (i=0;i<9;i++)

if (game[Triangles{

k++ ;

if (astep) A++;

if (bstep) B++;

}

if (!(k-j)) SWAP(astep,bstep);

return k-j;

}

unsigned FAddLine(unsigned NLine)

{

unsigned i,j=0,k=0;

for (i=0;i<9;i++)

if (game[Triangles

*[0]] && game[Triangles**[1]] && game[Triangles**[2]])*

j++ ;

game[NLine]=1;

for (i=0;i<9;i++)

if (game[Trianglesj++ ;

game[NLine]=1;

for (i=0;i<9;i++)

if (game[Triangles

*[0]] && game[Triangles[i][1]] && game[Triangles[i][2]])*

k++ ;

return k-j;

}

void MakeForB(unsigned &A, unsigned &B, unsigned C, unsigned D);

void MakeForA(unsigned &A, unsigned &B, unsigned C, unsigned D)

{

unsigned AA,i,f=1,at,bt,kk[20];

A=B=0;

AA=0;

for (i=0;i<18;i++)

if (game[i]==0)

{

kk[i] = FAddLine(i);

game[i]=0;

if (kk[i]>AA) AA=kk[i];

}

for (int U=AA;U>=0;U--)

{

for (i=0;i<18;i++)

if (game[i]==0)

if (unsigned(U)==kk[i] && ((U && (i>=C)) || ((!U) && (cc[D-1] || (i>=aa[D-1])))))

{

game[i]=1;

cc[D]=U;

aa[D]=i;

if (U) MakeForA(at,bt,i,D+1);

if (!U) MakeForB(at,bt,0,D+1);

at+=U;

if (f || (at>A)) {A=at;B=bt;f=0;}

game[i]=0;

if (!B) break;

}

if (!B) break;

}

}

void MakeForB(unsigned &A, unsigned &B, unsigned C, unsigned D)

{

unsigned BB,i,f=1,at,bt,kk[20];

A=B=0;

BB=0;

for (i=0;i<18;i++)

if (game[i]==0)

{

kk[i] = FAddLine(i);

game[i]=0;

if (kk[i]>BB) BB=kk[i];

}

for (int U=BB;U>=0;U--)

{

for (i=0;i<18;i++)

if (game[i]==0)

if (unsigned(U)==kk[i] && ((U && (i>=C)) || ((!U) && (cc[D-1] || (i>=aa[D-1])))))

{

game[i]=1;

cc[D]=U;

aa[D]=U;

if (U) MakeForB(at,bt,i,D+1);

if (!U) MakeForA(at,bt,0,D+1);

bt+=U;

if (f || (bt>B)) {A=at;B=bt;f=0;}

game[i]=0;

if (!A) break;

}

if (!A) break;

}

}

void main()

{

unsigned t,tt,t2,tt2,f=0;

scanf("%u",&t2);

for (tt2=1;tt2<=t2;tt2++)

{

if (f) printf("\n"); else f=1;

scanf("%u",&t);

for (tt=1;tt<=t;tt++)

{

unsigned n,i,j,x,y;

scanf("%u",&n);

A=B=bstep=0;

astep=1;

for (i=0;i<18;i++) game[i]=0;

unsigned b[18];

for (i=0;i<n;i++)

{

scanf("%u%u",&x,&y);

for (j=0;j<18;j++)

if (Lines[j][0]==x && Lines[j][1]==y)

{

cc[i]=AddLine(j);

if (cc[i]) b[i]=j; else b[i]=0;

aa[i]=j;

break;

}

}

unsigned AA,BB;

if (astep) MakeForA(AA,BB,b[n-1],n); else MakeForB(AA,BB,b[n-1],n);

A+=AA;

B+=BB;

printf("Game %u: ",tt);

if (A>B) printf("A wins.\n"); else printf("B wins.\n");

}

}

}[/cpp]k++ ;

return k-j;

}

void MakeForB(unsigned &A, unsigned &B, unsigned C, unsigned D);

void MakeForA(unsigned &A, unsigned &B, unsigned C, unsigned D)

{

unsigned AA,i,f=1,at,bt,kk[20];

A=B=0;

AA=0;

for (i=0;i<18;i++)

if (game[i]==0)

{

kk[i] = FAddLine(i);

game[i]=0;

if (kk[i]>AA) AA=kk[i];

}

for (int U=AA;U>=0;U--)

{

for (i=0;i<18;i++)

if (game[i]==0)

if (unsigned(U)==kk[i] && ((U && (i>=C)) || ((!U) && (cc[D-1] || (i>=aa[D-1])))))

{

game[i]=1;

cc[D]=U;

aa[D]=i;

if (U) MakeForA(at,bt,i,D+1);

if (!U) MakeForB(at,bt,0,D+1);

at+=U;

if (f || (at>A)) {A=at;B=bt;f=0;}

game[i]=0;

if (!B) break;

}

if (!B) break;

}

}

void MakeForB(unsigned &A, unsigned &B, unsigned C, unsigned D)

{

unsigned BB,i,f=1,at,bt,kk[20];

A=B=0;

BB=0;

for (i=0;i<18;i++)

if (game[i]==0)

{

kk[i] = FAddLine(i);

game[i]=0;

if (kk[i]>BB) BB=kk[i];

}

for (int U=BB;U>=0;U--)

{

for (i=0;i<18;i++)

if (game[i]==0)

if (unsigned(U)==kk[i] && ((U && (i>=C)) || ((!U) && (cc[D-1] || (i>=aa[D-1])))))

{

game[i]=1;

cc[D]=U;

aa[D]=U;

if (U) MakeForB(at,bt,i,D+1);

if (!U) MakeForA(at,bt,0,D+1);

bt+=U;

if (f || (bt>B)) {A=at;B=bt;f=0;}

game[i]=0;

if (!A) break;

}

if (!A) break;

}

}

void main()

{

unsigned t,tt,t2,tt2,f=0;

scanf("%u",&t2);

for (tt2=1;tt2<=t2;tt2++)

{

if (f) printf("\n"); else f=1;

scanf("%u",&t);

for (tt=1;tt<=t;tt++)

{

unsigned n,i,j,x,y;

scanf("%u",&n);

A=B=bstep=0;

astep=1;

for (i=0;i<18;i++) game[i]=0;

unsigned b[18];

for (i=0;i<n;i++)

{

scanf("%u%u",&x,&y);

for (j=0;j<18;j++)

if (Lines[j][0]==x && Lines[j][1]==y)

{

cc[i]=AddLine(j);

if (cc[i]) b[i]=j; else b[i]=0;

aa[i]=j;

break;

}

}

unsigned AA,BB;

if (astep) MakeForA(AA,BB,b[n-1],n); else MakeForB(AA,BB,b[n-1],n);

A+=AA;

B+=BB;

printf("Game %u: ",tt);

if (A>B) printf("A wins.\n"); else printf("B wins.\n");

}

}

}[/cpp]