Submission #1131212


Source Code Expand

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;

public class Main {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	
	static void solve()
	{
		int n = ni();
		char[][] map = nm(n,n);
		int ptn = 0;
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				ptn |= (map[i][j] == '#' ? 0 : 1)<<i*n+j;
			}
		}
		int[] d = new int[1<<n*n];
		Arrays.fill(d, 99999999);
		Queue<Integer> q = new ArrayDeque<>();
		q.add(ptn);
		d[ptn] = 0;
		while(!q.isEmpty()){
			int cur = q.poll();
			for(int i = 0;i < n;i++){
				int mem = cur>>>i*n;
				
				for(int j = 0;j < n;j++){
					int nex = cur;
					for(int k = 0;k < n;k++){
						nex &= ~(1<<k*n+j);
						nex |= (mem>>>k&1)<<k*n+j;
					}
					if(d[nex] > d[cur] + 1){
						d[nex] = d[cur] + 1;
						q.add(nex);
					}
				}
			}
		}
		tr(d);
		out.println(d[0] > 9999 ? -1 : d[0]);
	}
	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static boolean eof()
	{
		if(lenbuf == -1)return true;
		int lptr = ptrbuf;
		while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
		
		try {
			is.mark(1000);
			while(true){
				int b = is.read();
				if(b == -1){
					is.reset();
					return true;
				}else if(!isSpaceChar(b)){
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}
	
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;
	
	private static int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
//	private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static double nd() { return Double.parseDouble(ns()); }
	private static char nc() { return (char)skip(); }
	
	private static String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private static char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private static char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private static int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private static int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}

Submission Info

Submission Time
Task B - Row to Column
User uwi
Language Java8 (OpenJDK 1.8.0)
Score 300
Code Size 4205 Byte
Status RE
Exec Time 801 ms
Memory 153428 KB

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 300 / 300 0 / 1000
Status
AC × 5
AC × 20
AC × 21
RE × 22
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 18132 KB
0_01.txt AC 68 ms 19156 KB
0_02.txt AC 68 ms 19152 KB
0_03.txt AC 69 ms 21076 KB
0_04.txt AC 801 ms 18772 KB
1_00.txt AC 67 ms 19668 KB
1_01.txt AC 67 ms 15956 KB
1_02.txt AC 69 ms 15956 KB
1_03.txt AC 71 ms 19028 KB
1_04.txt AC 68 ms 19284 KB
1_05.txt AC 71 ms 20948 KB
1_06.txt AC 69 ms 19156 KB
1_07.txt AC 68 ms 21204 KB
1_08.txt AC 70 ms 21332 KB
1_09.txt AC 69 ms 19284 KB
1_10.txt AC 68 ms 19412 KB
1_11.txt AC 69 ms 19156 KB
1_12.txt AC 68 ms 19156 KB
1_13.txt AC 71 ms 21076 KB
1_14.txt AC 69 ms 19028 KB
2_00.txt RE 98 ms 21972 KB
2_01.txt AC 328 ms 20692 KB
2_02.txt RE 97 ms 19668 KB
2_03.txt RE 85 ms 19540 KB
2_04.txt RE 95 ms 21460 KB
2_05.txt RE 88 ms 20948 KB
2_06.txt RE 84 ms 18260 KB
2_07.txt RE 82 ms 21588 KB
2_08.txt RE 87 ms 21588 KB
2_09.txt RE 84 ms 21716 KB
2_10.txt RE 85 ms 19412 KB
2_11.txt RE 188 ms 18388 KB
2_12.txt RE 85 ms 18516 KB
2_13.txt RE 83 ms 21588 KB
2_14.txt RE 98 ms 21716 KB
2_15.txt RE 83 ms 20436 KB
2_16.txt RE 135 ms 153428 KB
2_17.txt RE 132 ms 151508 KB
2_18.txt RE 81 ms 20564 KB
2_19.txt RE 84 ms 19668 KB
2_20.txt RE 82 ms 20948 KB
2_21.txt RE 81 ms 20180 KB
2_22.txt RE 84 ms 18004 KB