Advertisement
naimul64

Worksapp3

Apr 25th, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package jp.co.worksap.global;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class CharacterRecognition {
  10.    
  11.     private static int[][][] characters = null;
  12.     private static HashMap<List<Integer>,Integer> dp = new HashMap<>();
  13.    
  14.     public static void main( String[] args ){
  15.        
  16.         Scanner in = new Scanner( System.in );
  17.        
  18.         int n = in.nextInt();
  19.         int m = in.nextInt();
  20.         int c = in.nextInt();
  21.         in.nextLine();
  22.        
  23.         //This will hold the character pixels.
  24.         characters = new int[c][n][m];
  25.        
  26.         for( int i = 0; i < c; i++ ){
  27.            
  28.             in.nextLine();
  29.             for( int j = 0; j<n; j++ ){
  30.                
  31.                 String line = in.nextLine();
  32.                
  33.                 for( int k = 0; k<m; k++ ){
  34.                    
  35.                     characters[i][j][k] = (int)( line.charAt(k) - '0' );
  36.                 }
  37.             }
  38.         }
  39.        
  40.         List<Integer> chars = new ArrayList<>();
  41.        
  42.         for( int i = 0; i<c; i++ ) chars.add( i );
  43.        
  44.         System.out.println( getMinPixel( chars,n,m ) );
  45.     }
  46.  
  47.     private static int getMinPixel( List<Integer> chars, int n, int m ) {
  48.        
  49.         //If only one character, no pixel needed to check
  50.         if( chars.size() == 1 ){
  51.             return 0;
  52.         }
  53.        
  54.         //If already solve for this character set, return previously calculated result
  55.         if( dp.containsKey( chars ) )
  56.             return dp.get( chars );
  57.        
  58.         //In worst case, we have to check all the pixels.
  59.         int min = n*m;
  60.        
  61.         for( int i = 0; i<n; i++ ){
  62.             for( int j = 0; j<m; j++ ){
  63.                
  64.                 //List to hold characters, which has zero in position i,j
  65.                 List<Integer> charHavingzero = new ArrayList<>();
  66.                
  67.                 //List to hold characters, which has one in position i,j
  68.                 List<Integer> charHavingOne = new ArrayList<>();
  69.                
  70.                 for( int k = 0; k<chars.size(); k++ ){
  71.                    
  72.                     if( characters[ chars.get(k) ][i][j] == 1 ){
  73.                         charHavingOne.add( k );
  74.                     }
  75.                     else{
  76.                         charHavingzero.add( k );
  77.                     }
  78.                 }
  79.                
  80.                 int max = 0;
  81.                
  82.                 //if one of them has zero element, pixel at i,j can't distinguish them.
  83.                 //So continue
  84.                 if( charHavingOne.isEmpty() || charHavingzero.isEmpty() )
  85.                     continue;
  86.              
  87.                 max = Math.max( getMinPixel( charHavingzero, n, m ), getMinPixel( charHavingOne, n, m ) );
  88.                  
  89.                 //Adjust min variable
  90.                 min = Math.min( min, max+1 );
  91.             }
  92.         }
  93.        
  94.         dp.put( chars, min );
  95.        
  96.         return min;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement