Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Solution {
  5.     Scanner in = new Scanner(new InputStreamReader(System.in));
  6.     PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  7.  
  8.     private void solution() throws IOException {
  9.         int n = in.nextInt();
  10.         int m = in.nextInt();
  11.         String[] grid = new String[n];
  12.         for (int i = 0; i < n; ++i) {
  13.             grid[i] = in.next();
  14.         }
  15.         int[] v = new int[n];
  16.         for (int i = 0; i < n; ++i) {
  17.             int nmask = 0;
  18.             for (int j = 0; j < m; ++j) {
  19.                 if (grid[i].charAt(j) == '*') {
  20.                     nmask = nmask | (1 << j);
  21.                 }
  22.             }
  23.             v[i] = nmask;
  24.         }
  25.         int firstLength = n / 2;
  26.         int secondLength = (n + 1) / 2;
  27.         int[] first = new int[1 << firstLength];
  28.         int[] second = new int[1 << secondLength];
  29.         for (int mask = 0; mask < (1 << firstLength); ++mask) {
  30.             int curMask = 0;
  31.             for (int i = 0; i < firstLength; ++i) {
  32.                 if (((mask >> i) & 1) == 1) {
  33.                     curMask |= v[i];
  34.                 }
  35.             }
  36.             first[mask] = curMask;
  37.         }
  38.         for (int mask = 0; mask < (1 << secondLength); ++mask) {
  39.             int curMask = 0;
  40.             for (int i = 0; i < secondLength; ++i) {
  41.                 if (((mask >> i) & 1) == 1) {
  42.                     curMask |= v[n / 2 + i];
  43.                 }
  44.             }
  45.             second[mask] = curMask;
  46.         }
  47.        
  48.         int res = Integer.MAX_VALUE;
  49.         for (int mask = 0; mask < (1 << n); ++mask) {
  50.             int a = Integer.bitCount(mask);
  51.             int firstMask = first[((1 << firstLength) - 1) ^ (mask & ((1 << firstLength) - 1))];
  52.             int secondMask = second[((1 << secondLength) - 1) ^ (mask >> firstLength)];
  53.             int b = Integer.bitCount(firstMask | secondMask);
  54.             res = Math.min(res, Math.max(a, b));
  55.         }
  56.         out.println(res);
  57.         out.flush();
  58.     }
  59.  
  60.     private class Scanner {
  61.         BufferedReader reader;
  62.         StringTokenizer tokenizer;
  63.        
  64.         public Scanner(Reader reader) {
  65.             this.reader = new BufferedReader(reader);
  66.             tokenizer = new StringTokenizer("");
  67.         }
  68.  
  69.         public boolean hasNext() throws IOException {
  70.             while (!tokenizer.hasMoreTokens()) {
  71.                 String line = reader.readLine();
  72.                 if (line == null) {
  73.                     return false;
  74.                 }
  75.                 tokenizer = new StringTokenizer(line);
  76.             }
  77.             return true;
  78.         }
  79.        
  80.         public String nextLine() throws IOException {
  81.             tokenizer = new StringTokenizer("");
  82.             return reader.readLine();
  83.         }
  84.        
  85.         public String next() throws IOException {
  86.             hasNext();
  87.             return tokenizer.nextToken();
  88.         }
  89.        
  90.         public int nextInt() throws IOException, NumberFormatException {
  91.             return Integer.parseInt(next());
  92.         }
  93.        
  94.     }
  95.  
  96.     public static void main(String[] args) throws IOException {
  97.         new Solution().solution();
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement