Submission #1518272


Source Code Expand

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * http://abc025.contest.atcoder.jp/tasks/abc025_d
 */
public class Main {

	static final int N = 5;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[][] f = new int[N][N];
		Set<Integer> list = new HashSet<Integer>();
		for(int i=1; i<=N*N; i++) list.add(i);
		for(int y=0; y<N; y++){
			for(int x=0; x<N; x++){
				f[y][x] = sc.nextInt();
				list.remove(f[y][x]);
			}
		}
		sc.close();
		System.out.println(getCount(f, list));
	}

	static long getCount(int[][] f, Set<Integer> list) {
		
		if(list.isEmpty()){
			return check(f) ? 1 : 0;
		}
		
		int result = 0;
		boolean find = false;
		for(int y=0; y<N; y++){
			for(int x=0; x<N; x++){
				if(f[y][x]==0){
					find = true;
					for(int n:list){				
						int[][] f2 = getCopy(f);
						Set<Integer> list2 = new HashSet<Integer>();
						f2[y][x]=n;
						for(int i:list) if(i!=n) list2.add(i);
						result += getCount(f2,list2);			
					}
					break;
				}
			}
			if(find) break;
		}
		
		return result;
	
	}
	
	static boolean check(int[][] f){
		
		//System.out.println("------------------------");
		//for(int y=0; y<N; y++) System.out.println(Arrays.toString(f[y]));
		//System.out.println("========================");

		for(int y=0; y<N; y++){
			for(int x=0; x<N-2; x++){
				int n = f[y][x];
				if(n>f[y][x+1] && f[y][x+1]>f[y][x+2]) return false;
				if(n<f[y][x+1] && f[y][x+1]<f[y][x+2]) return false;
			}
		}
		
		for(int y=0; y<N-2; y++){
			for(int x=0; x<N; x++){
				int n = f[y][x];
				if(y<N-2 && n>f[y+1][x] && f[y+1][x]> f[y+2][x]) return false;
				if(y<N-2 && n<f[y+1][x] && f[y+1][x]< f[y+2][x]) return false;
			}
		}
		
		return true;
	}
	
	static int[][] getCopy(int[][] f){
		int[][] copy = new int[N][N];
		for(int y=0; y<N; y++)
			for(int x=0; x<N; x++)
				copy[y][x] = f[y][x];
		return copy;
	}

}

Submission Info

Submission Time
Task D - 25個の整数
User namayaki
Language Java8 (OpenJDK 1.8.0)
Score 30
Code Size 2026 Byte
Status TLE
Exec Time 5263 ms
Memory 357416 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 0 / 70
Status
AC × 4
AC × 19
AC × 19
TLE × 10
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
Subtask1 sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, test-1-01.txt, test-1-02.txt, test-1-03.txt, test-1-04.txt, test-1-05.txt, test-1-06.txt, test-1-07.txt, test-1-08.txt, test-1-09.txt, test-1-10.txt, test-1-11.txt, test-1-12.txt, test-1-13.txt, test-1-14.txt, test-1-15.txt
Subtask2 sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, test-1-01.txt, test-1-02.txt, test-1-03.txt, test-1-04.txt, test-1-05.txt, test-1-06.txt, test-1-07.txt, test-1-08.txt, test-1-09.txt, test-1-10.txt, test-1-11.txt, test-1-12.txt, test-1-13.txt, test-1-14.txt, test-1-15.txt, test-2-01.txt, test-2-02.txt, test-2-03.txt, test-2-04.txt, test-2-05.txt, test-2-06.txt, test-2-07.txt, test-2-08.txt, test-2-09.txt, test-2-10.txt
Case Name Status Exec Time Memory
sample-01.txt AC 115 ms 23636 KB
sample-02.txt AC 250 ms 52016 KB
sample-03.txt AC 117 ms 18900 KB
sample-04.txt AC 91 ms 19412 KB
test-1-01.txt AC 223 ms 47164 KB
test-1-02.txt AC 95 ms 19924 KB
test-1-03.txt AC 115 ms 21076 KB
test-1-04.txt AC 137 ms 30800 KB
test-1-05.txt AC 130 ms 26196 KB
test-1-06.txt AC 231 ms 52700 KB
test-1-07.txt AC 229 ms 51820 KB
test-1-08.txt AC 230 ms 49516 KB
test-1-09.txt AC 224 ms 53120 KB
test-1-10.txt AC 94 ms 17748 KB
test-1-11.txt AC 92 ms 21716 KB
test-1-12.txt AC 228 ms 51508 KB
test-1-13.txt AC 101 ms 20052 KB
test-1-14.txt AC 237 ms 55676 KB
test-1-15.txt AC 211 ms 47864 KB
test-2-01.txt TLE 5259 ms 176972 KB
test-2-02.txt TLE 5258 ms 236240 KB
test-2-03.txt TLE 5263 ms 357416 KB
test-2-04.txt TLE 5262 ms 238612 KB
test-2-05.txt TLE 5262 ms 351004 KB
test-2-06.txt TLE 5258 ms 240444 KB
test-2-07.txt TLE 5258 ms 238820 KB
test-2-08.txt TLE 5258 ms 236228 KB
test-2-09.txt TLE 5262 ms 350516 KB
test-2-10.txt TLE 5262 ms 259576 KB