现在的位置: 首页 > 综合 > 正文

ZOJ 3605 Find the Marble(三维dp)

2014年09月05日 ⁄ 综合 ⁄ 共 2546字 ⁄ 字号 评论关闭

n个杯子,有一个石头放在第s号杯子里,然后交换这些杯子m次,只能记住k次,每次交换被忘掉的概率相同,那个猜的人最可能猜哪号杯里有石头。

dp[i][j][t] 代表从第i个交换中有j次没看到。然后这是第t个情况为dp[i][j][t]。选择一个最大的dp[m][m-k][ti]输出ti。状态转移: if(能看见) if(t==x) dp[i][j][t] += dp[i-1][j][y] else if(t == y) dp[i][j][t] += dp[i-1][j][x] else dp[i][j][t] += dp[i-1][j][t]。

if(看不见) dp[i][j][t] += dp[i-1][j-1][t]。

注意这里是枚举所有的可能所以最终的dp表示由看的见和看不见的和加起来。

初始化的时候要把dp[0][0][s] = 1。因为一个都不看的时候只在s上。

最后遍历输出最大可能性的结果。

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 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 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 integers ai 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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898

const int maxn = 110;
using namespace std;

LL dp[maxn][maxn][maxn];

struct node
{
    int x, y;
} f[maxn];

int main()
{
    int T;
    cin >>T;
    while(T--)
    {
        int n, m, k, s;
        cin >>n>>m>>k>>s;
        memset(dp, 0 , sizeof(dp));
        for(int i = 1; i <= m; i++)
            cin >>f[i].x>>f[i].y;
        dp[0][0][s] = 1;
        for(int i=1; i<=m; i++)
        {
            for(int j=0; j<=i; j++)//从0开始;
            {
                for(int t=1; t<=n; t++)
                {
                    if(j<i)
                    {
                        if(t == f[i].x)
                            dp[i][j][t] += dp[i-1][j][f[i].y];
                        else if(t == f[i].y)
                            dp[i][j][t] += dp[i-1][j][f[i].x];
                        else
                            dp[i][j][t] += dp[i-1][j][t];
                    }
                    if(j>0)
                        dp[i][j][t]+=dp[i-1][j-1][t];
                }
            }
        }
        s=1;
        for(int t=2; t<=n; t++)
            if(dp[m][m-k][t]>dp[m][m-k][s])
                s=t;
       cout<<s<<endl;
    }
    return 0;
}



抱歉!评论已关闭.