Submission #1131203


Source Code Expand

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
	
	public static boolean dfs(final int deep, final int limit, final int n, boolean[][] board){
		//System.out.println(deep + " " + limit + " " + Arrays.deepToString(board));
		if(deep >= limit){
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					if(!board[i][j]){ return false; }
				}
			}
			
			return true;
		}
		

		for(int row = 0; row < n; row++){
			for(int col = 0; col < n; col++){
				
				boolean[] prev_col = new boolean[n];
				boolean[] prev_row = new boolean[n];
				for(int i = 0; i < n; i++){
					prev_col[i] = board[i][col];
					prev_row[i] = board[row][i];
				}
				
				for(int i = 0; i < n; i++){
					board[i][col] = prev_row[i];
				}
				
				if(dfs(deep + 1, limit, n, board)){ return true; }
				
				for(int i = 0; i < n; i++){
					board[i][col] = prev_col[i];
				}
			}
		}
		
		return false;
	}
	
	public static void main(String[] args) throws IOException {
		final Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		
		if(n > 3){ return; }
		
		boolean[][] board = new boolean[n][n];
		for(int i = 0; i < n; i++){
			final char[] input = sc.next().toCharArray();
			
			for(int j = 0; j < n; j++){
				board[i][j] = input[j] == '#';
			}
		}
		
		int count = 0;
		while(!dfs(0, count, n, board) && count < n * n){
			count++;
		}
		
		
		System.out.println(count >= n * n ? -1 : count);
	}
	
	public static class Scanner implements Closeable {
		private BufferedReader br;
		private StringTokenizer tok;
 
		public Scanner(InputStream is) throws IOException {
			br = new BufferedReader(new InputStreamReader(is));
		}
 
		private void getLine() throws IOException {
			while (!hasNext()) {
				tok = new StringTokenizer(br.readLine());
			}
		}
 
		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}
 
		public String next() throws IOException {
			getLine();
			return tok.nextToken();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
		
		public int[] nextIntArray(int n) throws IOException {
			final int[] ret = new int[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextInt();
			}
			return ret;
		}
		
		public long[] nextLongArray(int n) throws IOException {
			final long[] ret = new long[n];
			for(int i = 0; i < n; i++){
				ret[i] = this.nextLong();
			}
			return ret;
		}
 
		public void close() throws IOException {
			br.close();
		}
	}
}

Submission Info

Submission Time
Task B - Row to Column
User mondatto
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 2996 Byte
Status WA
Exec Time 2105 ms
Memory 337756 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 0 / 300 0 / 1000
Status
AC × 5
AC × 18
TLE × 2
AC × 18
WA × 23
TLE × 2
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 71 ms 21076 KB
0_01.txt AC 67 ms 19284 KB
0_02.txt AC 68 ms 18772 KB
0_03.txt AC 70 ms 15700 KB
0_04.txt AC 85 ms 22196 KB
1_00.txt TLE 2105 ms 218844 KB
1_01.txt AC 67 ms 19156 KB
1_02.txt AC 84 ms 20148 KB
1_03.txt AC 92 ms 26936 KB
1_04.txt AC 120 ms 35852 KB
1_05.txt AC 89 ms 23024 KB
1_06.txt AC 127 ms 22160 KB
1_07.txt AC 84 ms 22068 KB
1_08.txt AC 70 ms 18644 KB
1_09.txt AC 85 ms 19412 KB
1_10.txt AC 91 ms 28344 KB
1_11.txt AC 76 ms 19156 KB
1_12.txt TLE 2105 ms 337756 KB
1_13.txt AC 77 ms 18132 KB
1_14.txt AC 84 ms 24116 KB
2_00.txt WA 67 ms 17748 KB
2_01.txt WA 68 ms 19156 KB
2_02.txt WA 68 ms 19156 KB
2_03.txt WA 69 ms 19028 KB
2_04.txt WA 67 ms 18388 KB
2_05.txt WA 69 ms 21076 KB
2_06.txt WA 67 ms 19028 KB
2_07.txt WA 68 ms 21332 KB
2_08.txt WA 69 ms 18644 KB
2_09.txt WA 68 ms 19156 KB
2_10.txt WA 70 ms 18644 KB
2_11.txt WA 69 ms 20436 KB
2_12.txt WA 71 ms 19668 KB
2_13.txt WA 67 ms 19156 KB
2_14.txt WA 72 ms 19284 KB
2_15.txt WA 67 ms 20948 KB
2_16.txt WA 66 ms 21204 KB
2_17.txt WA 69 ms 18516 KB
2_18.txt WA 69 ms 18772 KB
2_19.txt WA 69 ms 21204 KB
2_20.txt WA 69 ms 18772 KB
2_21.txt WA 66 ms 21332 KB
2_22.txt WA 72 ms 19924 KB