CF63C Bulls and Cows 超详细题解

链接: 原题链接

题目描述

The “Bulls and Cows” game needs two people to play. The thinker thinks of a number and the guesser tries to guess it.

The thinker thinks of a four-digit number in the decimal system. All the digits in the number are different and the number may have a leading zero. It can’t have more than one leading zero, because all it’s digits should be different. The guesser tries to guess the number. He makes a series of guesses, trying experimental numbers and receives answers from the first person in the format " xx bulls yy cows". xx represents the number of digits in the experimental number that occupy the same positions as in the sought number. yy represents the number of digits of the experimental number that present in the sought number, but occupy different positions. Naturally, the experimental numbers, as well as the sought number, are represented by four-digit numbers where all digits are different and a leading zero can be present.

For example, let’s suppose that the thinker thought of the number 0123. Then the guessers’ experimental number 1263 will receive a reply “1 bull 2 cows” (3 occupies the same positions in both numbers and 1 and 2 are present in both numbers but they occupy different positions). Also, the answer to number 8103 will be “2 bulls 1 cow” (analogically, 1 and 3 occupy the same positions and 0 occupies a different one).

When the guesser is answered “4 bulls 0 cows”, the game is over.

Now the guesser has already made several guesses and wants to know whether his next guess can possibly be the last one.

输入格式

输出格式

If the input data is enough to determine the sought number, print the number with four digits on a single line. If it has less than four digits, add leading zero. If the data is not enough, print “Need more data” without the quotes. If the thinker happens to have made a mistake in his replies, print “Incorrect data” without the quotes.

题目大意

农夫约翰想要将N(1 <= N <= 100,000)奶牛和公牛排在一排,以便在年度展会上展示。
FJ观察到公牛最近非常好斗; 如果两条公牛队在一条线上太靠近,他们会争辩并开始战斗,破坏了演示。资源丰富,FJ计算出任何两只公牛必须在它们之间至少有K(0 <= K <N)奶牛才能避免战斗。
FJ希望你通过计算可以避免任何战斗的N头公牛和奶牛的可能序列来帮助他。FJ认为所有公牛都是一样的,所有的母牛都是一样的; 因此,如果两个序列在​​某个位置具有不同种类的牛,则它们只是不同的。
输入描述:
第1行:两个以空格分隔的整数:N和K.

输出描述:

第1行:一个整数,表示FJ可以创建这样一系列牛的方式。由于此数字可能非常大,因此输出模数为5,000,011的模数。

输入输出样例
输入
2
1263 1 2
8103 2 1
输出
Need more data
输入
1234 2 2
1256 0 2
输出
2134
输入
2
0123 1 1
4567 1 2
输出
Incorrect data

思路讲解

我们用Total记录有多少个数属于思考者所给出的数。
先看有答案的样例:
先看第一组数据,2公2母,Total是4,说明正确答案就是1 2 3 4这四个数的一种组合方式。再根据2公2母,公的个数就是位置对的个数,母就是位置错的的个数,那么就是说第一组数据有两个位置对,两个位置错。
再看第一二组数据:
0公2母,没有位置对的,有2个位置错的,那么我们先对比一下第一组和第二组数据,既然第二组有2个位置错的数据,那么肯定和第一组有2个公共数据,就是1和2。现在可以确定1和2的位置是错的,那么再看第一组数据,既然1和2的位置是错的,因为2公2母,则3和4的位置是对的,那么答案即为把1和2的位置交换一下:2134

再看第一组样例(need more data):
两组数据Total都是3,说明每组都有3个数字是正确答案中的数字。但是我们看这两组数据,公共数字只有1和3,那么还有一个到底是哪个?其实不能确定。再细看,第一组数据1公2母,1个数字位置对,两个不对,第二组数据2公1母,再结合前面的1和3是公共数字,那么可以确定3的位置是对的,而1的位置是错的,然后第一组剩下的2和6,第二组剩下的8和0到底哪两个是答案数字,无法判断,缺少数据。

最后第三个样例(错误数据):
第一组数据Total为2,第二组数据Total为3,那么按道理应该至少有1个公共数字吧,但是却没有,很明显的数据错误。

那么该怎么用解决这个问题呢?

我们目标是找到尽量一个3位或4位数,满足所给数据的条件,可以采用暴力枚举的方法,枚举3位或4位数,使得满足所给条件的数唯一,因为如果有多个数满足的话,条件一定是不够的,因为题目中思考者只想出一个数字。

我们用cnt来记录枚举后找到的满足条件的数的个数
如果是1,则找到的就是正确答案,如果cnt大于1,则条件不足,如果没找到,即cnt=0,则数据存在错误。

那么怎么确定cnt的数值呢?
首先我们确定枚举的范围:123-9876,

我们把每一组所猜测的数a分成4个数,用num数组记录这4个数。(如果x为a的其中一个数,则num[x]=1)

再找出每一组数据上有几个数属于所给数,如果x属于,则num[x]为1,用sum求和记录即可。

再把枚举的数j的也分成4个数,用bull记录枚举的数和猜测的数对应位置数值相等的个数,一一比较每个位置上的数的值,如果相等,则bull++(位置正确加一)

用v[j]数组记录j符合几组所给条件。
判断:如果bull和每一组数据所给的正确位置数的b相等,并且sum-bull(所有属于所给数的个数-位置对的个数即为是所给数的一个数但位置不对的个数)=c,则v[j]++。
再对123-9876枚举
当v[j]==n即满足所有条件,时cnt++。
只有cnt等于1时,输出正确答案。

代码实现

#include<bits/stdc++.h>
using namespace std;
const int MAXN=10005;
int n,a,b,c;
int num[MAXN];
int bull;
int main()
{
	cin>>n;
	int v[MAXN];//记录遍历的数字j符合的次数 
	memset(v,0,sizeof(v));
	for(int i=1;i<=n;i++)
	{
		cin>>a>>b>>c;
		memset(num,0,sizeof(num));
		num[a%10]=num[(a/10)%10]=num[(a/100)%10]=num[a/1000]=1;//每一组猜词的每一个数字的数组记为1
		for(int j=123;j<=9876;j++)//暴力搜索的范围 //四位不重复的数字 
		{
			int sum=num[j%10]+num[(j/10)%10]+num[(j/100)%10]+num[j/1000];匹配的数字总数:不考虑对应位置 
			int p=j%10,q=(j/10)%10,r=(j/100)%10,s=j/1000;//将每一个数字分开 
			if(p==q||p==r||p==s||q==r||q==s||r==s)continue;//如果存在相同的数字,continue 保证每个数不重复 
			int bull=0;//公牛数(位置正确的数)b 
			if(p==a%10) bull++;//
			if(q==(a/10)%10)bull++;
			if(r==(a/100)%10)bull++;
			if(s==a/1000)bull++;//位置正确bull++ 
			if(bull==b&&sum-bull==c)v[j]++;//如果公牛和母牛的个数和 b、c对应 ,则v[]++; 
		}
		
	}
	int ans,cnt=0;
	for(int i=123;i<=9876;i++)
	{
		if(v[i]==n)//v[]与n相等时 cnt++,表示满足几个猜的数的答案的个数 
		{
			cnt++;
			ans=i;
		}		
	}
	if(cnt==1)cout<<ans/1000<<(ans/100)%10<<(ans/10)%10<<ans%10;//满足的答案唯一,即为正确 
	else if(cnt>1)cout<<"Need more data";//答案不唯一,则缺少数据 
	else cout<<"Incorrect data";//无答案,则存在错误 
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值