Project Euler:Problem 84 Monopoly odds

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

GOA1CC1A2T1R1B1CH1B2B3JAIL
H2 C1
T2 U1
H1 C2
CH3 C3
R4 R2
G3 D1
CC3 CC2
G2 D2
G1 D3
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):
    1. Advance to GO
    2. Go to JAIL
  • Chance (10/16 cards):
    1. Advance to GO
    2. Go to JAIL
    3. Go to C1
    4. Go to E3
    5. Go to H2
    6. Go to R1
    7. Go to next R (railway company)
    8. Go to next R
    9. Go to next U (utility company)
    10. Go back 3 squares.

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)以外,如果玩家连续三次掷骰子得到两个相同的数字,那么第三次玩家也将直接进监狱。

在游戏开始时,CC和CH中的牌是乱序排放的。当玩家落到CC或CH时,他从相应的 牌堆顶上拿一张牌 ,并执行完下面的指令后, 将牌放回到牌堆的底部 。每个牌堆中有16张牌,但是简单起见我们在此题目中只考虑涉及到移动的牌,任何不涉及到移动的牌将会被忽略,而且玩家仍然将处于CC/CH格子上。
  • CC (2/16 张牌):
    1. 去到 GO
    2. 去到  JAIL
  • CH(10/16 张牌):
    1. 去到  GO
    2. 去到  JAIL
    3. 去到  C1
    4. 去到  E3
    5. 去到  H2
    6. 去到  R1
    7. 去到下一个 R (铁路公司)
    8. 去到下一个 R
    9. 去到下一个 U (工具公司)
    10. 回退3格

这个题目的核心问题是每个格子被访问到的可能性。也就是每次掷骰子之后落到某个格子的几率。因此,除了落到G2J的几率为零以外,落到CH的几率最小,因为落到CH后有5/8的几率会再次移动到其他格子。而我们感兴趣的是每次掷骰子之后最终落到的那个格子。我们对“只是访问”和被送到JAIL不作区分,我们也同时忽略需要掷一个double(两个相同的数字)才能走出JAIL的规则,只是假设玩家下一轮自动走出监狱。

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

从统计上来说可以得到三个最受欢迎的格子按顺序是,JAIL(6.24%)= 10号格子,E3(3.18%)= 24号格子,GO(3.09%)= 00号格子。所以这三个最受欢迎的格子可以用一个六位的字符串表示:102400。

如果我们用两个4面的骰子代替两个6面的骰子,找出代表三个最受欢迎格子的六位字符串。

import random

def shuffle_16card():             #随机洗牌
    deck=[i for i in range(1,17)]
    random.shuffle(deck)
    return deck

def next_rail(pos):
    if pos in range(0,5):
        return 5
    if pos in range(35,40):
        return 5
    if pos in range(5,15):
        return 15
    if pos in range(15,25):
        return 25
    if pos in range(25,35):
        return 35

def next_utility(pos):
    if pos in range(0,12):
        return 12
    if pos in range(28,40):
        return 12
    if pos in range(12,28):
        return 28

def cc(card,pos):
    if card==1:
        return 0
    elif card==2:
        return 10
    else:
        return pos

def chance(card,pos):
    if card==1:
        return 0
    elif card==2:
        return 10
    elif card==3:
        return 11
    elif card==4:
        return 24
    elif card==5:
        return 39
    elif card==6:
        return 5
    elif card == 7 or card==8:
        return next_rail(pos)
    elif card==9:
        return next_utility(pos)
    elif card==10:
        pos=pos-3
        if pos < 0:
            pos=40+pos
        return pos
    else:
        return pos
        
def dice_num():
    a=random.randrange(1,5)
    b=random.randrange(1,5)
    if a== b:
        doubles=True
    else:
        doubles=False
    return(a+b,doubles)

def main():
    chancedeck=shuffle_16card()
    ccdeck=shuffle_16card()
    pos=0
    doublecount=0
    count=0
    result=[0 for i in range(40)]
    while count <1000000000:
        count=count+1
        dice=dice_num()
        if dice[1] == True:
            doublecount=doublecount+1
        else:
            doublecount=0
        if doublecount==3:
            pos=10
            doublecount=0
        else:
            pos=pos+dice[0]
            if pos >= 40:
                pos=pos-40
            if 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=10
        result[pos]=result[pos]+1
    print(result)
    ans=[[i,result[i]] for i in range(40)]
    ans.sort(key=lambda x:x[1])
    print(ans)

if __name__ == '__main__':  
    main() 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值