Find the Marble(线性dp/概率dp)

https://cn.vjudge.net/problem/ZOJ-3605

Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of the pots. After that, Bob cannot see the inside of the pots. Then Alice makes a sequence of swappings and Bob guesses which pot the marble is in. In each of the swapping, Alice chooses two different pots and swaps their positions.

Unfortunately, Alice's actions are very fast, so Bob can only catch k of m swappings and regard these k swappings as all actions Alice has performed. Now given the initial pot the marble is in, and the sequence of swappings, you are asked to calculate which pot Bob most possibly guesses. You can assume that Bob missed any of the swappings with equal possibility.
Input

There are several test cases in the input file. The first line of the input file contains an integer N (N ≈ 100), then N cases follow.

The first line of each test case contains 4 integers n, m, k and s(0 < s ≤ n ≤ 50, 0 ≤ k ≤ m ≤ 50), which are the number of pots, the number of swappings Alice makes, the number of swappings Bob catches and index of the initial pot the marble is in. Pots are indexed from 1 to n. Then m lines follow, each of which contains two integersai and bi (1 ≤ ai, bi ≤ n), telling the two pots Alice swaps in the i-th swapping.
Outout

For each test case, output the pot that Bob most possibly guesses. If there is a tie, output the smallest one.
Sample Input

3
3 1 1 1
1 2
3 1 0 1
1 2
3 3 2 2
2 3
3 2
1 2

Sample Output

2
1
3
一共有 n 个杯子,有一个石头开始是放在第 s 号杯子里,然后交换这些杯子 m 次,但是只能记住其中的 k 次,每次交换没有看到交换的概率相同,求最终猜的那个人最可能猜的是哪号杯子里有石头。

dp[i][j][k]代表交换了i次,看到了j次,石子在k位置

https://blog.csdn.net/u012860063/article/details/45199239

#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
ll dp[55][55][55];
int a[55],b[55]; 
int n,m,k,s;
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
    	scanf("%d%d%d%d",&n,&m,&k,&s);
    	memset(dp,0,sizeof(dp));
    	dp[0][0][s]=1;
    	for(int i=1;i<=m;i++){
    	    scanf("%d%d",&a[i],&b[i]);
	}
	for(int i=1;i<=m;i++){
	    dp[i][0][s]=1;
	    for(int j=1;j<=min(i,k);j++){
		dp[i][j][a[i]]=dp[i-1][j-1][b[i]];//看到石子交换
		dp[i][j][b[i]]=dp[i-1][j-1][a[i]];
		for(int p=1;p<=n;p++) {	//没看到石子交换
		    dp[i][j][p]+=dp[i-1][j][p];//没看到交换
		    if(p!=a[i]&&p!=b[i]) //看到交换,但没石子 
		    dp[i][j][p]+=dp[i-1][j-1][p];
		}
	     }
	}
	int ans=1;
        for(int i=2;i<=n;i++){
            if(dp[m][k][i]>dp[m][k][ans]) ans=i;
	} 
	printf("%d\n",ans);
    }
    return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于没有提供数据集,我提供一个示例代码,用于训练一个深度学习模型来识别岩石图像种类。本示例使用 TensorFlow 框架和 Keras API。 首先,导入必要的库: ```python import os import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers, models from tensorflow.keras.preprocessing.image import ImageDataGenerator ``` 然后,定义一些超参数: ```python batch_size = 32 img_height = 224 img_width = 224 epochs = 20 ``` 接着,使用 ImageDataGenerator 函数来加载和预处理数据集: ```python train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_data_dir = 'path/to/train/data' train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') test_datagen = ImageDataGenerator(rescale=1./255) test_data_dir = 'path/to/test/data' test_generator = test_datagen.flow_from_directory( test_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') ``` 在上面的代码中,我们使用 `flow_from_directory` 函数加载数据集。它会自动将图像分成不同的类别,并将它们转换为 one-hot 编码。 接下来,定义一个卷积神经网络模型: ```python model = models.Sequential([ layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)), layers.MaxPooling2D((2, 2)), layers.Conv2D(64, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Conv2D(128, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Conv2D(256, (3, 3), activation='relu'), layers.MaxPooling2D((2, 2)), layers.Flatten(), layers.Dense(128, activation='relu'), layers.Dropout(0.5), layers.Dense(7, activation='softmax') ]) ``` 在上面的代码中,我们定义了一个包含多个卷积层和全连接层的卷积神经网络。最后一层是一个 softmax 层,将输出的结果转换成概率分布。 然后,编译模型并开始训练: ```python model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(train_generator, epochs=epochs, validation_data=test_generator) ``` 在训练过程中,模型将在训练集上进行训练,并在验证集上进行验证。最终,我们得到一个训练好的深度学习模型,可以用于识别岩石图像种类。 最后,给出标签的代码,假设有以下 7 类岩石: ```python rock_classes = { 0: 'andesite', 1: 'gneiss', 2: 'marble', 3: 'quartzite', 4: 'rhyolite', 5: 'schist', 6: 'slate' } ``` 这些标签对应的数字是 0 到 6。例如,如果模型的输出是 [0.1, 0.2, 0.3, 0.1, 0.1, 0.1, 0.1],则可以将其预测为 'marble' 类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值