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
SampleOutput
[/code]
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]
相关文章推荐
- Gym - 101775L SOS 博弈 找规律
- HDOJ1517[K(2~9)倍博弈][有规律存在]
- Play a game(巴什博弈丶找规律)
- sgu - 520 - Fire in the Country(bfs + dfs + 博弈)
- hdu 3863 No Gambling (水博弈规律)
- nyoj 358 取石子(五)(Fibonacci博弈)(博弈——找规律)
- nyoj 2359 巴什博弈?(博弈——找规律)
- hdu 1564 Play a game(博弈——找规律)
- hdu 1525 Euclid's Game(博弈——找规律)
- 51nod 1070 Bash游戏 V4(博弈——找规律)
- hdu 1079 Calendar Game (SG函数)(博弈——找规律)
- hdu 1517 A Multiplication Game (博弈——找规律)
- L先生与晨跑(找规律 先手后手博弈)
- 【博弈 && dfs && 状态压缩】HackerRank - permutation-game
- AtCoder Grant Contest 10 F - Tree Game 博弈 dfs
- HDOJ1564Play a game(找规律博弈)
- POJ 1740 A New Stone Game 博弈,规律
- hdu 5754 博弈递推 dfs记忆优化
- nyoj 358 取石子(五)(Fibonacci博弈)(博弈——找规律)
- nyoj 2359 巴什博弈?(博弈——找规律)