Submission #37509559


Source Code Expand

#include <cstring>
#include <cstdio>
namespace io {
	int c, t, s[100];
	template <typename T> inline void read(T& x) {
		x = 0;
		t = 0;
		c = getchar();
		while (c < 48 || c > 57) {
			if (c == 45) t = 1;
			c = getchar();
		}
		while (c >= 48 && c <= 57) {
			x = (x << 3) + (x << 1) + (c ^ 48);
			c = getchar();
		}
		if (t) x = -x;
	}
	template <typename T> inline void write(T x) {
		if (x) {
			if (x < 0) {
				putchar(45);
				x = -x;
			}
			t = 0;
			while (x) {
				s[t++] = x % 10;
				x /= 10;
			}
			while (t--) {
				putchar(s[t] ^ 48);
			}
		} else {
			putchar(48);
		}
		putchar(10);
	}
}
using io::read;
using io::write;
const int _ = 1e3 + 10;
bool a[_][_], b[_][2][2], l[2], r[2], tmp;
int n, m, k, f[_][2][2], ans;
inline void cmin(int& a, int b) {
	if (a > b) a = b;
}
int main() {
	read(n);
	read(m);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			read(k);
			a[i][j] = k;
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (((j == 1 || a[i][j] != a[i][j-1])) && ((j == m || a[i][j] != a[i][j+1]))) {
				if (i == 1) {
					l[a[i][j] ^ a[i+1][j]] = true;
				} else if (i == n) {
					r[a[i][j] ^ a[i-1][j]] = true;
				} else {
					b[i][a[i][j] ^ a[i-1][j]][a[i][j] ^ a[i+1][j]] = true;
				}
			}
		}
	}
	memset(f, 0x3F, sizeof(f));
	for (int x = 0; x <= 1; x++) {
		for (int y = 0; y <= 1; y++) {
			tmp = true;
			for (int p = 0; p <= 1; p++) {
				if (l[p] && (!((x ^ y) == p))) tmp = false;
			}
			if (tmp) f[2][x][y] = (x + y);
		}
	}
	for (int i = 2; i < n; i++) {
		for (int x = 0; x <= 1; x++) {
			for (int y = 0; y <= 1; y++) {
				for (int z = 0; z <= 1; z++) {
					if (f[i][x][y] != 0x3F3F3F3F) {
						tmp = true;
						for (int p = 0; p <= 1; p++) {
							for (int q = 0; q <= 1; q++) {
								if (b[i][p][q] && (!(((x ^ y) == p) || ((y ^ z) == q)))) tmp = false;
							}
						}
						if (tmp) cmin(f[i+1][y][z], f[i][x][y] + z);
					}
				}
			}
		}
	}
	ans = 0x3F3F3F3F;
	for (int x = 0; x <= 1; x++) {
		for (int y = 0; y <= 1; y++) {
			tmp = true;
			for (int p = 0; p <= 1; p++) {
				if (r[p] && (!((x ^ y) == p))) tmp = false;
			}
			if (tmp) cmin(ans, f[n][x][y]);
		}
	}
	if (ans == 0x3F3F3F3F) ans = -1;
	write(ans);
	return 0;
}

Submission Info

Submission Time
Task E - Don't Isolate Elements
User Ox3F
Language C++ (GCC 9.2.1)
Score 500
Code Size 2348 Byte
Status AC
Exec Time 22 ms
Memory 2540 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 30
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All example0.txt, example1.txt, example2.txt, hand0.txt, hand1.txt, hand2.txt, hand3.txt, random0.txt, random1.txt, random10.txt, random11.txt, random12.txt, random13.txt, random14.txt, random15.txt, random16.txt, random17.txt, random18.txt, random19.txt, random2.txt, random20.txt, random21.txt, random22.txt, random3.txt, random4.txt, random5.txt, random6.txt, random7.txt, random8.txt, random9.txt
Case Name Status Exec Time Memory
example0.txt AC 7 ms 1428 KiB
example1.txt AC 2 ms 1504 KiB
example2.txt AC 1 ms 1444 KiB
hand0.txt AC 1 ms 1416 KiB
hand1.txt AC 2 ms 1388 KiB
hand2.txt AC 2 ms 1556 KiB
hand3.txt AC 16 ms 2408 KiB
random0.txt AC 16 ms 2416 KiB
random1.txt AC 15 ms 2368 KiB
random10.txt AC 17 ms 2468 KiB
random11.txt AC 16 ms 2420 KiB
random12.txt AC 20 ms 2384 KiB
random13.txt AC 22 ms 2348 KiB
random14.txt AC 18 ms 2284 KiB
random15.txt AC 18 ms 2292 KiB
random16.txt AC 18 ms 2488 KiB
random17.txt AC 19 ms 2408 KiB
random18.txt AC 17 ms 2292 KiB
random19.txt AC 19 ms 2328 KiB
random2.txt AC 15 ms 2384 KiB
random20.txt AC 17 ms 2300 KiB
random21.txt AC 19 ms 2428 KiB
random22.txt AC 20 ms 2340 KiB
random3.txt AC 16 ms 2412 KiB
random4.txt AC 16 ms 2380 KiB
random5.txt AC 16 ms 2368 KiB
random6.txt AC 21 ms 2428 KiB
random7.txt AC 16 ms 2540 KiB
random8.txt AC 15 ms 2384 KiB
random9.txt AC 16 ms 2412 KiB