写的时候多打了个叹号... 坑了很久
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
int answer = 0;
int n;
scanf("%d", &n);
int mat[17][17];
int vis[17][17];
memset(vis, 0, sizeof(mat));
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n+2; i++){
for (int j = 0; j < n+2; j++){
if (i == 0 || i == n+1 || j == 0 || j == n+1) {
mat[i][j] = 0;
vis[i][j] = 1;
}
}
}
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
cin >> mat[row][col];
if (mat[row][col]) vis[row][col] = 1;
}
}
for (;;){
bool done = true;
for (int row = 1; row <= n; row++){
for (int col = 1; col <= n; col++){
char u = 0;
if (vis[row - 1][col]) u++;
if (vis[row + 1][col]) u++;
if (vis[row][col + 1]) u++;
if (vis[row][col - 1]) u++;
char sum = mat[row - 1][col] + mat[row + 1][col] + mat[row][col + 1] + mat[row][col - 1];
if (u == 4 && (sum % 2)) answer = -1;
else if (u == 3 && !(sum % 2)) {
if (!vis[row - 1][col]){
vis[row - 1][col] = 1;
}
else if (!vis[row + 1][col]){
vis[row + 1][col] = 1;
}
else if (!vis[row][col + 1]){
vis[row][col + 1] = 1;
}
else{
vis[row][col - 1] = 1;
}
}
else if (u == 3 && (sum % 2)){
if (answer != -1) {
done = false;
answer++;
}
if (!vis[row - 1][col]){
vis[row - 1][col] = 1;
mat[row - 1][col] = 1;
}
else if (!vis[row + 1][col]){
vis[row + 1][col] = 1;
mat[row + 1][col] = 1;
}
else if (!vis[row][col + 1]){
vis[row][col + 1] = 1;
mat[row][col + 1] = 1;
}
else{
vis[row][col - 1] = 1;
mat[row][col - 1] = 1;
}
}
}
}
if (done) break;
}
printf("%d", answer);
return 0;
}