有一天,n名士兵在日出时(惊讶地)袭击了一座城堡。
每个士兵都带着一个教规和一支步枪。
城堡有力量。
在第一天,每个士兵在城堡中射击他的正典,花费城堡n点力量(即城堡以s = s-n强度点结束第一天)。在所有士兵开除后,城堡派遣dpw防御者与他们作战。
在随后的日子里,城堡和士兵按照这些规则进行斗争:
所有士兵先开火。一名士兵可以解雇他的经典 城堡或他的步枪在其中一个防御者(但不是两个和每个 士兵只能射一次)。对一名后卫的一次射门杀死了他。一 射击城堡的力量减少了1点。
然后每个d防守者射杀一名士兵(只有一名)杀死他。
如果城堡仍有强度点(即s> 0),则此时会发送一批新的dpw防御者。下一轮的防守者总数将为d = d + dpw。
每个新的一天重复1到3次。
如果所有士兵都被捍卫者杀死,城堡就会胜出。
如果在士兵射击和城堡力量为零之后没有防守者,那么士兵就会获胜。
我们想知道士兵赢得比赛所需的最少轮数(或者如果不可能,则返回-1)。
我们还希望了解每轮开始时士兵,防御者和城堡力量的数量,以获得最少数量的解决方案。
为了找到我编写这个python程序(正常工作)的最小轮次数
global dpw
# called when the castleStrenght=0. At this point
# the game goes on with soldiers killing defenders and
# defenders killing soldiers but there are no new
# defenders being added since the castle is gone
def kill(soldiers,totalDefenders):
if totalDefenders <= 0:
return 0
if soldiers <=0:
return 999999999
soldiersLeft = soldiers - (totalDefenders - soldiers)
totalDefendersLeft = totalDefenders - soldiers
return 1 + kill(soldiersLeft, totalDefendersLeft)
# simulates one round of the game
def round(soldiers, totalDefenders, castleHits):
if soldiers <= 0:
return 999999999
if castleHits <= 0:
return kill(soldiers, totalDefenders - dpw)
mini = 999999999
for i in range(0,soldiers):
if (i > totalDefenders):
break
soldiersLeft = soldiers - (totalDefenders - i)
totalDefendersLeft = totalDefenders - i + dpw
castleHitsLeft = castleHits - (soldiers - i)
mini = min(mini, 1 + round(soldiersLeft, totalDefendersLeft, castleHitsLeft))
return mini
# run the game
def attackCastle(soldiers,castleHits,defendersPerRound):
global dpw
dpw = defendersPerRound
ret = round(soldiers,0,castleHits)
if ret >= 99999999:
return -1
else:
return ret
gameSoldiers = 10
castleStrength = 11
defendersPerWave = 15
#prints 4
print attackCastle(gameSoldiers, castleStrength, defendersPerWave)
然而,我在如何在每轮开始时获得士兵数量,城堡力量和全面防御者时遇到困难。我想我需要在递归调用期间传递一个像变量一样的堆栈,但是当我回溯时我需要弹出项目。不知道该怎么做呢?我开始修改上面的代码,就像这个
global dpw
def kill(soldiers,totalDefenders,path):
if totalDefenders <= 0:
return 0
if soldiers <=0:
return 999999999
soldiersLeft = soldiers - (totalDefenders - soldiers)
totalDefendersLeft = totalDefenders - soldiers
path.append({"soldiers":soldiersLeft,"totalDefenders":totalDefendersLeft,
"castleHits":0})
return 1 + kill(soldiersLeft, totalDefendersLeft,path)
def round(soldiers, totalDefenders, castleHits,path):
if soldiers <= 0:
return 999999999
if castleHits <= 0:
path.append({"soldiers":soldiers,"totalDefenders":totalDefenders-dpw,
"castleHits":0})
return kill(soldiers, totalDefenders - dpw,path)
mini = 999999999
for i in range(0,soldiers):
if (i > totalDefenders):
break
soldiersLeft = soldiers - (totalDefenders - i)
totalDefendersLeft = totalDefenders - i + dpw
castleHitsLeft = castleHits - (soldiers - i)
path.append({"soldiers":soldiersLeft,"totalDefenders":totalDefendersLeft,
"castleHits":castleHitsLeft})
mini = min(mini, 1 + round(soldiersLeft, totalDefendersLeft, castleHitsLeft,path))
return mini
def attackCastle(soldiers,castleHits,defendersPerRound):
global dpw
dpw = defendersPerRound
ret = round(soldiers,0,castleHits,[])
if ret >= 99999999:
return -1
else:
return ret
gameSoldiers = 10
castleStrength = 11
defendersPerWave = 15
#prints 4
print attackCastle(gameSoldiers, castleStrength, defendersPerWave)