Project Euler:Problem 84 Monopoly odds

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

GO A1 CC1 A2 T1 R1 B1 CH1 B2 B3 JAIL
H2   C1
T2   U1
H1   C2
CH3   C3
R4   R2
G3   D1
CC3   CC2
G2   D2
G1   D3
G2J F3 U2 F2 F1 R3 E3 E2 CH2 E1 FP

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()

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

时间: 2024-08-06 11:17:29

Project Euler:Problem 84 Monopoly odds的相关文章

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler:Problem 90 Cube digit pairs

Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. For example, the square number 64

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

Project Euler:Problem 67 Maximum path sum II

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom in triangle.txt (righ

Project Euler:Problem 69 Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9

Project Euler:Problem 78 Coin partitions

Let p(n) represent the number of different ways in which n coins can be separated into piles. For example, five coins can be separated into piles in exactly seven different ways, so p(5)=7. OOOOO OOOO   O OOO   OO OOO   O   O OO   OO   O OO   O   O 

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 55 Lychrel numbers

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindromes so quickly. For example, 349 + 943 = 1292, 1292 + 2921 = 4213 4213 + 3124 = 7337 That is, 349 took three iterations to arrive at a palindrome. Al