UVA 1637 Double Patience 概率DP

游戏开发与AI音视频处理技术综述
本文深入探讨了游戏开发领域的关键技术,包括游戏引擎、编程语言、硬件优化等,并重点介绍了AI音视频处理在游戏开发中的应用,如语义识别、语音识别、图像处理等。此外,文章还涵盖了相关开发工具和测试方法,为游戏开发者提供全面的技术指导。




Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

 Status

Description

Download as PDF

Double Patience is a single player game played with a standard 36-card deck. The cards are shuffled and laid down on a table in 9 piles of 4 cards each, faces up.

After the cards are laid down, the player makes turns. In a turn he can take top cards of the same rank from any two piles and remove them. If there are several possibilities, the player can choose any one. If all the cards are removed from the table, the player wins the game, if some cards are still on the table and there are no valid moves, the player loses.

George enjoys playing this patience. But when there are several possibilities to remove two cards, he doesn't know which one to choose. George doesn't want to think much, so in such case he just chooses a random pair from among the possible variants and removes it. George chooses among all possible pairs with equal probability.

For example, if the top cards are KS, KH, KD, 9H, 8S, 8D, 7C, 7D, and 6H, he removes any particular pair of (KS, KH), (KS, KD), (KH, KD), (8S, 8D), and (7C, 7D) with the equal probability of 1/5.

Once George's friend Andrew came to see him and noticed that he sometimes doesn't act optimally. George argued, that it is not important - the probability of winning any given patience with his strategy is large enough.

Help George to prove his statement - given the cards on the table in the beginning of the game, find out what is the probability of George winning the game if he acts as described.

Input 

The input file contains several test cases, each of them consists of nine lines. Each line contains the description of four cards that form a pile in the beginning of the game, from the bottom card to the top one.

Each card is described with two characters: one for rank, one for suit. Ranks are denoted as `6' for six, `7' for seven, `8' for eight, `9' for nine, `T' for ten, `J' for jack, `Q' for queen, `K' for king, and `A' for ace. Suits are denoted as `S' for spades, `C' for clubs, `D' for diamonds, and `H' for hearts. For example, ``KS" denotes the king of spades.

Card descriptions are separated from each other by one space.

Output 

For each test case, output a line with one real number - the probability that George wins the game if he plays randomly. Your answer must be accurate up to 10-6 .

Sample Input 

AS 9S 6C KS
JC QH AC KH
7S QD JD KD
QS TS JS 9H
6D TD AD 8S
QC TH KC 8D
8C 9D TC 7C
9C 7H JH 7D
8H 6S AH 6H

Sample Output 

0.589314

Source


Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths ::  Examples

 Status




#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

double dp[5][5][5][5][5][5][5][5][5];
bool vis[5][5][5][5][5][5][5][5][5];
char dui[10][5];

char str[4][5];

double dfs(int w1,int w2,int w3,int w4,int w5,int w6,int w7,int w8,int w9)
{
    if(vis[w1][w2][w3][w4][w5][w6][w7][w8][w9])
        return dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];
    vis[w1][w2][w3][w4][w5][w6][w7][w8][w9]=true;
    double& x =dp[w1][w2][w3][w4][w5][w6][w7][w8][w9];
    bool flag=false;
    int top[10]={w1,w2,w3,w4,w5,w6,w7,w8,w9};
    for(int i=0;i<9;i++)
        if(top[i]) {flag=true; break;}
    if(flag==false)
    {
        return x=1.0;
    }

    int qu=0;
    double sum=0.0;
    for(int i=0;i<9;i++)
    {
        if(top[i]!=0)
        for(int j=i+1;j<9;j++)
        {
            if(top[j]==0) continue;
            if(dui[i][top[i]]==dui[j][top[j]])
            {
                top[i]--; top[j]--;
                qu++;
                sum+=dfs(top[0],top[1],top[2],top[3],top[4],top[5],top[6],top[7],top[8]);
                top[i]++; top[j]++;
            }
        }
    }
    if(sum>0) x=sum/(1.*qu);
    return x;
}

int main()
{
	while(scanf("%s%s%s%s",str[0],str[1],str[2],str[3])!=EOF)
	{
	    memset(dp,0,sizeof(dp));
	    memset(vis,0,sizeof(vis));

		for(int i=0;i<4;i++)
			dui[0][i+1]=str[i][0];
		for(int i=0;i<8;i++)
		{
			scanf("%s%s%s%s",str[0],str[1],str[2],str[3]);
			for(int j=0;j<4;j++)
				dui[i+1][j+1]=str[j][0];
		}
		dfs(4,4,4,4,4,4,4,4,4);
		printf("%.6lf\n",dp[4][4][4][4][4][4][4][4][4]);
	}
	return 0;
}



在深度学习领域,尤其是在使用DNN(深度神经网络)进行训练时,参数`patience`通常用于早停策略(Early Stopping)。 ### 含义 `patience`代表在验证集上的性能指标(如损失值、准确率等)没有得到改善的情况下,训练过程可以继续进行的最大轮数(epochs)。例如,当`patience`设置为5时,意味着如果在连续5个训练轮次中,验证集上的性能都没有提升,训练过程就会停止。 ### 作用 - **防止过拟合**:在训练神经网络时,随着训练轮数的增加,模型在训练集上的性能通常会不断提高,但在验证集上的性能可能会在达到某个点后开始下降,这就是过拟合的表现。通过设置`patience`,可以在验证集性能不再提升时及时停止训练,避免模型过度学习训练数据中的噪声和细节,从而提高模型的泛化能力。 - **节省计算资源**:如果模型已经在验证集上达到了较好的性能,继续训练可能是不必要的,反而会浪费大量的计算资源和时间。使用早停策略可以在合适的时机停止训练,提高训练效率。 ### 使用方法 以下是一个使用Python和Keras库实现早停策略并设置`patience`的示例代码: ```python from tensorflow.keras.callbacks import EarlyStopping from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense import numpy as np # 生成一些示例数据 X_train = np.random.rand(1000, 10) y_train = np.random.randint(0, 2, 1000) X_val = np.random.rand(200, 10) y_val = np.random.randint(0, 2, 200) # 构建一个简单的DNN模型 model = Sequential() model.add(Dense(64, activation='relu', input_shape=(10,))) model.add(Dense(1, activation='sigmoid')) # 编译模型 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # 定义早停策略 early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) # 训练模型 history = model.fit(X_train, y_train, epochs=100, validation_data=(X_val, y_val), callbacks=[early_stopping]) ``` 在上述代码中,`EarlyStopping`回调函数用于实现早停策略,`monitor='val_loss'`表示监控验证集的损失值,`patience=5`表示如果验证集损失值在连续5个轮次中没有下降,训练将停止,`restore_best_weights=True`表示在训练停止后,模型将恢复到验证集损失值最小的那一轮的权重。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值