結果

問題 No.460 裏表ちわーわ
ユーザー GrenacheGrenache
提出日時 2016-12-11 16:22:40
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 2,843 bytes
コンパイル時間 3,727 ms
コンパイル使用メモリ 75,804 KB
実行使用メモリ 56,408 KB
最終ジャッジ日時 2023-08-19 11:22:50
合計ジャッジ時間 8,494 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,088 KB
testcase_01 AC 122 ms
55,724 KB
testcase_02 AC 127 ms
55,408 KB
testcase_03 AC 121 ms
56,096 KB
testcase_04 AC 122 ms
55,376 KB
testcase_05 AC 132 ms
56,396 KB
testcase_06 AC 132 ms
53,836 KB
testcase_07 AC 136 ms
55,792 KB
testcase_08 AC 137 ms
56,212 KB
testcase_09 AC 125 ms
55,692 KB
testcase_10 AC 138 ms
55,324 KB
testcase_11 AC 136 ms
56,036 KB
testcase_12 AC 132 ms
55,552 KB
testcase_13 AC 137 ms
53,640 KB
testcase_14 AC 138 ms
55,676 KB
testcase_15 AC 137 ms
55,476 KB
testcase_16 AC 131 ms
56,408 KB
testcase_17 AC 136 ms
55,976 KB
testcase_18 AC 138 ms
55,720 KB
testcase_19 AC 138 ms
56,020 KB
testcase_20 AC 134 ms
55,584 KB
testcase_21 AC 138 ms
55,636 KB
testcase_22 AC 138 ms
55,980 KB
testcase_23 AC 139 ms
55,840 KB
testcase_24 AC 139 ms
55,904 KB
testcase_25 AC 123 ms
55,628 KB
testcase_26 AC 126 ms
56,360 KB
testcase_27 AC 125 ms
55,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
		}
	}
}
0