Submission #1191523


Source Code Expand

import java.io.*;
import java.util.*;


public class Main {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();

		char[][] a = new char[n][];
		for (int i = 0; i < n; i++) {
			a[i] = sc.next().toCharArray();
		}

		int[] cntr = new int[n];
		int[] cnth = new int[n];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (a[i][j] == '#') {
					cnth[i]++;
					cntr[j]++;
				}
			}
		}

		boolean flag = false;
		for (int i = 0; i < n; i++) {
			if (cnth[i] > 0) {
				flag = true;
				break;
			}
		}

		if (!flag) {
			pr.println(-1);
			return;
		}

		int ret1 = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			if (cntr[i] == 0 && cnth[i] < n) {
				ret1 = Math.min(ret1, n - cnth[i] + 1);
			} else {
				ret1 = Math.min(ret1, n - cnth[i]);
			}
		}

		int ret2 = 0;
		for (int j = 0; j < n; j++) {
			if (cntr[j] < n) {
				ret2++;
			}
		}

		if (ret2 == 0) {
			pr.println(0);
		} else {
			if (ret1 == Integer.MAX_VALUE) {
				pr.println(-1);
			} else {
				pr.println(ret2 + ret1);
			}
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		private boolean isPrintable(int ch) {
			return ch >= '!' && ch <= '~';
		}

		private boolean isCRLF(int ch) {
			return ch == '\n' || ch == '\r' || ch == -1;
		}

		private int nextPrintable() {
			try {
				int ch;
				while (!isPrintable(ch = br.read())) {
					if (ch == -1) {
						throw new NoSuchElementException();
					}
				}

				return ch;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		String next() {
			try {
				int ch = nextPrintable();
				StringBuilder sb = new StringBuilder();
				do {
					sb.appendCodePoint(ch);
				} while (isPrintable(ch = br.read()));

				return sb.toString();
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		int nextInt() {
			try {
				// parseInt from Integer.parseInt()
				boolean negative = false;
				int res = 0;
				int limit = -Integer.MAX_VALUE;
				int radix = 10;

				int fc = nextPrintable();
				if (fc < '0') {
					if (fc == '-') {
						negative = true;
						limit = Integer.MIN_VALUE;
					} else if (fc != '+') {
						throw new NumberFormatException();
					}
					fc = br.read();
				}
				int multmin = limit / radix;

				int ch = fc;
				do {
					int digit = ch - '0';
					if (digit < 0 || digit >= radix) {
						throw new NumberFormatException();
					}
					if (res < multmin) {
						throw new NumberFormatException();
					}
					res *= radix;
					if (res < limit + digit) {
						throw new NumberFormatException();
					}
					res -= digit;

				} while (isPrintable(ch = br.read()));

				return negative ? res : -res;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		long nextLong() {
			try {
				// parseLong from Long.parseLong()
				boolean negative = false;
				long res = 0;
				long limit = -Long.MAX_VALUE;
				int radix = 10;

				int fc = nextPrintable();
				if (fc < '0') {
					if (fc == '-') {
						negative = true;
						limit = Long.MIN_VALUE;
					} else if (fc != '+') {
						throw new NumberFormatException();
					}
					fc = br.read();
				}
				long multmin = limit / radix;

				int ch = fc;
				do {
					int digit = ch - '0';
					if (digit < 0 || digit >= radix) {
						throw new NumberFormatException();
					}
					if (res < multmin) {
						throw new NumberFormatException();
					}
					res *= radix;
					if (res < limit + digit) {
						throw new NumberFormatException();
					}
					res -= digit;

				} while (isPrintable(ch = br.read()));

				return negative ? res : -res;
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		float nextFloat() {
			return Float.parseFloat(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			try {
				int ch;
				while (isCRLF(ch = br.read())) {
					if (ch == -1) {
						throw new NoSuchElementException();
					}
				}
				StringBuilder sb = new StringBuilder();
				do {
					sb.appendCodePoint(ch);
				} while (!isCRLF(ch = br.read()));

				return sb.toString();
			} catch (IOException e) {
				throw new NoSuchElementException();
			}
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new NoSuchElementException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}

Submission Info

Submission Time
Task B - Row to Column
User garnacha
Language Java8 (OpenJDK 1.8.0)
Score 1300
Code Size 5043 Byte
Status AC
Exec Time 128 ms
Memory 26324 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 300 / 300 1000 / 1000
Status
AC × 5
AC × 20
AC × 43
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt
Subtask 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt
Case Name Status Exec Time Memory
0_00.txt AC 70 ms 20692 KB
0_01.txt AC 70 ms 18772 KB
0_02.txt AC 70 ms 21460 KB
0_03.txt AC 69 ms 20564 KB
0_04.txt AC 68 ms 19412 KB
1_00.txt AC 68 ms 17876 KB
1_01.txt AC 70 ms 18388 KB
1_02.txt AC 70 ms 18516 KB
1_03.txt AC 79 ms 21588 KB
1_04.txt AC 73 ms 21204 KB
1_05.txt AC 69 ms 19412 KB
1_06.txt AC 70 ms 21460 KB
1_07.txt AC 69 ms 21332 KB
1_08.txt AC 70 ms 21332 KB
1_09.txt AC 78 ms 19540 KB
1_10.txt AC 72 ms 22612 KB
1_11.txt AC 77 ms 17236 KB
1_12.txt AC 70 ms 19796 KB
1_13.txt AC 69 ms 18004 KB
1_14.txt AC 68 ms 18644 KB
2_00.txt AC 122 ms 26324 KB
2_01.txt AC 126 ms 25812 KB
2_02.txt AC 123 ms 26196 KB
2_03.txt AC 126 ms 23252 KB
2_04.txt AC 128 ms 24532 KB
2_05.txt AC 121 ms 25812 KB
2_06.txt AC 124 ms 24404 KB
2_07.txt AC 118 ms 23508 KB
2_08.txt AC 128 ms 25684 KB
2_09.txt AC 125 ms 22996 KB
2_10.txt AC 116 ms 22356 KB
2_11.txt AC 127 ms 24148 KB
2_12.txt AC 125 ms 24148 KB
2_13.txt AC 116 ms 26068 KB
2_14.txt AC 124 ms 25428 KB
2_15.txt AC 117 ms 21076 KB
2_16.txt AC 120 ms 25300 KB
2_17.txt AC 107 ms 23508 KB
2_18.txt AC 108 ms 24148 KB
2_19.txt AC 128 ms 22100 KB
2_20.txt AC 120 ms 26324 KB
2_21.txt AC 117 ms 25044 KB
2_22.txt AC 122 ms 25684 KB