Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class IterativePermutation {
- public static ArrayList<Guess> guessList;
- public static int k;
- public static int correct = 0;
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- k = in.nextInt();
- guessList = new ArrayList<Guess>();
- while(true) {
- Guess cur = new Guess();
- int[] guess = new int[k];
- int placeholder = in.nextInt();
- if(placeholder == -1) {
- break;
- }
- guess[0] = placeholder;
- for(int i =1; i < k; i ++) {
- guess[i] = in.nextInt();
- }
- cur.bull = in.nextInt();
- cur.cow = in.nextInt();
- cur.guess = guess;
- guessList.add(cur);
- }
- gen(new int[k], 0, new BitSet(10));
- System.out.println(correct + " possible solutions.");
- }
- public static void gen(int[] cur, int pos, BitSet used) {
- if(pos == k -1) {
- for(int i =0; i < 10; i++) {
- if(!used.get(i)) {
- cur[pos] = i;
- // StringBuilder sb = new StringBuilder();
- // for(int j =0; j < k; j++) {
- // sb.append(cur[j]);
- // }
- // System.out.println(sb.toString());
- check(cur);
- }
- }
- return;
- }
- for(int i =0; i < 10; i++) {
- if(!used.get(i)) {
- cur[pos] = i;
- used.flip(i);
- gen(cur, pos+1,used);
- used.flip(i);
- }
- }
- }
- public static void check(int[] toBeChecked) {
- for(Guess g : guessList) {
- int bullFound = 0;
- int cowFound = 0;
- for(int i = 0; i < k; i++) {
- if(g.guess[i] == toBeChecked[i]) {
- bullFound++;
- }else {
- for(int j =0; j<k; j++) {
- if(g.guess[i] == toBeChecked[j]) {
- cowFound++;
- break;
- }
- }
- }
- }
- if(bullFound != g.bull || cowFound != g.cow) {
- return;
- }
- }
- if(correct == 0) {
- StringBuilder sb = new StringBuilder();
- for(int i =0; i < k; i++) {
- sb.append(toBeChecked[i]);
- }
- System.out.print(sb.toString());
- System.out.print(" is one of ");
- }
- correct++;
- }
- }
- class Guess{
- int[] guess;
- int bull;
- int cow;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement