結果
| 問題 |
No.1345 Beautiful BINGO
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-05-27 15:52:49 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,580 bytes |
| コンパイル時間 | 4,640 ms |
| コンパイル使用メモリ | 81,976 KB |
| 実行使用メモリ | 72,356 KB |
| 最終ジャッジ日時 | 2024-11-06 09:49:38 |
| 合計ジャッジ時間 | 15,888 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 1 |
| other | AC * 19 WA * 3 TLE * 3 -- * 36 |
ソースコード
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
import java.util.Scanner;
public class No1345 {
private static List<List<Integer>> combination(List list, int count) {
List ret = new ArrayList<List<Integer>>();
for (int i=0; i < list.size(); i++) {
if (i + count > list.size()) {
break;
}
Stack stack = new Stack<Integer>();
stack.push(list.get(i));
_combination(ret, list, stack, i+1, count);
}
return ret;
}
private static void _combination(List ret, List list, Stack stack, int index, int count) {
for (int i=index; i < list.size(); i++) {
stack.push(list.get(i));
if (stack.size() == count) {
ret.add(Arrays.asList(stack.toArray()));
stack.pop();
continue;
}
_combination(ret, list, stack, i+1, count);
stack.pop();
}
}
private static class Row {
public List<Integer> row;
public int cnt;
public Row() {
row = new ArrayList<Integer>();
cnt = 0;
}
}
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;
}
Row[] RowBlt = new Row[1<<N];
for (int i=0; i < 1 << N; i++) {
Row row = new Row();
for (int j=0; j < N; j++) {
if ((i & (1 << j)) != 0) {
row.row.add(j);
row.cnt += RowSum[j];
}
}
RowBlt[i] = row;
}
List<Integer> Range = new ArrayList<Integer>();
for (int i=0; i < N+2; i++) {
Range.add(i);
}
int ans = Integer.MAX_VALUE;
for (Row row : RowBlt) {
if (row.row.size() > M) {
continue;
} else if (row.row.size() == M) {
ans = Math.min(ans, row.cnt);
} else {
for (List<Integer> comb : combination(Range, M-row.row.size())) {
int n = 0;
for (int c : comb) {
if (c == N) {
for (int i=0; i < N; i++) {
if (!row.row.contains(i) && !comb.contains(i)) {
n += A[i][i];
}
}
} else if (c == N+1) {
for (int i=0; i < N; i++) {
if (!row.row.contains(i) && !comb.contains(N-1-i) && !(comb.contains(N) && i == N-1-i)) {
n += A[i][N-1-i];
}
}
} else {
for (int i=0; i < N; i++) {
if (!row.row.contains(i)) {
n += A[i][c];
}
}
}
}
ans = Math.min(ans, row.cnt + n);
}
}
}
System.out.println(ans);
}
}