zjuoj 3605 Find the Marble

在本文中,我们深入探讨了一项名为“石头与杯子”的游戏,Alice通过快速交换杯子,使得Bob只能看到部分操作。面对有限的线索,Bob如何猜测石头所在的杯子?本文详细介绍了解题思路与算法实现。

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

 

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

Author: GUAN, Yao
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

 

 

分析;

Alice和Bob在玩一个游戏,该游戏需要n个杯子和一个石头,开始时石头被罩在在某个杯子里,Alice可交换任意两个杯子,经过一系列的交换,由Bob猜石头在哪个杯子里,交换总共m步,但Bob只看到了其中的k步,问Bob猜哪个杯子的可能性最大。

第一感觉就是和组合(在n个数里取m个有多少种)很相似,再看题目因为顺序是一定的,即选了k步之后,顺序只有一种。

c[n][m]=c[n-1][m-1]+c[n-1][m];

具体编码的时候只考虑到交换的两个杯子而忘记其他杯子在选择时的值也要加上c[n-1][m-1];

 

AC代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #define MAXN 55
 4 using namespace std;
 5 
 6 long long dp[MAXN][MAXN][MAXN];
 7 
 8 int main()
 9 {
10     int T,n,m,s,k,i,j,t,a[MAXN],b[MAXN];
11     scanf("%d",&T);
12     while(T--)
13     {
14         scanf("%d%d%d%d",&n,&m,&k,&s);
15         for(i=1;i<=m;i++)
16             scanf("%d%d",a+i,b+i);
17         memset(dp,0,sizeof(dp));
18         dp[0][0][s]=1;
19         for(i=1;i<=m;i++)
20         {
21             dp[i][0][s]=1;
22             for(j=1;j<=i&&j<=k;j++)
23             {
24                 dp[i][j][b[i]]=dp[i-1][j-1][a[i]];
25                 dp[i][j][a[i]]=dp[i-1][j-1][b[i]];
26                 for(t=1;t<=n;t++)
27                 {
28                     dp[i][j][t]+=dp[i-1][j][t];
29                     if(t!=a[i]&&t!=b[i])//最开始忘记考虑的一种情况
30                         dp[i][j][t]+=dp[i-1][j-1][t];
31                 }
32             }
33         }
34         /*for(i=1;i<=m;i++,putchar('\n'))
35             for(j=0;j<=k;j++,putchar('\n'))
36                 for(t=1;t<=n;t++)
37                     printf("%d ",dp[i][j][t]);
38         printf("\n");*/
39         for(s=1,t=2;t<=n;t++)
40             if(dp[m][k][t]>dp[m][k][s])
41                 s=t;
42         printf("%d\n",s);
43     }
44     return 0;
45 }
View Code

 

转载于:https://www.cnblogs.com/jeff-wgc/p/4472584.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值