-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulls and Cows
More file actions
90 lines (77 loc) · 2.47 KB
/
Bulls and Cows
File metadata and controls
90 lines (77 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.*;
import java.util.*;
public class Main {
static class Guess {
String s;
int bulls, cows;
Guess(String s, int b, int c) {
this.s = s;
this.bulls = b;
this.cows = c;
}
}
static int[] check(String a, String b) {
int bulls = 0, cows = 0;
boolean[] usedA = new boolean[4];
boolean[] usedB = new boolean[4];
for (int i = 0; i < 4; i++) {
if (a.charAt(i) == b.charAt(i)) {
bulls++;
usedA[i] = usedB[i] = true;
}
}
for (int i = 0; i < 4; i++) {
if (usedA[i]) continue;
for (int j = 0; j < 4; j++) {
if (!usedB[j] && a.charAt(i) == b.charAt(j)) {
cows++;
usedB[j] = true;
break;
}
}
}
return new int[]{bulls, cows};
}
static boolean distinct(String s) {
boolean[] seen = new boolean[10];
for (int i = 0; i < 4; i++) {
int d = s.charAt(i) - '0';
if (seen[d]) return false;
seen[d] = true;
}
return true;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
Guess[] guesses = new Guess[n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String s = st.nextToken();
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
guesses[i] = new Guess(s, b, c);
}
List<String> valid = new ArrayList<>();
for (int x = 0; x < 10000; x++) {
String cand = String.format("%04d", x);
if (!distinct(cand)) continue;
boolean ok = true;
for (Guess g : guesses) {
int[] bc = check(cand, g.s);
if (bc[0] != g.bulls || bc[1] != g.cows) {
ok = false;
break;
}
}
if (ok) valid.add(cand);
}
if (valid.isEmpty()) {
System.out.println("Incorrect data");
} else if (valid.size() == 1) {
System.out.println(valid.get(0));
} else {
System.out.println("Need more data");
}
}
}