Submission #1131265


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] == '#';
			}
		}
		
		boolean flg = false;
		for(int i = 0; i < n; i++){
			if(board[0][i] || board[i][0]){
				flg = true;
				break;
			}
		}
		
		if(flg){
			System.out.println(-1);
			return;
		}
		
		int count = 0;
		while(!dfs(0, count, n, board)){
			count++;
		}
		
		
		System.out.println(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 3153 Byte
Status WA
Exec Time 2105 ms
Memory 223068 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 0 / 300 0 / 1000
Status
AC × 1
WA × 3
TLE × 1
AC × 3
WA × 14
TLE × 3
AC × 3
WA × 37
TLE × 3
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 WA 70 ms 20564 KB
0_01.txt TLE 2105 ms 220736 KB
0_02.txt WA 68 ms 18388 KB
0_03.txt WA 68 ms 20564 KB
0_04.txt AC 83 ms 22196 KB
1_00.txt TLE 2105 ms 220764 KB
1_01.txt WA 68 ms 18900 KB
1_02.txt WA 68 ms 21076 KB
1_03.txt WA 68 ms 21076 KB
1_04.txt WA 67 ms 21204 KB
1_05.txt AC 87 ms 23532 KB
1_06.txt WA 68 ms 20948 KB
1_07.txt WA 67 ms 18004 KB
1_08.txt WA 67 ms 19284 KB
1_09.txt WA 67 ms 21076 KB
1_10.txt WA 68 ms 20436 KB
1_11.txt WA 67 ms 21076 KB
1_12.txt TLE 2105 ms 223068 KB
1_13.txt WA 67 ms 18772 KB
1_14.txt AC 83 ms 22068 KB
2_00.txt WA 68 ms 21204 KB
2_01.txt WA 65 ms 19540 KB
2_02.txt WA 68 ms 23124 KB
2_03.txt WA 68 ms 20692 KB
2_04.txt WA 68 ms 16980 KB
2_05.txt WA 70 ms 19412 KB
2_06.txt WA 68 ms 21076 KB
2_07.txt WA 68 ms 18900 KB
2_08.txt WA 67 ms 19156 KB
2_09.txt WA 68 ms 19028 KB
2_10.txt WA 66 ms 23252 KB
2_11.txt WA 67 ms 19284 KB
2_12.txt WA 66 ms 22996 KB
2_13.txt WA 67 ms 19156 KB
2_14.txt WA 67 ms 19156 KB
2_15.txt WA 68 ms 17492 KB
2_16.txt WA 68 ms 21076 KB
2_17.txt WA 69 ms 20180 KB
2_18.txt WA 69 ms 21076 KB
2_19.txt WA 67 ms 17620 KB
2_20.txt WA 66 ms 19284 KB
2_21.txt WA 67 ms 17492 KB
2_22.txt WA 68 ms 18772 KB