結果

問題 No.1345 Beautiful BINGO
ユーザー 小野寺健
提出日時 2021-05-27 18:03:23
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 3,204 bytes
コンパイル時間 4,684 ms
コンパイル使用メモリ 86,236 KB
実行使用メモリ 81,708 KB
最終ジャッジ日時 2024-11-06 11:10:40
合計ジャッジ時間 14,646 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24 TLE * 1 -- * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.HashMap;

import java.util.stream.Collectors;

public class No1345 {
	
	public static class Combination {
	    public static List<List<Integer>> make (List<Integer> candidate, int r) {
	        if (candidate.size() < r || candidate.size() <= 0 || r <= 0) {
	            List<List<Integer>> empty = new ArrayList<>();
	            empty.add(new ArrayList<>());
	            return empty;
	        }

	        List<List<Integer>> combination = new ArrayList<>();
	        for (int i = 0; i <= candidate.size() - r; i++) {
	            Integer picked = candidate.get(i);
	            List<Integer> rest = new ArrayList<>(candidate);
	            rest.subList(0, i + 1).clear();
	            combination.addAll(make(rest, r - 1).stream().map(list -> {
	                list.add(0, picked);
	                return list;
	            }).collect(Collectors.toList()));
	        }
	        return combination;
	    }
	}
	
	private static class Row {
		public List<Integer> row;
		public int cnt;
		public int[] col;
		public Row(int n) {
			row = new ArrayList<Integer>();
			cnt = 0;
			col = new int[n];
		}
		public void SetCol(int[][] A) {
			for (int i=0; i < col.length; i++) {
				for (int j : row) {
					col[i] += A[j][i];
				}
			}
		}
	}
	
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int M = scan.nextInt();
		int[] RowSum = new int[N];
		int[][] A = new int[N][N];
		for (int i=0; i < N; i++) {
			int sum = 0;
			for (int j=0; j < N; j++) {
				int a = scan.nextInt();
				sum += a;
				A[i][j] = a;
			}
			RowSum[i] = sum;
		}
		scan.close();
		Row[] RowBlt = new Row[1<<N];
		for (int i=0; i < 1 << N; i++) {
			Row row = new Row(N);
			for (int j=0; j < N; j++) {
				if ((i & (1 << j)) != 0) {
					row.cnt += RowSum[j];
				} else {
					row.row.add(j);
				}
			}
			row.SetCol(A);
			RowBlt[i] = row;
		}
		List<Integer> Range = new ArrayList<Integer>();
		for (int i=0; i < N+2; i++) {
			Range.add(i);
		}
		long total = 0, total2 = 0;
		int ans = Integer.MAX_VALUE;
		HashMap<Integer, List<List<Integer>>> combDict = new HashMap<Integer, List<List<Integer>>>();
		for (Row row : RowBlt) {
			int size = N - row.row.size();
			if (size > M || M - size > N+2) {
				continue;
			} else if (size == M) {
				ans = Math.min(ans, row.cnt);
			} else {
				List<List<Integer>> combination = null;
				if (combDict.containsKey(M-size)) {
					combination = combDict.get(M-size);
				} else {
					combination = Combination.make(Range, M-size);
					combDict.put(M-size, combination);
				}
				for (List<Integer> comb : combination) {
					int n = 0;
					for (int c : comb) {
						if (c == N) {
							for (int i : row.row) {
								if (!comb.contains(i)) {
									n += A[i][i];
								}
							}
						} else if (c == N+1) {
							for (int i : row.row) {
								if (!comb.contains(N-1-i) && !(comb.contains(N) && i == N-1-i)) {
									n += A[i][N-1-i];
								}
							}							
						} else {
							n += row.col[c];
						}
					}
					ans = Math.min(ans, row.cnt + n);
				}
			}
		}
		System.out.println(ans);
	}
}
0