Project Euler:Problem 84 Monopoly odds

In the game,Monopoly, the standard board is set up in the following way:

GOA1CC1A2T1R1B1CH1B2B3JAIL

H2C1

T2U1

H1C2

CH3C3

R4R2

G3D1

CC3CC2

G2D2

G1D3

G2JF3U2F2F1R3E3E2CH2E1FP

A player starts on the GO square and adds the scores on two 6-sided dice to determine the number of squares they advance in a clockwise direction. Without any further rules we would expect to visit each square with equal probability: 2.5%. However, landing on G2J (Go To Jail), CC (community chest), and CH (chance) changes this distribution.

In addition to G2J, and one card from each of CC and CH, that orders the player to go directly to jail, if a player rolls three consecutive doubles, they do not advance the result of their 3rd roll. Instead they proceed directly to jail.

At the beginning of the game, the CC and CH cards are shuffled. When a player lands on CC or CH they take a card from the top of the respective pile and, after following the instructions, it is returned to the bottom of the pile. There are sixteen cards in each pile, but for the purpose of this problem we are only concerned with cards that order a movement; any instruction not concerned with movement will be ignored and the player will remain on the CC/CH square.

Community Chest (2/16 cards):Advance to GOGo to JAILChance (10/16 cards):

The heart of this problem concerns the likelihood of visiting a particular square. That is, the probability of finishing at that square after a roll. For this reason it should be clear that, with the exception of G2J for which the probability of finishing on it is zero, the CH squares will have the lowest probabilities, as 5/8 request a movement to another square, and it is the final square that the player finishes at on each roll that we are interested in. We shall make no distinction between "Just Visiting" and being sent to JAIL, and we shall also ignore the rule about requiring a double to "get out of jail", assuming that they pay to get out on their next turn.

By starting at GO and numbering the squares sequentially from 00 to 39 we can concatenate these two-digit numbers to produce strings that correspond with sets of squares.

Statistically it can be shown that the three most popular squares, in order, are JAIL (6.24%) = Square 10, E3 (3.18%) = Square 24, and GO (3.09%) = Square 00. So these three most popular squares can be listed with the six-digit modal string: 102400.

If, instead of using two 6-sided dice, two 4-sided dice are used, find the six-digit modal string.

英语太渣还是看中文翻译比较不容易理解错题意

玩家从GO开始,每次将两个六面骰子的得分相加来决定他下一步走向的格子(顺时针方向)。如果没有其他规则的话,我们可以预料每个格子被访问到的几率是相等的:2.5%。但是,如果落到格子G2J(去监狱JAIL),格子CC(公益金),格子CH(机会)的话,这个分布就会被改变。

除了G2J以及CC和CH中的一张牌能够让玩家直接进监狱(JAIL)以外,如果玩家连续三次掷骰子得到两个相同的数字,,那么第三次玩家也将直接进监狱。。每个牌堆中有16张牌,但是简单起见我们在此题目中只考虑涉及到移动的牌,任何不涉及到移动的牌将会被忽略,而且玩家仍然将处于CC/CH格子上。CC (2/16 张牌):去到 GO去到 JAILCH(10/16 张牌):

从GO开始对格子编号(00到39),我们将这些两位数的数字相连接来得到与格子的集合相对应的字符串。

从统计上来说可以得到三个最受欢迎的格子按顺序是,JAIL(6.24%)= 10号格子,E3(3.18%)= 24号格子,GO(3.09%)= 00号格子。所以这三个最受欢迎的格子可以用一个六位的字符串表示:102400。如果我们用两个4面的骰子代替两个6面的骰子,找出代表三个最受欢迎格子的六位字符串。import randomdef shuffle_16card():#随机洗牌deck=[i for i in range(1,17)]random.shuffle(deck)return deckdef next_rail(pos):if pos in range(0,5):return 5if pos in range(35,40):return 5if pos in range(5,15):return 15if pos in range(15,25):return 25if pos in range(25,35):return 35def next_utility(pos):if pos in range(0,12):return 12if pos in range(28,40):return 12if pos in range(12,28):return 28def cc(card,pos):if card==1:return 0elif card==2:return 10else:return posdef chance(card,pos):if card==1:return 0elif card==2:return 10elif card==3:return 11elif card==4:return 24elif card==5:return 39elif card==6:return 5elif card == 7 or card==8:return next_rail(pos)elif card==9:return next_utility(pos)elif card==10:pos=pos-3if pos < 0:pos=40+posreturn poselse:return posdef dice_num():a=random.randrange(1,5)b=random.randrange(1,5)if a== b:doubles=Trueelse:doubles=Falsereturn(a+b,doubles)def main():chancedeck=shuffle_16card()ccdeck=shuffle_16card()pos=0doublecount=0count=0result=[0 for i in range(40)]while count <1000000000:count=count+1dice=dice_num()if dice[1] == True:doublecount=doublecount+1else:doublecount=0if doublecount==3:pos=10doublecount=0else:pos=pos+dice[0]if pos >= 40:pos=pos-40if pos == 2 or pos == 17 or pos == 33:t1=ccdeck.pop()ccdeck.insert(0,t1)pos=cc(t1,pos)if pos==7 or pos == 22 or pos == 36:t2=chancedeck.pop()chancedeck.insert(t2,pos)pos=chance(t2,pos)if pos == 30:pos=10result[pos]=result[pos]+1print(result)ans=[[i,result[i]] for i in range(40)]ans.sort(key=lambda x:x[1])print(ans)if __name__ == '__main__':main()

版权声明:本文为博主原创文章,未经博主允许不得转载。

生活是一段奇妙的旅行,就在那一去无返的火车上。

Project Euler:Problem 84 Monopoly odds

相关文章:

你感兴趣的文章:

标签云: