結果
| 問題 |
No.460 裏表ちわーわ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-11 16:22:40 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 134 ms / 2,000 ms |
| コード長 | 2,843 bytes |
| コンパイル時間 | 3,698 ms |
| コンパイル使用メモリ | 82,100 KB |
| 実行使用メモリ | 41,728 KB |
| 最終ジャッジ日時 | 2024-11-29 03:33:43 |
| 合計ジャッジ時間 | 8,575 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
import java.io.*;
import java.util.*;
public class Main_yukicoder460 {
private static Scanner sc;
private static Printer pr;
private static void solve() {
int m = sc.nextInt();
int n = sc.nextInt();
int[][] a = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
Map<Long, Integer> hm = new HashMap<>();
Map<Long, Integer> hm2 = new HashMap<>();
{
int[] mask = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (Math.abs(i - j) <= 1) {
mask[i] |= 0x1 << (n - 1 - j);
}
}
}
for (int i = 0; i < 0x1 << n; i++) {
long ans = 0;
for (int j = 0; j < n; j++) {
if ((i & 0x1 << j) != 0) {
ans ^= mask[j];
}
}
if (hm.containsKey(ans)) {
if (hm.get(ans) > Integer.bitCount(i)) {
hm.put(ans, Integer.bitCount(i));
hm2.put(ans, i);
}
} else {
hm.put(ans, Integer.bitCount(i));
hm2.put(ans, i);
}
}
}
long[][] conv = new long[m][n];
int[][] tmp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int ii = 0; ii < m; ii++) {
for (int jj = 0; jj < n; jj++) {
if (Math.abs(ii - i) <= 1 && Math.abs(jj - j) <= 1) {
tmp[ii][jj] = 1;
} else {
tmp[ii][jj] = 0;
}
}
}
conv[i][j] = enc(tmp);
}
}
long st = enc(a);
int min = Integer.MAX_VALUE;
out:
for (int i = 0; i < 0x1 << n; i++) {
long ans = st;
for (int j = 0; j < n; j++) {
if ((i & 0x1 << j) != 0) {
ans ^= conv[0][j];
}
}
int cnt = Integer.bitCount(i);
for (int j = 1; j < m; j++) {
long line = getLine(ans, j - 1, m, n);
if (!hm.containsKey(line)) {
continue out;
}
cnt += hm.get(line);
int mask = hm2.get(line);
for (int k = 0; k < n; k++) {
if ((mask & 0x1 << k) != 0) {
ans ^= conv[j][k];
}
}
}
if (getLine(ans, m - 1, m, n) == 0) {
min = Math.min(min, cnt);
}
}
if (min == Integer.MAX_VALUE) {
pr.println("Impossible");
} else {
pr.println(min);
}
}
private static long enc(int[][] a) {
int m = a.length;
int n = a[0].length;
long tmp = 0;
for (int i = 0; i < m; i++) {
tmp <<= 8;
for (int j = 0; j < n; j++) {
if (a[i][j] == 1) {
tmp |= 0x1 << n - 1 - j;
}
}
}
return tmp;
}
// k行目を取り出す
private static long getLine(long a, int k, int m, int n) {
return (a >> 8 * (m - 1 - k)) & 0xff;
}
// ---------------------------------------------------
public static void main(String[] args) {
sc = new Scanner(System.in);
pr = new Printer(System.out);
solve();
pr.close();
sc.close();
}
private static class Printer extends PrintWriter {
Printer(PrintStream out) {
super(out);
}
}
}