Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
Description
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 "x bulls y cows". x represents the number of digits in the experimental number that occupy the same positions as in the sought number. y 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.
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 "aibici", 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.
Sample Input
2 1263 1 2 8103 2 1
Need more data
2 1234 2 2 1256 0 2
2134
2 0123 1 1 4567 1 2
Incorrect data
题目大意:
Bulls and Cows是一个猜数字游戏,一个人写下一个数字,另一个人猜这个数字,其中b代表数字正确,但是位置不正确的数字有几个,而c代表数字正确而且位置也正确的数字有几个。
其中猜的数字由 0 ~ 9 的不重复的4个数字组成。
给你n组数据,每行有三个数字,第一个是你猜的数字,第二个是b,第三个是c
现在如果可能猜出的结果有1个输出该数字,如果不止一个输出Need more data
如果结果不肯能出现输出Incorrect data
解析:
直接暴力求解,所有的结果,如果遇到求解的数字为符合所有的可能,记cnt++
如果cnt == 1 输出结果,cnt == 0 输出Incorrect data,如果cnt > 1输出Need more data
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 20;
struct Node {
int num[4];
int a,b;
}node[N];
int ans[4];
int vis[11];
bool ok(int num) {
memset(vis,0,sizeof(vis));
int t;
for(int i = 0; i < 4; i++) {
t = num % 10;
if(vis[t]) {
return false;
}
ans[i] = t;
vis[t] = 1;
num /= 10;
}
return true;
}
int main() {
int n;
char arr[5];
while(scanf("%d",&n) != EOF) {
for(int i = 0; i < n; i++) {
scanf("%s %d %d",arr,&node[i].a,&node[i].b);
for(int j = 0; j < 4; j++) {
node[i].num[j] = arr[j] - '0';
}
}
int cnt1,cnt2,cnt;
cnt = 0;
int res;
for(int i = 1; i < 10000; i++) {
if(ok(i)) {
int j;
for(j = 0; j < n; j++) {
cnt1 = cnt2 = 0;
for(int k = 0; k < 4; k++) {
if(ans[k] == node[j].num[k]) {
cnt1++;
}
if(vis[node[j].num[k]]) {
cnt2++;
}
}
if(cnt1 != node[j].a || cnt2 - cnt1 != node[j].b) {
break;
}
}
if(j == n) {
res = i;
cnt++;
}
}
}
if(cnt == 1) {
for(int i = 0; i < 4; i++) {
printf("%d",res%10);
res /= 10;
}
printf("\n");
}else if(cnt > 1){
printf("Need more data\n");
}else {
printf("Incorrect data\n");
}
}
return 0;
}