题意翻译
题目描述
给你一个n∗nn*nn∗n的010101矩阵(就是每个元素只可能为000和111),你的蒟蒻是把尽量少的000变成111,使得每个元素的上,下,左,右的元素(存在的情况下)之和均为整数。
输入格式
输入的第一行为测试数据的组数T(T<=30)T(T<=30)T(T<=30).每组数据的第一行为一个正整数n(1<=n<=15)n(1<=n<=15)n(1<=n<=15);接下来nnn行每行包含了nnn个不是000就是111的整数,相邻整数间用一个空格隔开。
输出格式
对于每组数据,输出被改变的元素的最小的个数。无解则输出−1-1−1
题目描述
输入输出格式
输入格式:
输出格式:
输入输出样例
输入样例#1: 复制
3
3
0 0 0
0 0 0
0 0 0
3
0 0 0
1 0 0
0 0 0
3
1 1 1
1 1 1
0 0 0
输出样例#1: 复制
Case 1: 0
Case 2: 3
Case 3: -1
枚举第一行最终的状态,可以递推的确定以后的状态,看是否矛盾即可;
普及+/提高-难度;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-7
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }
int a[20][20];
int b[20][20];
int n;
int judge(int s) {
ms(b);
for (int i = 0; i < n; i++) {
if (s&(1 << i))b[0][i] = 1;
else if (a[0][i] == 1)return inf;
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
if (i > 1)sum += b[i - 2][j];
if (j > 0)sum += b[i - 1][j - 1];
if (j < n - 1)sum += b[i - 1][j + 1];
b[i][j] = sum % 2;
if (a[i][j] == 1 && b[i][j] == 0)return inf;
}
}
int tot = 0;
REP(i, n) REP(j, n) {
if (a[i][j] != b[i][j])tot++;
}
return tot;
}
int main()
{
//ios::sync_with_stdio(false);
int T; rdint(T);
for (int kase = 1; kase <= T; kase++) {
rdint(n);
REP(i, n)
REP(j, n)rdint(a[i][j]);
int fin = 1 << n;
int ans = inf;
for (int i = 0; i < fin; i++) {
ans = min(ans, judge(i));
}
if (ans == inf)ans = -1;
printf("Case %d: %d\n", kase, ans);
}
return 0;
}