WhiZTiM

ACM-ICPC 2013 South Africa Regionals ProblemD Solution

Mar 29th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.53 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package problemd;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10. /*
  11.  * NumberType represents a possible representation of a single
  12.  * number or figure in a char array
  13.  */
  14. class NumberType
  15. {
  16.     public NumberType(int start, int stop, final char[] array)
  17.     {
  18.         this.start = start;
  19.         this.stop = stop;
  20.         this.data = array;
  21.     }
  22.     public final int start;
  23.     public final int stop;
  24.     public final char[] data;
  25. }
  26.  
  27. /**
  28.  *
  29.  * @author WhiZTiM
  30.  */
  31. public class ProblemD {
  32.  
  33.     /**
  34.      * @param args the command line arguments
  35.      */
  36.     public static void main(String[] args) {
  37.         ProblemD wahala = new ProblemD();
  38.         wahala.run_main();
  39.     }
  40.    
  41.        
  42.     private void run_main()
  43.     {
  44.         Scanner sc = new Scanner(System.in);
  45.        
  46.         //Store answers as boolean value
  47.         ArrayList<Boolean> answers = new ArrayList<>();
  48.         while(true)
  49.         {
  50.             String[] values = sc.nextLine().split("[ ]+"); //inputs
  51.            
  52.             if(values.length < 44)
  53.                 break;
  54.            
  55.             //convert to character array
  56.             char[] inputs = new char[44];
  57.             for(int i = 0; i < values.length; i++)
  58.             {
  59.                 inputs[i] = values[i].charAt(0);
  60.             }
  61.            
  62.             answers.add(check_validity(inputs));
  63.         }
  64.        
  65.         //Print answers
  66.         for(Boolean bool : answers)
  67.         {
  68.             if(bool.booleanValue())
  69.                 System.out.println("valid");
  70.             else
  71.                 System.out.println("invalid");
  72.         }
  73.     }
  74.    
  75.     private boolean check_validity(final char[] input_sequence)
  76.     {
  77.         boolean succeded = false;
  78.        
  79.         //a copy of input_sequence for validation
  80.         char[] inputs = new char[44];
  81.         System.arraycopy(input_sequence, 0, inputs, 0, 44);
  82.        
  83.         NumberType na1 = new NumberType(0, 7, inputs);
  84.         NumberType na2 = new NumberType(7, 14, inputs);
  85.         NumberType nb1 = new NumberType(16, 23, inputs);
  86.         NumberType nb2 = new NumberType(23, 30, inputs);
  87.         NumberType nc1 = new NumberType(30, 37, inputs);
  88.         NumberType nc2 = new NumberType(37, 44, inputs);
  89.        
  90.         // outer loop to remove a stick, inner loop to place a stick
  91.         // Runtime ... O(n^2) . .. . estimated: 44 x 44 = 1936 loop count
  92.         // Worst case = 44 * 44 * 7 = 14,000 approx... really cheap!!!
  93.         for(int i = 0; i < 44; i++)
  94.         {
  95.             if(inputs[i] == '0')    // if theres no stick here
  96.                 continue;
  97.             // take away the stick and use it in the inner loop
  98.             inputs[i] = '0';
  99.            
  100.             //attempt placing the loop
  101.             for(int j = 0; j < 44; j++)
  102.             {
  103.                 //The rule says a matchstick must move so, don't operate on self
  104.                 if( i == j )
  105.                     continue;
  106.                
  107.                 if(inputs[j] == '1')    // if theres a stick
  108.                     continue;
  109.                 // put the stick here
  110.                 inputs[j] = '1';
  111.                
  112.                 //Find out arithmetic operation, refer to question
  113.                 boolean IsAddition = false;
  114.                 if(inputs[14] == '1' && inputs[15] == '1')
  115.                     IsAddition = true;
  116.                 //a vertical stick alone; or no-stick is an invalid operand
  117.                 if((inputs[14] == '1' && inputs[15] == '0') ||
  118.                    (inputs[14] == '0' && inputs[15] == '0') )
  119.                 {
  120.                     inputs[j] = '0';  // revert the changes
  121.                     continue;
  122.                 }
  123.                 succeded = try_computation(na1, na2, IsAddition,
  124.                         nb1, nb2, nc1, nc2);
  125.                 if(succeded)
  126.                     break;
  127.                
  128.                 inputs[j] = '0';     // revert the changes
  129.             }
  130.            
  131.             if(succeded)
  132.                 break;
  133.            
  134.             inputs[i] = '1';        // revert the changes
  135.         }
  136.        
  137.         return succeded;
  138.     }
  139.  
  140.     /*
  141.      * Attempts to recognise number fields and then compute
  142.      * returns true if the 44 sticks represents a valid math equation
  143.      * else, returns false
  144.      */
  145.     private static boolean try_computation(NumberType na1, NumberType na2,
  146.                            boolean isAddition, NumberType nb1, NumberType nb2,
  147.                            NumberType nc1, NumberType nc2)
  148.     {
  149.         int ka1 = get_representation(na1);
  150.         int ka2 = get_representation(na2);
  151.         int kb1 = get_representation(nb1);
  152.         int kb2 = get_representation(nb2);
  153.         int kc1 = get_representation(nc1);
  154.         int kc2 = get_representation(nc2);
  155.        
  156.         if( !( is_valid(ka1) && is_valid(ka2) && is_valid(kb1) &&
  157.                is_valid(kb2) && is_valid(kc1) && is_valid(kc2)) )
  158.                 return false;
  159.         int A = concatenate_numbers(ka1, ka2);
  160.         int B = concatenate_numbers(kb1, kb2);
  161.         int C = concatenate_numbers(kc1, kc2);
  162.        
  163.         if(isAddition)
  164.             return ((A + B) == C);
  165.         return ((A - B) == C);
  166.     }
  167.    
  168.     /*
  169.      * Gets the type system's int representation of NumberType
  170.      * i.e from the range of 7 sticks, the possible int value NumberType num represents
  171.      * returns the integer value if recognized, else
  172.      * returns -1
  173.      */
  174.     private static int get_representation(final NumberType num)
  175.     {
  176.         if(equivalent(num, n0))
  177.             return 0;
  178.         if(equivalent(num, n1))
  179.             return 1;
  180.         if(equivalent(num, n2))
  181.             return 2;
  182.         if(equivalent(num, n3))
  183.             return 3;
  184.         if(equivalent(num, n4))
  185.             return 4;
  186.         if(equivalent(num, n5))
  187.             return 5;
  188.         if(equivalent(num, n6))
  189.             return 6;
  190.         if(equivalent(num, n7))
  191.             return 7;
  192.         if(equivalent(num, n8))
  193.             return 8;
  194.         if(equivalent(num, n9))
  195.             return 9;
  196.         return -1;
  197.     }
  198.    
  199.     /*
  200.      * Compares NumberType num with the char sequence in field
  201.      * returns true if they are EXACTLY the same match
  202.      * else, returns false!
  203.      */
  204.     private static boolean equivalent(NumberType num, char[] field)
  205.     {
  206.         assert(num.data.length >= field.length);
  207.         for(int i = 0; i < field.length; i++)
  208.             if(num.data[ num.start + i ] != field[i])
  209.                 return false;
  210.         return true;
  211.     }
  212.  
  213.     private static boolean is_valid(int a)
  214.     {
  215.         return a >= 0;
  216.     }
  217.    
  218.     private static int concatenate_numbers(int a, int b)
  219.     {
  220.         return (10 * a) + b;
  221.     }
  222.        
  223.     //final static char[] xx = {'0', '1', '2', '3', '4', '5', '6'};
  224.     final static char[] n0 = {'1', '0', '1', '1', '1', '1', '1'};
  225.     final static char[] n1 = {'0', '0', '0', '1', '1', '0', '0'};
  226.     final static char[] n2 = {'1', '1', '1', '1', '0', '0', '1'};
  227.     final static char[] n3 = {'1', '1', '1', '1', '1', '0', '0'};
  228.     final static char[] n4 = {'0', '1', '0', '1', '1', '1', '0'};
  229.     final static char[] n5 = {'1', '1', '1', '0', '1', '1', '0'};
  230.     final static char[] n6 = {'1', '1', '1', '0', '1', '1', '1'};
  231.     final static char[] n7 = {'1', '0', '0', '1', '1', '0', '0'};
  232.     final static char[] n8 = {'1', '1', '1', '1', '1', '1', '1'};
  233.     final static char[] n9 = {'1', '1', '0', '1', '1', '1', '0'};
  234. }
Add Comment
Please, Sign In to add comment