您的位置:首页 > 其它

ZOJ 3605 Find the Marble(dp啊 三维)

2015-04-22 19:13 337 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4708

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 n, m, k 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
≤ ai, bi ≤ 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


Author: GUAN, Yao

Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

题意:

一共有 n 个杯子,有一个石头开始是放在第 s 号杯子里,然后交换这些杯子 m 次,但是只能记住其中的 k
次,每次交换没有看到交换的概率相同,求最终猜的那个人最可能猜的是哪号杯子里有石头。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
long long dp[57][57][57];
//dp[i][j][k]表示交换了i次,看到了j次,最后石子在k位置的方案数
int main()
{
	int t;
	int n, m, k, s;
	int a[117], b[117];
	scanf("%d",&t);
	while(t--)
	{
		memset(dp, 0,sizeof(dp));
		scanf("%d%d%d%d",&n,&m,&k,&s);
		dp[0][0][s] = 1;
		int i, j, h;
		for(i = 1; i <= m; i++)
		{
			scanf("%d%d",&a[i],&b[i]);
		}
		for(i = 1; i <= m; i++)
		{
			dp[i][0][s] = 1;
			for(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(h = 1; h <= n; h++)//没看到石子交换
				{
					//dp[i][j][h]来自两个地方,一个是没有看到交换,还有一个是看到了交换但是被子里没有石子
					dp[i][j][h] += dp[i-1][j][h];
					if(h!=a[i] && h!=b[i])//看到交换,但是交换的两个杯子中都没有石子
					{
						dp[i][j][h] += dp[i-1][j-1][h];
					}	
				}
			}
		}
		int ans = 1;
		for(i = 2; i <= n; i++)
		{
			if(dp[m][k][i] > dp[m][k][ans])
			{
				ans = i;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: