dp-Find The Marble

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 integers ai 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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll dp[55][55][55];//前i次看到k次,目标在j 
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,k,s;
		scanf("%d%d%d%d",&n,&m,&k,&s);
		memset(dp,0,sizeof(dp));
		dp[0][s][0]=1;
		int a,b;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&a,&b);
			for(int j=1;j<=n;j++){
				for(int p=0;p<=k;p++){
					if(p!=0){
						if(j==b){//要在b,那已经知道在a 
							dp[i][j][p]+=dp[i-1][a][p-1];
						}else if(j==a){//要在a,那已经知道在b 
							dp[i][j][p]+=dp[i-1][b][p-1];
						}else{//要在j,与本次交换无关,已经知道在j 
							dp[i][j][p]+=dp[i-1][j][p-1];
						}
					}
					dp[i][j][p]+=dp[i-1][j][p];
				}
			}
		}
		ll maxn=0;
		int res=0;
		for(int i=1;i<=n;i++){
			if(maxn<dp[m][i][k]){
				maxn=dp[m][i][k];
				res=i;
			}
		}
		printf("%d\n",res);
	}
	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
*/
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值