前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZOJ 3605 Find the Marble(dp)

ZOJ 3605 Find the Marble(dp)

作者头像
ShenduCC
发布2018-04-26 16:20:41
4550
发布2018-04-26 16:20:41
举报
文章被收录于专栏:算法修养算法修养

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
代码语言:javascript
复制
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
代码语言:javascript
复制
2
1
3
三维DP dp[i][j][p]表示前i个转换,看到了j个,球的位置是p#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>

using namespace std;
long long int dp[55][55][55];
int n;
int m,k,s;
int a[55][2];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&m,&k,&s);
        for(int i=1;i<=m;i++)
            scanf("%d%d",&a[i][0],&a[i][1]);
        memset(dp,0,sizeof(dp));
        dp[0][0][s]=1;
        for(int i=1;i<=m;i++)
        {
            for(int j=0;j<=k;j++)
            {
                for(int p=1;p<=n;p++)
                {
                    dp[i][j][p]+=dp[i-1][j][p];
                    if(a[i][0]==p)
                        dp[i][j+1][a[i][1]]+=dp[i-1][j][p];
                    else if(a[i][1]==p)
                        dp[i][j+1][a[i][0]]+=dp[i-1][j][p];
                    else
                        dp[i][j+1][p]+=dp[i-1][j][p];
                }
            }
        }
        int ans=1;
        for(int i=2;i<=n;i++)
            if(dp[m][k][ans]<dp[m][k][i]) ans=i;
        printf("%d\n",ans);
    }
    return 0;

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-04-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Input
  • Outout
  • Sample Input
  • Sample Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档