zoj 3605 Find the Marble 计数dp

来源:互联网 发布:网页美工和前端工作 编辑:程序博客网 时间:2024/05/16 05:09

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

33 1 1 11 23 1 0 11 23 3 2 22 33 21 2

Sample Output

213

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3605



题意:

有n个盒子,有个宝石在第s个盒子中。第一个个人做m次交换操作,第二个人只看到其中的任意k次操作,每个操作被看到的几率是一样的。问第二个人最可能猜这个宝石在几号盒子。


做法:

dp算方法数。 dp[i][j][k],i表示真实的操作了几次,k表示第二个人看到了几次操作,j表示第几个盒子,dp的值表示这种情况下第一个人有多少种情况会猜宝石在j盒子中。

初始化dp[0][s][0]=1,开始操作数为0时,肯定猜的是在第s个盒子中。

然后转移:

1.ab交换 ab直接互相转移  dp[i][a][k]=dp[i-1][b][k-1];   dp[i][b][k]+=dp[i-1][a][k-1];

2.ab交换  其他的盒子,其中j不等于a且不等于b         dp[i][j][k]+=dp[i-1][j][k-1]

2.不做交换    dp[i][j][k]+=dp[i-1][j][k];


然后把dp[m][?][k],把第二维所有数求最大值就好了,几号最大 输出几


#include <stdio.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <malloc.h>#include <ctype.h>#include <math.h>#include <string>#include <iostream>#include <algorithm>using namespace std;#include <stack>#include <queue>#include <vector>#include <deque>#include <set>#include <map>#define INF 999999999#define eps 0.00001#define LL __int64d#define pi acos(-1.0)long long dp[60][60][60];int main(){long long t;cin>>t;long long n,m,bu,s;while(t--){cin>>n>>m>>bu>>s;memset(dp,0,sizeof dp);dp[0][s][0]=1;for(long long i=1;i<=m;i++)//操作{long long a,b;cin>>a>>b;for(long long j=1;j<=n;j++)//壶{for(long long k=0;k<=bu;k++){if(k!=0){//换if(j==a)dp[i][j][k]+=dp[i-1][b][k-1];if(j==b)dp[i][j][k]+=dp[i-1][a][k-1];if(j!=a&&j!=b)dp[i][j][k]+=dp[i-1][j][k-1];}dp[i][j][k]+=dp[i-1][j][k];//不换 }}}long long maxx=-1;long long id;for(long long j=1;j<=n;j++){if(dp[m][j][bu]>maxx){maxx=dp[m][j][bu];id=j;}//printf("%lld ",dp[m][j][bu]);} //printf("\n");printf("%lld\n",id);}return 0;}/*33 1 1 11 23 1 0 11 23 3 2 22 33 21 2*/


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 体育生没过线怎么办 户主去世房产不能过户怎么办 苍蝇飞到嘴唇上怎么办 苍蝇不小心碰到嘴唇了怎么办 苍蝇老往身上飞怎么办 单位乒乓球比赛有领导参加怎么办 意外看到别人打野战怎么办 骨盆低想顺产要怎么办 右胯比左胯突出怎么办 一岁宝宝骨盆不对称怎么办 入盆了又出来了怎么办 大腿前突小腿后怎么办 英语不好高二了怎么办 断奶后又复吸怎么办 招联金融综合评定不足怎么办 git本地分支比远程高怎么办 娃儿上嘴唇里面破了怎么办 错过了各大招聘怎么办 优秀团员申请表没有获奖情况怎么办 大学生毕业学生登记表涂改了怎么办 c语言挂科了怎么办 吸入腐蚀性气体鼻子流血了怎么办 腰劳损痛的厉害怎么办 两个宝宝斜颈左边力量差怎么办 姿势不正确引起的习惯性斜颈怎么办 一岁宝宝有点斜颈怎么办 六个月宝宝有点斜颈怎么办 5月宝宝有点斜颈怎么办 半岁宝宝头偏怎么办 肌性斜颈成年后怎么办 5个月孩子脖子歪怎么办 宝宝一岁多头歪怎么办 四个月宝宝歪脖子怎么办 1岁宝宝脖子歪怎么办 宝宝脖子是歪的怎么办 小孩脖子睡歪了怎么办 脸部三角区肿了怎么办 面部三角区挤了怎么办 胳膊扭着了肿了怎么办 多囊卵巢综合症治不好怎么办 胳膊受了风发麻怎么办