码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3605 Find the Marble 计数dp

时间:2015-04-19 21:24:22      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:计数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 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

链接: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;
}


/*
3
3 1 1 1
1 2
3 1 0 1
1 2
3 3 2 2
2 3
3 2
1 2

*/


zoj 3605 Find the Marble 计数dp

标签:计数dp

原文地址:http://blog.csdn.net/u013532224/article/details/45132413

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!