2016年10月3日星期一

Split the Pixel






      public static Map<String,String> map = new HashMap<>();  
      public static String closestColor(String sequence){  
        map.put("000000000000000000000000","White");  
        map.put("111111111111111111111111","Black");  
        map.put("111111110000000000000000","Red");  
        map.put("000000001111111100000000","Green");  
        map.put("000000000000000011111111","Blue");  
        int minDist = Integer.MAX_VALUE;  
        int count = 0;  
        String color="";  
        for(String seq: map.keySet()){  
          int dist = distance(sequence,seq);  
          System.out.println(dist);  
          if(minDist==dist){  
            count++;  
          }  
          if(minDist>dist){  
            minDist = dist;  
            count = 1;  
            color = map.get(seq);  
          }  
        }  
        if(count > 1) return "Ambiguous";  
        return color;    
      }  
      public static int distance(String s1, String s2){  
        int r1 = Integer.parseInt(s1.substring(0,8),2);  
        int g1 = Integer.parseInt(s1.substring(8,16),2);  
        int b1 = Integer.parseInt(s1.substring(16,24),2);  
        int r2 = Integer.parseInt(s2.substring(0,8),2);  
        int g2 = Integer.parseInt(s2.substring(8,16),2);  
        int b2 = Integer.parseInt(s2.substring(16,24),2);  
        return (int)(Math.abs(r1^2-r2^2)+Math.abs(g1^2-g2^2)+Math.abs(b1^2-b2^2));  
      }  

没有评论:

发表评论