Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Solution {
- Scanner in = new Scanner(new InputStreamReader(System.in));
- PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- private void solution() throws IOException {
- int n = in.nextInt();
- int m = in.nextInt();
- String[] grid = new String[n];
- for (int i = 0; i < n; ++i) {
- grid[i] = in.next();
- }
- int[] v = new int[n];
- for (int i = 0; i < n; ++i) {
- int nmask = 0;
- for (int j = 0; j < m; ++j) {
- if (grid[i].charAt(j) == '*') {
- nmask = nmask | (1 << j);
- }
- }
- v[i] = nmask;
- }
- int firstLength = n / 2;
- int secondLength = (n + 1) / 2;
- int[] first = new int[1 << firstLength];
- int[] second = new int[1 << secondLength];
- for (int mask = 0; mask < (1 << firstLength); ++mask) {
- int curMask = 0;
- for (int i = 0; i < firstLength; ++i) {
- if (((mask >> i) & 1) == 1) {
- curMask |= v[i];
- }
- }
- first[mask] = curMask;
- }
- for (int mask = 0; mask < (1 << secondLength); ++mask) {
- int curMask = 0;
- for (int i = 0; i < secondLength; ++i) {
- if (((mask >> i) & 1) == 1) {
- curMask |= v[n / 2 + i];
- }
- }
- second[mask] = curMask;
- }
- int res = Integer.MAX_VALUE;
- for (int mask = 0; mask < (1 << n); ++mask) {
- int a = Integer.bitCount(mask);
- int firstMask = first[((1 << firstLength) - 1) ^ (mask & ((1 << firstLength) - 1))];
- int secondMask = second[((1 << secondLength) - 1) ^ (mask >> firstLength)];
- int b = Integer.bitCount(firstMask | secondMask);
- res = Math.min(res, Math.max(a, b));
- }
- out.println(res);
- out.flush();
- }
- private class Scanner {
- BufferedReader reader;
- StringTokenizer tokenizer;
- public Scanner(Reader reader) {
- this.reader = new BufferedReader(reader);
- tokenizer = new StringTokenizer("");
- }
- public boolean hasNext() throws IOException {
- while (!tokenizer.hasMoreTokens()) {
- String line = reader.readLine();
- if (line == null) {
- return false;
- }
- tokenizer = new StringTokenizer(line);
- }
- return true;
- }
- public String nextLine() throws IOException {
- tokenizer = new StringTokenizer("");
- return reader.readLine();
- }
- public String next() throws IOException {
- hasNext();
- return tokenizer.nextToken();
- }
- public int nextInt() throws IOException, NumberFormatException {
- return Integer.parseInt(next());
- }
- }
- public static void main(String[] args) throws IOException {
- new Solution().solution();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement