本文主要是介绍B - Bulls and Cows CodeForces - 63C(构造,模拟),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Input
The first input line contains an integer n (1 ≤ n ≤ 10) which represents the number of already made guesses. Then follow n lines in the form of “ai bi ci”, where ai is the i-th experimental number, bi is the number of bulls, ci is the number of cows (1 ≤ i ≤ n, 0 ≤ bi, ci, bi + ci ≤ 4). The experimental numbers are correct, i.e., each of them contains exactly four digits, in each of them all the four digits are different, and there can be a leading zero. All the experimental numbers are different. As the guesser hasn’t guessed the number yet, the answer “4 bulls 0 cows” is not present.
Output
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.
Examples
Input
2
1263 1 2
8103 2 1
Output
Need more data
Input
2
1234 2 2
1256 0 2
Output
2134
Input
2
0123 1 1
4567 1 2
Output
Incorrect data
题意: 要你猜一个数字,给你的n个数字和匹配的结果。
思路: 基本思路是模拟,只要模拟不写出问题就没有问题。因为n的范围相当小,所以暴力是没问题的。所以考虑优化,题目给的是相同位置的数字个数和不相同位置的相同数字个数,或许可以从这里考虑一些优化方案。如果n比较小的话,可以把n个数字合在一起,然后状态压缩枚举合法状态;相同位置相同数字的状态不难枚举,但是如果是不同位置的相同数字呢?或许可以考虑状态再多开一个进制,一个进制记录相同位置的相同数字,另外的进制记录不同位置的相同数字。虽然多开了一维,但是状态被压缩的更多了,差别应该也不大。
下面的代码是暴力代码,究极暴力啊。
#include<iostream>
#include<stdio.h>
#include<map>
#include<cstring>
#include<algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;int a[200][200],n;
vector<pair<int,int> >query;
vector<int>ans;
bool judge(int x,int y,int z,int k)
{int b[10];b[1] = x;b[2] = y;b[3] = z;b[4] = k;for(int i = 1;i <= n;i++){int tmp1 = 0,tmp2 = 0;for(int j = 1;j <= 4;j++){if(a[i][j] == b[j])tmp1++;}for(int j = 1;j <= 4;j++){for(int q = 1;q <= 4;q++){if(j == q)continue;if(a[i][j] == b[q])tmp2++;}}if(tmp1 == query[i - 1].first && tmp2 == query[i - 1].second){continue;}else{return false;}}return true;
}int main()
{scanf("%d",&n);for(int i = 1;i <= n;i++){for(int j = 1;j <= 4;j++){scanf("%1d",&a[i][j]);}int x,y;scanf("%d%d",&x,&y);query.push_back(make_pair(x,y));}for(int i = 0;i <= 9;i++){for(int j = 0;j <= 9;j++){if(i == j)continue;for(int k = 0;k <= 9;k++){if(k == i || k == j)continue;for(int q = 0;q <= 9;q++){if(q == i || q == j || q == k)continue;if(judge(i,j,k,q)){ans.push_back(i * 1000 + j * 100 + k * 10 + q);}}}}}if((int)ans.size() == 1){printf("%04d\n",ans[0]);}else if((int)ans.size() >= 2){printf("Need more data\n");}else{printf("Incorrect data");}return 0;
}
这篇关于B - Bulls and Cows CodeForces - 63C(构造,模拟)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!