Advertisement
Guest User

Untitled

a guest
Nov 8th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class IterativePermutation {
  4.    
  5.    
  6. public static ArrayList<Guess> guessList;
  7. public static int k;
  8. public static int correct = 0;
  9.     public static void main(String[] args) {
  10.         Scanner in = new Scanner(System.in);
  11.         k = in.nextInt();
  12.         guessList = new ArrayList<Guess>();
  13.         while(true) {
  14.             Guess cur = new Guess();
  15.             int[] guess = new int[k];
  16.             int placeholder = in.nextInt();
  17.             if(placeholder == -1) {
  18.                 break;
  19.             }
  20.             guess[0] = placeholder;
  21.             for(int i =1; i < k; i ++) {
  22.                 guess[i] = in.nextInt();
  23.             }
  24.             cur.bull = in.nextInt();
  25.             cur.cow = in.nextInt();
  26.            
  27.             cur.guess = guess;
  28.             guessList.add(cur);
  29.         }        
  30.         gen(new int[k], 0, new BitSet(10));
  31.        
  32.        
  33.         System.out.println(correct + " possible solutions.");
  34.     }
  35.     public static void gen(int[] cur, int pos, BitSet used) {
  36.         if(pos == k -1) {
  37.             for(int i =0; i < 10; i++) {
  38.                 if(!used.get(i)) {
  39.                     cur[pos] = i;
  40. //                    StringBuilder sb = new StringBuilder();
  41. //                    for(int j =0; j < k; j++) {
  42. //                        sb.append(cur[j]);
  43. //                    }
  44. //                    System.out.println(sb.toString());
  45.                     check(cur);
  46.                 }
  47.             }
  48.             return;
  49.          
  50.         }
  51.         for(int i =0; i < 10; i++) {
  52.             if(!used.get(i)) {
  53.                 cur[pos] = i;
  54.                 used.flip(i);
  55.                 gen(cur, pos+1,used);
  56.                 used.flip(i);
  57.             }
  58.         }
  59.        
  60.     }
  61.    
  62.     public static void check(int[] toBeChecked) {
  63.         for(Guess g : guessList) {
  64.             int bullFound = 0;
  65.             int cowFound  = 0;
  66.            
  67.             for(int i = 0; i < k; i++) {
  68.                 if(g.guess[i] == toBeChecked[i]) {
  69.                     bullFound++;
  70.                 }else {
  71.                     for(int j =0; j<k; j++) {
  72.                         if(g.guess[i] == toBeChecked[j]) {
  73.                             cowFound++;
  74.                             break;
  75.                         }
  76.                     }
  77.                    
  78.                 }
  79.              
  80.             }
  81.             if(bullFound != g.bull || cowFound != g.cow) {
  82.            
  83.                 return;
  84.             }
  85.         }
  86.        
  87.         if(correct == 0) {
  88.             StringBuilder sb = new StringBuilder();
  89.             for(int i =0; i < k; i++) {
  90.                 sb.append(toBeChecked[i]);
  91.             }
  92.             System.out.print(sb.toString());
  93.             System.out.print(" is one of ");
  94.         }
  95.         correct++;
  96.        
  97.     }
  98.    
  99. }
  100. class Guess{
  101.     int[] guess;
  102.     int bull;
  103.     int cow;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement