ZOJ3605-Find the Marble(可能性DP)

简介:
Find the Marble
Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 ofm 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 integerN (N ≈ 100), then N cases follow.

The first line of each test case contains 4 integers nmk 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 ≤ aibi ≤ 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个容器,M次两两交换。当中K次是能够知道的,一開始珠子放在当中一个容器里。问你交换完以后,珠子在哪个容器的概率最大
 
思路:用DP[m][k][n] 表示 m次交换,知道了当中的k次,结尾为n的方案数
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using  namespace std;
typedef long long ll;
const  int maxn = 60;
int n,m,k,s;
int A[maxn],B[maxn];
ll dp[maxn][maxn][maxn];
int main(){

    int ncase;
    cin >> ncase;
    while(ncase--){
        scanf("%d%d%d%d",&n,&m,&k,&s);
        for(int i = 1; i <= m; i++){
            scanf("%d%d",&A[i],&B[i]);
        }
        memset(dp,0,sizeof dp);
        dp[0][0][s] = 1;
        for(int i = 1; i <= m; i++){
            dp[i][0][s] = 1;
            for(int j = 1; j <= i&&j <= 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 d = 1; d <= n; d++){
                    dp[i][j][d] += dp[i-1][j][d];
                    if(d!=A[i]&&d!=B[i]){
                        dp[i][j][d] += dp[i-1][j-1][d];
                    }
                }
            }
        }
        int idx = 1;
        for(int i = 2; i <= n; i++){
            if(dp[m][k][i] > dp[m][k][idx]){
                idx = i;
            }
        }
        cout<<idx<<endl;
    }
    return 0;
}


 

版权声明:本文博客原创文章,博客,未经同意,不得转载。





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4643620.html,如需转载请自行联系原作者


相关文章
|
5月前
CF 1538 G. Gift Set (贪心+思维)
【6月更文挑战第14天】
36 0
|
6月前
|
算法 测试技术
Big Event in HDU(dp算法)
Big Event in HDU(dp算法)
light oj 1231-1232 - 1233- Coin Change 背包
暂时到半懂不懂也没办法讲明白,就不误人子弟了,直接贴代码了。
35 0
hdoj 1028/poj 2704 Pascal's Travels(记忆化搜索||dp)
有个小球,只能向右边或下边滚动,而且它下一步滚动的步数是它在当前点上的数字,如果是0表示进入一个死胡同。求它从左上角到右下角到路径数目。 注意, 题目给了提示了,要用64位的整数。
37 0
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
LeetCode contest 187 1437. 是否所有 1 都至少相隔 k 个元素 Check If All 1's Are at Least Length K Places Away
|
存储 测试技术
牛客hot100--BM50---两数之和(简单难度)
牛客hot100--BM50---两数之和(简单难度)
133 0
牛客hot100--BM50---两数之和(简单难度)
|
人工智能 BI
CF1398C. Good Subarrays(思维 前缀和)
CF1398C. Good Subarrays(思维 前缀和)
105 0
洛谷P1216-[USACO1.5][IOI1994]数字三角形 Number Triangles(DP)
洛谷P1216-[USACO1.5][IOI1994]数字三角形 Number Triangles(DP)
洛谷P1216-[USACO1.5][IOI1994]数字三角形 Number Triangles(DP)
|
Java
HDOJ 1420 Prepared for New Acmer(DP)
HDOJ 1420 Prepared for New Acmer(DP)
88 0