Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jp.co.worksap.global;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Scanner;
- public class CharacterRecognition {
- private static int[][][] characters = null;
- private static HashMap<List<Integer>,Integer> dp = new HashMap<>();
- public static void main( String[] args ){
- Scanner in = new Scanner( System.in );
- int n = in.nextInt();
- int m = in.nextInt();
- int c = in.nextInt();
- in.nextLine();
- //This will hold the character pixels.
- characters = new int[c][n][m];
- for( int i = 0; i < c; i++ ){
- in.nextLine();
- for( int j = 0; j<n; j++ ){
- String line = in.nextLine();
- for( int k = 0; k<m; k++ ){
- characters[i][j][k] = (int)( line.charAt(k) - '0' );
- }
- }
- }
- List<Integer> chars = new ArrayList<>();
- for( int i = 0; i<c; i++ ) chars.add( i );
- System.out.println( getMinPixel( chars,n,m ) );
- }
- private static int getMinPixel( List<Integer> chars, int n, int m ) {
- //If only one character, no pixel needed to check
- if( chars.size() == 1 ){
- return 0;
- }
- //If already solve for this character set, return previously calculated result
- if( dp.containsKey( chars ) )
- return dp.get( chars );
- //In worst case, we have to check all the pixels.
- int min = n*m;
- for( int i = 0; i<n; i++ ){
- for( int j = 0; j<m; j++ ){
- //List to hold characters, which has zero in position i,j
- List<Integer> charHavingzero = new ArrayList<>();
- //List to hold characters, which has one in position i,j
- List<Integer> charHavingOne = new ArrayList<>();
- for( int k = 0; k<chars.size(); k++ ){
- if( characters[ chars.get(k) ][i][j] == 1 ){
- charHavingOne.add( k );
- }
- else{
- charHavingzero.add( k );
- }
- }
- int max = 0;
- //if one of them has zero element, pixel at i,j can't distinguish them.
- //So continue
- if( charHavingOne.isEmpty() || charHavingzero.isEmpty() )
- continue;
- max = Math.max( getMinPixel( charHavingzero, n, m ), getMinPixel( charHavingOne, n, m ) );
- //Adjust min variable
- min = Math.min( min, max+1 );
- }
- }
- dp.put( chars, min );
- return min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement