結果

問題 No.460 裏表ちわーわ
ユーザー 37zigen37zigen
提出日時 2016-12-12 09:40:28
言語 Java19
(openjdk 21)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 3,263 bytes
コンパイル時間 4,567 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 60,076 KB
最終ジャッジ日時 2023-08-19 20:06:45
合計ジャッジ時間 10,914 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,564 KB
testcase_01 AC 120 ms
55,848 KB
testcase_02 AC 163 ms
59,136 KB
testcase_03 AC 119 ms
55,876 KB
testcase_04 AC 118 ms
56,196 KB
testcase_05 AC 124 ms
55,628 KB
testcase_06 AC 125 ms
55,808 KB
testcase_07 AC 127 ms
55,920 KB
testcase_08 AC 196 ms
59,524 KB
testcase_09 AC 124 ms
55,988 KB
testcase_10 AC 205 ms
59,468 KB
testcase_11 AC 125 ms
55,668 KB
testcase_12 AC 126 ms
56,112 KB
testcase_13 AC 202 ms
59,628 KB
testcase_14 AC 210 ms
59,864 KB
testcase_15 AC 197 ms
59,648 KB
testcase_16 AC 125 ms
55,768 KB
testcase_17 AC 205 ms
59,852 KB
testcase_18 AC 198 ms
60,076 KB
testcase_19 AC 198 ms
59,144 KB
testcase_20 AC 125 ms
56,884 KB
testcase_21 AC 178 ms
59,288 KB
testcase_22 AC 197 ms
59,992 KB
testcase_23 AC 177 ms
59,416 KB
testcase_24 AC 213 ms
59,484 KB
testcase_25 AC 118 ms
56,480 KB
testcase_26 AC 119 ms
55,660 KB
testcase_27 AC 118 ms
56,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.*;

public class Q460 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		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();
			}
		}

		int[][] mx = new int[M * N][M * N];
		int[] v = new int[M * N];
		for (int i = 0; i < M; ++i) {
			for (int j = 0; j < N; ++j) {
				v[i * N + j] = a[i][j];
			}
		}

		for (int i = 0; i < M; ++i) {
			for (int j = 0; j < N; ++j) {
				for (int dx = -1; dx <= 1; ++dx) {
					for (int dy = -1; dy <= 1; ++dy) {
						int nx = j + dx;
						int ny = i + dy;
						if (nx < 0 || ny < 0 || nx >= N || ny >= M)
							continue;
						mx[i * N + j][ny * N + nx] = 1;
					}
				}
			}
		}

		for (int i = 0; i < M * N; ++i) {
			int j = i;
			while (j < M * N && mx[j][i] == 0) {
				++j;
			}
			if (j == M * N)
				continue;
			if (i != j) {
				for (int k = 0; k < N * M; ++k) {
					int d = mx[i][k];
					mx[i][k] = mx[j][k];
					mx[j][k] = d;
				}
				int d = v[i];
				v[i] = v[j];
				v[j] = d;

			}
			for (int k = 0; k < M * N; ++k) {
				if (k == i)
					continue;
				if (mx[k][i] == 1) {
					for (int l = 0; l < M * N; ++l) {
						mx[k][l] = (mx[k][l] + mx[i][l]) % 2;
					}
					v[k] = (v[k] + v[i]) % 2;
				}
			}
		}

		ArrayList<Integer>[] to = new ArrayList[N * M];
		for (int i = 0; i < N * M; ++i) {
			to[i] = new ArrayList<>();
		}
		int c = 0;
		ArrayList<Integer> pending = new ArrayList<>();
		for (int i = N * M - 1; i >= 0; --i) {
			for (int k = 0; k < N * M; ++k) {
				if (mx[i][k] == 1) {
					to[k].add(i);
				}
			}
			ArrayDeque<Integer> tmp = new ArrayDeque<>();
			for (int k = i + 1; k < N * M; ++k) {
				if (mx[i][k] == 1) {
					tmp.add(k);
				}
			}
			if (tmp.size() > 0) {
				pending.addAll(tmp);
				continue;
			}
			if (tmp.size() == 0 && mx[i][i] == 1 && v[i] == 1) {
				++c;
				for (int k = 0; k <= i; ++k) {
					if (mx[k][i] == 1) {
						v[k] = v[k] ^ 1;
					}
				}
			} else if (v[i] == 1 && mx[i][i] == 0 && tmp.size() == 0) {
				if (mx[i][i] != 1) {
					System.out.println("Impossible");
					return;
				}
			}
		}
		int ans = Integer.MAX_VALUE;
		Collections.sort(pending);
		for (int i = 0; i < pending.size(); ++i) {
			while (i + 1 < pending.size() && pending.get(i) == pending.get(i + 1)) {
				pending.remove(i + 1);
			}
		}
		out: for (int i = 0; i < (1 << pending.size()); ++i) {
			int[] nv = Arrays.copyOf(v, v.length);
			int used = 0;
			for (int k = 0; k < pending.size(); ++k) {
				if ((i & (1 << k)) > 0) {
					++used;
					for (int t : to[pending.get(k)]) {
						nv[t] ^= 1;
					}
				}
			}
			for (int j = 0; j < N * M; ++j) {
				if (nv[j] == 1) {
					if (mx[j][j] == 0)
						continue out;
					else {
						++used;
						nv[j] ^= 1;
					}

				}
			}
			ans = Math.min(ans, c + used);
		}
		System.out.println(ans);
	}

	static void show(int[][] mx, int[] v, int M, int N) {
		for (int i = 0; i < M * N; ++i) {
			for (int j = 0; j < M * N; ++j) {
				System.out.print(mx[i][j] + " ");
			}
			System.out.print(v[i]);
			System.out.println();
		}
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0