• + 0 comments
    import random
    
    def make_dice(prob):
    
        cum_prob = [0]*6
        sum_prob = 0
        for i,p in enumerate(prob):
            sum_prob += p
            cum_prob[i] = sum_prob
        
        return cum_prob
    
    def roll_dice(cum_prob):
    
        num = None
        x = random.random() 
        if x > cum_prob[4]:
            num = 6
        elif x > cum_prob[3]:
            num = 5
        elif x > cum_prob[2]:
            num = 4
        elif x > cum_prob[1]:
            num = 3
        elif x > cum_prob[0]:
            num = 2
        else:
            num = 1
        
        return num
    
    def play_game(prob, move):
    
        cum_prob = make_dice(prob)
    
        pos = 1
        for count in range(1,1001):
    
            num = roll_dice(cum_prob)
            pos += num
    
            if pos==100:
                break
            if pos>100:
                pos -= num
            if move[pos]:
                pos = move[pos]
        
        return count
    
    # read input data
    size = 100
    n_tests = int(input())
    tests = []
    for _ in range(n_tests):
        test = {}
        line = input()
        test['prob'] = list(map(float, line.split(',')))
    
        input()
    
        test['move'] = [False]*size
        line = input()
        for p in line.split():
            start,end = map(int, p.split(','))
            test['move'][start] = end
    
        line = input()
        for p in line.split():
            start,end = map(int, p.split(','))
            test['move'][start] = end
        
        tests.append(test)
        
    # make simulations
    test_results = []
    
    for test in tests:
    
        prob = test['prob']
        move = test['move']
    
        sum_counts = 0
        n_counts = 0
    
        for _ in range(5000):
            count = play_game(prob, move)
            if count <= 1000:
                sum_counts += count
                n_counts += 1
    
        expected_counts = int(sum_counts/n_counts)
        test_results.append(expected_counts)
        
    # print output
    print('\n'.join(map(str,test_results)))