您的位置:首页 > 其它

zoj2686 Cycle Game---dfs 找规律 博弈

2012-09-15 10:01 232 查看
CycleGame

TimeLimit:5Seconds
MemoryLimit:32768KB

Hereisagameplayedonacyclebytwoplayers.Theruleofthisgameisasfollows:Atfirst,acycleisgivenandeachedgeisassignedanon-negativeinteger.Amongthoseintegers,atleastoneiszero.Furtheracoinisputonavertexofthecycle.
Fromthisvertex,thegamestartsandproceedswithtwoplayers'alternatingmoveswiththefollowingseriesofchoices:

Chooseanedgeincidentwiththevertexhavingthecoin,
Decreasethevalueofthisedgetoanynon-negativeintegerstrictly,
Movethecointotheadjacentvertexalongthisedge.

Thegameendswhenaplayeronhisturncannotmovebecausethevalueofeachedgeincidentwiththevertexhavingthecoinisequaltozero.Then,thatplayeristheloser.

Figure1illustratesanactualgame.Inthisgame,AliceisthefirstplayerandBobisthesecondplayer.InthestartingpositioninFigure1(a),Alicecannotbutchoosetherightedgeofthevertexhavingthecoin.Alicethendecreasesitsvaluefrom
2to0,andmovesthecoinalongthisedge,whichmakes(a)into(b).Next,Bobcannotbutchoosethedownedgeofthevertexhavingthecoin;hethendecreasesitsvaluefrom5to1,whichmakes(b)into(c).InFigure1(c),Alicechoosestheupedgeof
thevertexhavingthecoinanddecreasesitsvaluefrom1to0,whichmakes(c)into(d).Finally,inFigure1(d),Bobhasnomovesinceeachedgeincidentwiththevertexhavingthecoinisassignedtozero.Then,Alicewinsthisgame.



Figure1:Anexampleofcyclegame(Acoinisputontheblackvertex)
Infact,wheneverthegamestartsasshowninFigure1(a),thefirstplayercanalwayswinforanysecondplayer'smove.Inotherwords,inthestartingpositioninFigure1(a),thefirstplayerhasawinningstrategy.Inthisproblem,youshoulddetermine
whetherornotthefirstplayerhasawinningstrategyfromagivenstartingposition.

Input

TheinputconsistsofTtestcases.Thenumberoftestcases(T)isgivenonthefirstlineoftheinputfile.EachtestcasestartswithalinecontaininganintegerN(3<=N<=20),whereNisthenumberofverticesinacycle.Onthenextline,there
aretheNnon-negativeintegersassignedtotheedgesofthecycle.TheNintegersaregiveninclockwiseorderstartingfromthevertexhavingthecoinandtheyareseparatedbyasinglespace.NotethatatleastoneintegervalueamongtheNintegersmust
bezeroandthatthevalueofnointegercanbelargerthan30.

Output

Printexactlyonelineforeachtestcase.Thelineistocontain"YES"ifthefirstplayerhasawinningstrategyfromthestartingposition.Otherwise,thelineistocontain"NO".Thefollowingshowssampleinputandoutputfortwotestcases.

SampleInput

2
4
2530
3
000

SampleOutput

YES
NO


刚看这题的时候就觉得是水题,也不管数据,咔咔的敲完,一提交,5001s华丽的TLE~

其实这题是有规律的,可惜我没看出来,悲哀~

朝两个方向,只要某个方向的连续的非0个数为奇数,先手就有必胜策略。

参考:/article/2565968.html

[code]#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#defineC240
#defineTIME10
#defineinf1<<25
#defineLLlonglong
usingnamespacestd;
intn,a[30];
intslove(intm)
{
//顺时针找非0个数,如果是奇数,直接返回必胜
intcnt=0;
for(inti=m;cnt<n;i=(i+1)%n)
if(a[i]==0)
break;
else
cnt++;
if(cnt&1)
return1;
//逆时针找非0个数,如果是奇数,直接返回必胜
/*cnt=0;
for(inti=(m-1+n)%n;cnt<n;i=(i-1+n)%n)
if(a[i]==0)
break;
else
cnt++;
if(cnt&1)
return1;*/
for(inti=-1;i<=1;i+=2)
{
intk;
if(i==1)
k=m;
else
k=(m-1+n)%n;
for(intj=1;j<=a[k];j++)
{
a[k]-=j;
if(!slove((m+i+n)%n))
{
a[k]+=j;
return1;
}
a[k]+=j;
}
}
return0;
}
intmain()
{
intt;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(inti=0;i<n;i++)
scanf("%d",&a[i]);
puts(slove(0)?"YES":"NO");
}
return0;
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: